如何从VB Excel中的另一个模块调用一个模块的变量

时间:2018-05-14 05:06:52

标签: excel-vba vba excel

First Module1:

Public Sub ConnectTERDATA()
Dim fso As Object, _
Fileout As Object, _
Sql As Object, _
objNet As Object, _
FS As Object

Set cn = New ADODB.Connection
Set cm = New ADODB.Command
Set Rec_set = New ADODB.Recordset
Set fso = CreateObject("Scripting.FileSystemObject")

Dim ip As String, _
db As String, _
Row As Integer, _
SharepointAddress As String, _
LocalAddress As String, _
UserID As String, _
Pass As String, _
CommandTimeout As Integer, _
ws As Worksheet, _
DS As String

Row = 1
UserID = InputBox(Prompt:="Enter your User ID")
Pass = InputBoxDK(Prompt:="Enter your Password")
cn.ConnectionTimeout = 0 'To wait till the query finishes without generating error
cn.CommandTimeout = 5000
cn.Open "Data Source=TERA; Database=TERA; Persist Security Info=True; User ID=" & UserID & "; Password=" & Pass & "; Session Mode=ANSI;"
'Check for errors...
If cn.State = adStateOpen Then 'If connection is success, continue

MsgBox "Connected to TERADATA"
    End If
End Sub

第二单元2:

Public Sub DisconnectTERDATA()

    Call Module1.cn
    cn.Close


    MsgBox "Disconnected from TERADATA"
    End If
End Sub

但是当我调用那个变量" Cn"从module1抛出以下错误作为"预期程序不是Varaibale"谁能在这帮助我?

enter image description here

1 个答案:

答案 0 :(得分:2)

在公共Ex:

中声明您的变量
Public Userid as String
Public pass as String

然后我们可以在其他普通模块中使用它