VBA声明变量不允许作为对象模块的公共成员

时间:2016-07-10 11:44:35

标签: arrays vba excel-vba global-variables excel

解决

“全局”索引变量和数组必须位于模块中,并确保ThisWorkbook和不同的工作表不包含任何这些变量。

您好, 这是我第一次在这里提问。我只在VBA中编程了大约3个星期,之前没有编程的经验。

问题:

我的问题是,是否可以将索引变量和数组声明为Public,可以由多个子程序使用,如果可以,如何使用?

问题:

我一直试图将索引变量和数组声明为Public。 当我运行第一个Sub“Public Sub ArrayToFinnish()”时没有问题,我在这个sub中重新调整数组。当我然后使用“Public Sub ArrayToFinnish()”,“Private Sub”调用另一个子时,会出现以下错误消息, 我收到一条错误消息:

“编译错误:

常量,固定长度字符串,数组,用户定义类型和Declare语句不允许作为对象模块的公共成员“

我的数组“arrData()As Variant”

也是如此
 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"

错误消息如下所示:

Compile Error window

感谢您提供的任何帮助!

//亨利克

根据要求,我会提出所有代码。调用KN

时,错误标记为&lt; ----------- 错误
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

1 个答案:

答案 0 :(得分:1)

如果您在同一个模块中同时使用两个子例程,那么只需在所有模块上方的Option Explicit部分声明它们就足够了。

我对您的代码进行了一些修改,并将一些变量声明移到了Option Explicit部分。

请查看下面的代码,我可以进入 KN 子程序。但是,检查KCell.Column值时出错,因为您没有设置KCell范围(请参阅下面的代码,我标记了错误现在的位置)

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