【问题标题】:VBA using forms and textboxes使用表单和文本框的 VBA
【发布时间】:2017-02-08 04:41:51
【问题描述】:

对于如何实现文本框的代码,我有点困惑。我正在尝试设置一个显示文本框的表单,用户可以选择输入值(参数),然后将其输入另一个子程序并运行该子程序。例如,在以下代码中:

Option Explicit

Sub FillSampleTable()

'This will fill an x by y grid of numbers incrementing to use for testing purposes

Dim x As Double
Dim y As Double
Dim z As Double
Dim OffsetColumn As Integer
Dim offsetrows As Integer
Dim a As Long

Application.ScreenUpdating = False

a = 10
OffsetColumn = 1
offsetrows = 1
z = 0

For x = 1 To a

    For y = 1 To a

        z = z + 1
        Cells(x + offsetrows, y + OffsetColumn).Select
        Cells(x + offsetrows, y + OffsetColumn).Value = z

    Next y

Next x

Application.ScreenUpdating = True

End Sub

我希望有一个文本框来提示用户输入“a”的值,并且一旦加载了该值,就会运行这个子程序。如果没有输入任何值,那么它会发出错误消息框并返回到输入屏幕。 当我单击该框时,我得到:

 Private Sub GridSize_Change()
    'I renamed text box GridSize

    End Sub

但不知道该怎么办。 MS Excel v 2016。

【问题讨论】:

    标签: vba textbox userform


    【解决方案1】:

    您需要的是inputbox

    你可以做一个简单的:

    a = InputBox("Enter number a")
    

    由于您需要 a 为正整数,因此您至少应将输入框接受的数据类型指定为 1(数字)。但您可能还需要确保它是一个正整数。

    【讨论】:

    • 谢谢你是对的我用输入框完成了这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 2021-07-07
    相关资源
    最近更新 更多