VBA 使用表单和文本框

VBA using forms and textboxes

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

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' 的值,一旦加载该值,那么这个子程序就是 运行。如果没有输入任何值,那么它将向输入屏幕发送一个错误消息框和 return。 当我单击该框时,我得到:

 Private Sub GridSize_Change()
    'I renamed text box GridSize

    End Sub

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

你需要的是 inputbox.

你可以做一个简单的:

a = InputBox("Enter number a")

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