如何调用模块来从Sub_Click执行脚本?

我的表中有10个macrosbutton。 我有一个用于客户报告脚本的模块。

当button1被单击时,我创build了一个名为Button1_Click()的新macros。 在这个脚本里面,我只想设置一个variablesRow1:

Button1_Click() Dim Row1 As Integer Row1 = 1 

从这里我想调用包含完整报告脚本的CustomerReport模块,我想在CustomerReport脚本中重新使用Row1值,因为它标识Customer 1。

我曾尝试Call CustomerReport但没有任何反应。 我如何解决这个问题?

把Module1 Public Row1 as Integer

在你的Sub中, Row1 = 1

只要你不更改它,重新加载工作簿或重置你的macros,Row1将是1 …这种方式,你可以将它设置为任何值,而无需调用另一个macros…但你仍然可以使用该值后:)

编辑:只是为了您的评论

当创buildmacros时,最好使用标准设置模式:
首先设置一些特殊的行为

 Option Explicit ... 

因为你需要它

然后根据需要声明所有全局variables和types(从types开始,根据它们声明全局variables)

 Type MyType FirstChild As Long SecondChild(0 To 20) As Byte ThirdChild As String ... End Type Public dRunner() As Variant Public MySuperVariable As MyType ... 

在第三部分把所有你需要的直接函数(api)

 Public Declare Function SetTimer Lib "user32" (ByVal HWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long Public Declare Function KillTimer Lib "user32" (ByVal HWnd As Long, ByVal nIDEvent As Long) As Long ... 

那么所有自创的function

 Public Function MyModulo(a As Double, b As Double) As Double Do Until a < b a = a - b Loop MyModulo = a End Function ... 

然后从你的潜艇开始…除了Option Explicit你不会使用他们中的大多数…但是,只是保持秩序节省了很多麻烦:)

你可以像这样传递信息:

 Sub CallingSub() Dim Row1 as Long Row1 = 1 Call CalledSub(Row1) End Sub Sub CalledSub(Row1 as Long) msgbox "Row1 = " & Row1 End Sub 

你也可以这样缩短:

 Sub CallingSub() Call CalledSub(1) '<-- Putting the 1 in here is fine and the type is not passed anyway, notice in CalledSub we define it as a long within the brackets End Sub Sub CalledSub(Row1 as Long) msgbox "Row1 = " & Row1 End Sub 

丹在那里得到了答案。 除此之外,你可以在模块中调用方法和函数,就像这样:

模块1:

 Sub Test1() MsgBox "Module1 test1" End Sub Function GetNewPay(Pay As Integer, RaisePercent As Double) As Double GetNewPay = Pay * (1 + (RaisePercent / 100)) End Function 

模块2:

 Sub Test1() MsgBox "Module2 test1" Call Module1.Test1 Call Test2("Just testing") Dim NewPay As Double, OldPay As Integer, RaisePercent As Double OldPay = 20000 RaisePercent = 15 NewPay = Module1.GetNewPay(OldPay, RaisePercent) MsgBox "Old Pay is " & OldPay & ". New pay is " & NewPay & " with " & RaisePercent & "% raise" End Sub Sub Test2(Message As String) MsgBox "Message is " & Message & " from module2 test1" End Sub