Arrays VBA声明的变量不允许作为对象模块的公共成员 解决了的
“全局”索引变量和数组必须位于模块中,并确保此工作簿和不同的工作表不包含任何这些变量Arrays VBA声明的变量不允许作为对象模块的公共成员 解决了的,arrays,vba,excel,global-variables,Arrays,Vba,Excel,Global Variables,“全局”索引变量和数组必须位于模块中,并确保此工作簿和不同的工作表不包含任何这些变量 你好,, 这是我第一次在这里问问题。我只在VBA中编程了大约3周,之前没有编程经验 问题是: 我的问题是,是否可以将索引变量和数组声明为公共的,可以由多个子例程使用,如果可以,如何声明 问题是: 我一直在尝试将索引变量和数组声明为公共。 当我运行第一个子系统“Public Sub arraytofennish()”时,没有问题,我重新分配了此子系统中的数组。然后,当我使用“Public Sub arrayto
你好,, 这是我第一次在这里问问题。我只在VBA中编程了大约3周,之前没有编程经验 问题是: 我的问题是,是否可以将索引变量和数组声明为公共的,可以由多个子例程使用,如果可以,如何声明 问题是: 我一直在尝试将索引变量和数组声明为公共。 当我运行第一个子系统“Public Sub arraytofennish()”时,没有问题,我重新分配了此子系统中的数组。然后,当我使用“Public Sub arraytofennish()”和“Private Sub”调用另一个子系统时,会出现下面的错误消息,并且 我收到一条错误消息,上面说: “编译错误: 常量、固定长度字符串、数组、用户定义类型和Declare语句不允许作为对象模块的公共成员” 我的数组“arrData()作为变量”也会发生同样的情况
选项显式
公共wb作为工作簿
公共小区范围
公共i1尽可能长
将A(1到4)公开为字符串'=1000
对于j2=1至UBound(A,1)
arrData(arrRow,j2)=.单元格(i1,j2).值
下一个j2
如果结束
其他情况
结束选择
端接头
如果在同一个模块中使用两个子例程,那么只需在所有模块上方的选项Explicit
部分声明它们就足够了
我对您的代码做了一些修改,并将一些变量声明移动到选项Explicit
部分
请看下面的代码,我可以进入KN子程序。但是,在检查KCell.Column
值时出现错误,因为您没有设置KCell范围(请参见下面的代码,我标记了错误的位置)
你能把剩下的代码发布出来,并说明你的错误是从哪里来的吗?无论如何,当我使用变量和数组时,我希望所有模块中的所有函数和子例程都可以访问,我更喜欢在单独的模块中将它们标记为
Global
变量。
Option Explicit
Public wb As Workbook
Public Cell As Range
Public i1 As Long
Public A(1 To 4) As String '<---- Indexed Variable
Public arrRow As Long
Public arrData() As Variant '<---- Array
Public Sub ArrayToFinnish()
Dim i2 As Long
Dim j1 As Long, j2 As Long
Dim Cell As String
Dim lRow As Long
Dim lCol As Long
Dim rng As Range
Dim aCell As Range
A(1) = "String1"
A(2) = "String1"
A(3) = "String1"
A(4) = "String1"
Option Explicit
Public wb As Workbook
Public Cell As Range
Public i1 As Long
Public A(1 To 4) As String
Public arrRow As Long
Public arrData() As Variant
'Public ListGroup()
Public Sub ArrayToFinnish()
'Dim A(1 To 4) As String
'Dim i1 As Long
Dim i2 As Long
Dim j1 As Long, j2 As Long
'Dim arrData() As Variant
Dim Cell As String
Dim lRow As Long
Dim lCol As Long
Dim rng As Range
Dim aCell As Range
A(1) = "Ship Via Description"
A(2) = "Speditor"
A(3) = "Planned Ship Date/Time"
A(4) = "Weight"
'A(4) = "Customer Order"
'A(5) = "Customer Number"
Sheet1.Activate
lRow = Sheet1.Cells.Find(What:="*", LookIn:=xlValues, SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
lCol = Sheet1.Cells.Find(What:="*", LookIn:=xlValues, SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Set rng = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(1, lCol))
ReDim arrData(1 To lRow, 1 To UBound(A, 1))
'ListGroup = arrData(1 To lRow, 1 To Ubound(A,1))
For i1 = 2 To lRow
For j1 = 1 To UBound(A, 1)
Set aCell = rng.Find(A(j1))
Cell = Sheet1.Cells(i1, aCell.Column).Value
Select Case Cell
Case Cell = "EXPRESS"
Case Cell = "TRUCK"
Case Cell = "CZ/DACHSER/Axis Communications LLC"
Case Cell = "DE/ASH Logistik/Abris"
Case Cell = "DE/EXP Cargo/RRC Cent. Asia"
Case Cell = "HU/Trans-Gate/IQ Trading"
Case Cell = "USA/Atlanta/Splitpoint"
Case "AIRFREIGHT"
arrRow = arrRow + 1
KN <-----------**Error when calling KN**
Case Cell = "China/Shanghai/Splitpoint"
Case Cell = "Singapore/KN/CDP"
Case Cell = "US/Geodis/Miami"
Case Cell = "BR/Sao Paulo/Splitpoint"
Case Cell = "Japan / Multitek / Warehouse"
End Select
Next j1
Next i1
End Sub
Private Sub KN()
'DELETE should be global
Dim wb As Workbook
Set wb = ThisWorkbook
lRow = Sheet1.Cells.Find(What:="*", LookIn:=xlValues, SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
lCol = Sheet1.Cells.Find(What:="*", LookIn:=xlValues, SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
'DELETE
Dim ws As Worksheet
Dim j3 As Long
Dim KCellD As Range, KCellW As Range
Dim D As Date
Set ws = wb.ActiveSheet
Set rng = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(1, lCol))
Set KCellD = rng.Find(A(3))
Set KCellW = rng.Find(A(4))
With ws
D = Sheet1.Cells(i1, KCell.Column)
Select Case D
Case DateAdd("d", 1, Date)
If .Cells(i1, KCellW.Column).Value >= 50 Then
For j2 = 1 To UBound(A, 1)
arrData(arrRow, j2) = .Cells(i1, j2).Value
Next j2
End If
Case DateAdd("d", 2, Date)
If .Cells(i1, KCellW.Column).Value >= 1000 Then
For j2 = 1 To UBound(A, 1)
arrData(arrRow, j2) = .Cells(i1, j2).Value
Next j2
End If
Case Else
End Select
End Sub
Option Explicit
Dim wb As Workbook
Dim Cell, rng As Range
Dim A(1 To 4) As String
Dim arrData() As Variant
Dim arrRow, lRow, lCol As Long
Dim i1, i2, j1, j2 As Long
'Public ListGroup()
Public Sub ArrayToFinnish()
Dim Cell As String
Dim aCell As Range
A(1) = "Ship Via Description"
A(2) = "Speditor"
A(3) = "Planned Ship Date/Time"
A(4) = "Weight"
'A(4) = "Customer Order"
'A(5) = "Customer Number"
Sheet1.Activate
lRow = Sheet1.Cells.Find(What:="*", LookIn:=xlValues, SearchDirection:=xlPrevious, SearchOrder:=xlByRows).row
lCol = Sheet1.Cells.Find(What:="*", LookIn:=xlValues, SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Set rng = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(1, lCol))
ReDim arrData(1 To lRow, 1 To UBound(A, 1))
'ListGroup = arrData(1 To lRow, 1 To Ubound(A,1))
For i1 = 2 To lRow
For j1 = 1 To UBound(A, 1)
Set aCell = rng.Find(A(j1))
Cell = Sheet1.Cells(i1, aCell.Column).Value
Select Case Cell
Case Cell = "EXPRESS"
Case Cell = "TRUCK"
Case Cell = "CZ/DACHSER/Axis Communications LLC"
Case Cell = "DE/ASH Logistik/Abris"
Case Cell = "DE/EXP Cargo/RRC Cent. Asia"
Case Cell = "HU/Trans-Gate/IQ Trading"
Case Cell = "USA/Atlanta/Splitpoint"
Case "AIRFREIGHT"
arrRow = arrRow + 1
KN
Case Cell = "China/Shanghai/Splitpoint"
Case Cell = "Singapore/KN/CDP"
Case Cell = "US/Geodis/Miami"
Case Cell = "BR/Sao Paulo/Splitpoint"
Case Cell = "Japan / Multitek / Warehouse"
End Select
Next j1
Next i1
End Sub
Private Sub KN()
Dim ws As Worksheet
Dim KCell, KCellD, KCellW As Range
'Dim j3 As Long
Dim D As Date
Set wb = ThisWorkbook
lRow = Sheet1.Cells.Find(What:="*", LookIn:=xlValues, SearchDirection:=xlPrevious, SearchOrder:=xlByRows).row
lCol = Sheet1.Cells.Find(What:="*", LookIn:=xlValues, SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Set ws = wb.ActiveSheet
Set rng = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(1, lCol))
Set KCellD = rng.Find(A(3))
Set KCellW = rng.Find(A(4))
With ws
' ****** Getting an error here , you are not setting KCell Range ******
D = .Cells(i1, KCell.Column)
Select Case D
Case DateAdd("d", 1, Date)
If .Cells(i1, KCellW.Column).Value >= 50 Then
For j2 = 1 To UBound(A, 1)
arrData(arrRow, j2) = .Cells(i1, j2).Value
Next j2
End If
Case DateAdd("d", 2, Date)
If .Cells(i1, KCellW.Column).Value >= 1000 Then
For j2 = 1 To UBound(A, 1)
arrData(arrRow, j2) = .Cells(i1, j2).Value
Next j2
End If
Case Else ' not sure why need, you are not using it
End Select
End With
End Sub