我是Excel VBA的新手。我有以下代码:
Private Sub CommandButton1_Click()
Dim LR2 As Long
Dim cell2 As Range
'A MODIFIER
With Sheets("A")
LR2 = .Range("B" & Rows.Count).End(xlUp).Row
For Each cell2 In .Range("A5:A" & LR2)
If cell2.Value = "" Then
cell2.Value = TextBox1.Text
Exit For
End If
Next cell2
Label8.Caption = cell2.Offset(, 1).Text
Label9.Caption = cell2.Offset(, 2).Text
Label10.Caption = cell2.Offset(, 3).Text
Label11.Caption = cell2.Offset(, 4).Text
Label12.Caption = cell2.Offset(, 5).Text
Label13.Caption = cell2.Offset(, 6).Text
End With
End Sub
现在我想将变量cell2传递给另一个按钮:
Private Sub CommandButton2_Click()
cell2.Offset(, 7).Value = TextBox2.Text
cell2.Offset(, 19).Value = TextBox3.Text
End Sub
亲切的问候
答案 0 :(得分:0)
我不同意您的方法,但您可以使用私有变量。
Private cell2 As Range '<~~ this is at the TOP of the page in the declarations area
Private Sub CommandButton1_Click()
Dim LR2 As Long
'A MODIFIER
With Sheets("A")
LR2 = .Range("B" & Rows.Count).End(xlUp).Row
For Each cell2 In .Range("A5:A" & LR2)
If cell2.Value = "" Then
cell2.Value = TextBox1.Text
Exit For
End If
Next cell2
Label8.Caption = cell2.Offset(, 1).Text
Label9.Caption = cell2.Offset(, 2).Text
Label10.Caption = cell2.Offset(, 3).Text
Label11.Caption = cell2.Offset(, 4).Text
Label12.Caption = cell2.Offset(, 5).Text
Label13.Caption = cell2.Offset(, 6).Text
End With
End Sub
Private Sub CommandButton2_Click()
cell2.Offset(, 7).Value = TextBox2.Text
cell2.Offset(, 19).Value = TextBox3.Text
End Sub