声明VBA数组和If函数

时间:2018-05-26 19:49:31

标签: arrays excel vba excel-vba

我是VBA的初学者,我需要你帮助解决一些问题。

您将在下面找到我的代码。我收到了Sub GatheringofExpense() Dim Branches As Worksheet Dim Final As Worksheet Dim i As Integer Dim lrow As Range Dim lcol As Range Set Branches = Worksheets("Branches") Set Final = Worksheets("Final") 'Defining last row and last column in the table for our Array lrow = Range("A1000000").End(xlUp).Row lcol = Range("XFD4").End(xlToLeft).Column Mtable = Range(Cells(4, 1), Cells(lrow, lcol)) 'Assigning array for table For i = 1 To UBound(Mtable, 1) If Branches.Range("A" & i)="Barda" And Range("B" & i)="Fuzuli" Then Range("A" & i).End(xlToRight).Copy Final.Range("A1000000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll End If Next i End Sub 的编译错误。

感谢。

class TreeNode:
    def __init__(self, name, parent):
        self.name = name
        self.parent = parent
        self.children = []

    def __str__(self):
        string = "---- TreeNode Object ---- \n" + \
            "name: {} \n".format(self.name) + \
            "parent: {} \n".format(self.parent.name) + \
            "number of children: {} \n".format(len(self.children))
        return string

1 个答案:

答案 0 :(得分:1)

使用Option Explicit,将Int更改为Long,将数组声明为Variant,并限定所有范围

试试这个

Option Explicit

Public Sub GatheringOfExpense()
    Dim branches As Worksheet, final As Worksheet, lRow As Long, lCol As Long

    Set branches = Worksheets("Branches")
    Set final = Worksheets("Final")

    With branches   'Define last row and last column in "Branches" sheet, for our Array
        lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        lCol = .Cells(4, .Columns.Count).End(xlToLeft).Column

        Dim tblArr As Variant, nextRow As Long, r As Long

        tblArr = .Range(.Cells(4, 1), .Cells(lRow, lCol))   'Assig array to table
        nextRow = final.Cells(final.Rows.Count, "A").End(xlUp).Row + 1

        Application.ScreenUpdating = False
        For r = 1 To UBound(tblArr)
            If tblArr(r, 1) = "Barda" And tblArr(r, 2) = "Fuzuli" Then
               .Cells(r + 4 - 1, lCol).Copy
                final.Cells(nextRow, "A").PasteSpecial xlPasteAll
                nextRow = nextRow + 1
            End If
        Next
        Application.ScreenUpdating = True
    End With
End Sub