我试图将公开声明的变量值从用户表单传递回代码最初启动的模块。我可以在userform中打印变量的值,但是当代码跳回到起始模块时它不会持久存在。 Option Explicit是双方声明的,我也声明了单个变量(忽略var2 - var6,因为它们没有被使用,直到我修复这个奇异的问题)。
代码路径从下面第一部分开始,在" StatSelection.Show"跳转到用户窗体。然后跳转到userform子的末尾。第一个debug.print提供变量,但第二个是空白..
主要代码是:
Option Explicit
Public var1 As String
Sub NoNameGame2FirstRoundResults()
'
' NoNameGame Macro
'
'
'Load Round 1 file
Dim r1Name As Variant
Dim nngr1r As Workbook
ChDir "D:\Users\stefan.bagnato\Desktop\No Name Game"
r1Name = Application.GetOpenFilename
If r1Name <> False Then
Set nngr1r = Workbooks.Open(r1Name)
End If
'Load previous week's file
Dim r1rName As Variant
Dim wps As Workbook
ChDir "D:\Users\stefan.bagnato\Desktop\Weekly Performance Summary"
r1rName = Application.GetOpenFilename
If r1rName <> False Then
Set wps = Workbooks.Open(r1rName)
End If
'Create radio buttons to select week 1 stat
StatSelection.Show
'Parse Column B for names, and paste corresponding data in column C
nngr1r.Activate
Debug.Print var1
ActiveWorkbook.Worksheets("Sheet1").Columns(2).Find("Adam").Offset(N, 1) = var1
End Sub
用户形式是:
Option Explicit
Public var1 As String
Sub OptionButton1_Click()
Dim wps As Workbook, x As String
For Each wps In Workbooks
If wps.name <> ThisWorkbook.name Then x = wps.name
Next wps
Workbooks(x).Activate
var1 = Format((ActiveWorkbook.Worksheets("Sheet1").Range("F4")), "hh:mm:ss")
var2 = Format((ActiveWorkbook.Worksheets("Sheet1").Range("F5")), "hh:mm:ss")
var3 = Format((ActiveWorkbook.Worksheets("Sheet1").Range("F6")), "hh:mm:ss")
var4 = Format((ActiveWorkbook.Worksheets("Sheet1").Range("F7")), "hh:mm:ss")
var5 = Format((ActiveWorkbook.Worksheets("Sheet1").Range("F9")), "hh:mm:ss")
var6 = Format((ActiveWorkbook.Worksheets("Sheet1").Range("F10")), "hh:mm:ss")
Debug.Print var1
Me.Hide
End Sub
答案 0 :(得分:2)
从UserForm模块中删除Public变量声明,并仅在标准模块上声明它。