当前位置: 动力学知识库 > 问答 > 编程问答 >

textbox - VBA using forms and textboxes

问题描述:

I am a bit confused as to how to implement code for Text boxes. I am trying to set up a form which displays a text box, for which the user has the option to enter in the values (parameters) , which then are entered into another subprogram and runs that subprogram. For example, in the following code:

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

I would want to have a text box to prompt the user for the value of 'a', and once that value is loaded, then this subprogram is run. If no value is entered, then it would shoot an error msgbox and return to the entry screen.

When I click the box I get:

 Private Sub GridSize_Change()

'I renamed text box GridSize

End Sub

but don't know what to do with this. MS Excel v 2016.

网友答案:

What you need is an inputbox.

You could do a simple :

a = InputBox("Enter number a")

Since you need a to be a positive integer, you should at least specify the datatype accepted by the inputbox to 1 (number). But you probably also need to make sure it is a positive integer.

分享给朋友:
您可能感兴趣的文章:
随机阅读: