VBA: 在用户窗体的函数之间传递变量
VBA: Passing a variable between functions for UserForm
我正在创建一个 vba 用户窗体,在完成表单后,带有表单详细信息的字符串变量将粘贴到工作簿中的另一个 sheet。
我想在用户点击 addBtn 后将值 dStr
粘贴到一个新的 sheet 中。对此有任何帮助将不胜感激。我在其他地方读到过,您可以设置变量 public,但也不确定该怎么做。
Sub UserForm_Initialize()
Dim valueUSD, name, ric, pStr, sitchStr As String
Dim i, lRow As Long
i = 2
ric = Worksheets("Tester").Range("H" & i)
name = Worksheets("Tester").Range("B" & i)
valueUSD = Worksheets("Tester").Range("C" & i)
sitchStr = ""
dStr = ""
pStr = ric & " " & name & " " & valueUSD & " "
Label1.Caption = pStr
TextBox2.Value = ""
If activeCheck.Value = True Then
sitchStr = sitchStr + activeCheck.Caption
ElseIf itwCheck.Value = True Then
sitchStr = sitchStr + itwCheck.Caption
Else
sitchStr = ""
End If
dStr = pStr & vbNewLine & sitchStr & ", " & TextBox2.Value
End Sub
Sub addBtn_Click()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Dim myData As DataObject
Dim lastRow As Long
End Sub
设置变量public就像评论中提到的Pluatian一样。要将其插入到您想要的工作表中,您可以使用:
set pasteSheet = Application.Worksheets("Name of Paste Sheet") '<- input name of Sheet you want to paste it in
pasteSheet.Range("A1").Value = dstr '<- input cell you want to paste it in in Range
或者代替范围 属性
pasteSheet.Cells(1, 2) = dstr '<- 1 is the line number and 2 the Column Number, this example refers to Cell B1
您可以在您的 UserForm 项目中使用私有变量(在 Sub 外部使用 private 而不是 dim 设置您的声明)
如果变量需要在其他projects/functions中使用:public变量或属性
我正在创建一个 vba 用户窗体,在完成表单后,带有表单详细信息的字符串变量将粘贴到工作簿中的另一个 sheet。
我想在用户点击 addBtn 后将值 dStr
粘贴到一个新的 sheet 中。对此有任何帮助将不胜感激。我在其他地方读到过,您可以设置变量 public,但也不确定该怎么做。
Sub UserForm_Initialize()
Dim valueUSD, name, ric, pStr, sitchStr As String
Dim i, lRow As Long
i = 2
ric = Worksheets("Tester").Range("H" & i)
name = Worksheets("Tester").Range("B" & i)
valueUSD = Worksheets("Tester").Range("C" & i)
sitchStr = ""
dStr = ""
pStr = ric & " " & name & " " & valueUSD & " "
Label1.Caption = pStr
TextBox2.Value = ""
If activeCheck.Value = True Then
sitchStr = sitchStr + activeCheck.Caption
ElseIf itwCheck.Value = True Then
sitchStr = sitchStr + itwCheck.Caption
Else
sitchStr = ""
End If
dStr = pStr & vbNewLine & sitchStr & ", " & TextBox2.Value
End Sub
Sub addBtn_Click()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Dim myData As DataObject
Dim lastRow As Long
End Sub
设置变量public就像评论中提到的Pluatian一样。要将其插入到您想要的工作表中,您可以使用:
set pasteSheet = Application.Worksheets("Name of Paste Sheet") '<- input name of Sheet you want to paste it in
pasteSheet.Range("A1").Value = dstr '<- input cell you want to paste it in in Range
或者代替范围 属性
pasteSheet.Cells(1, 2) = dstr '<- 1 is the line number and 2 the Column Number, this example refers to Cell B1
您可以在您的 UserForm 项目中使用私有变量(在 Sub 外部使用 private 而不是 dim 设置您的声明)
如果变量需要在其他projects/functions中使用:public变量或属性