Excel VBA If 语句出现内存不足错误

Excel VBA Out of Memory Error on If Statement

我正在尝试找出 Excel 中的一个问题。我收到内存不足错误,我怀疑这不是问题所在,但我真的不知道。

基本上,我正在尝试制作一个宏来搜索电子表格中 5 个选项卡内的列(该列可以在 5 个选项卡中的任何一个中,并且变化很大),当它找到它时,执行vlookup 将 return 列匹配到主选项卡中的适当位置。下面是我的代码,它似乎应该可以工作,但我收到内存不足错误。当我调试时突出显示的行是 if 语句中的第一个 Vrange = rB 行。

Dim i As Integer
Dim r As Range
'
Dim wsMaster As Worksheet: Set wsMaster = Workbooks("LBImportMacroTemplate.xlsm").Worksheets("MasterTab")
Dim wsB As Worksheet: Set wsB = Workbooks("LBImportMacroTemplate.xlsm").Worksheets("B")
Dim wsE As Worksheet: Set wsE = Workbooks("LBImportMacroTemplate.xlsm").Worksheets("E")
Dim wsL As Worksheet: Set wsL = Workbooks("LBImportMacroTemplate.xlsm").Worksheets("L")
Dim wsI As Worksheet: Set wsI = Workbooks("LBImportMacroTemplate.xlsm").Worksheets("I")
Dim wsT As Worksheet: Set wsT = Workbooks("LBImportMacroTemplate.xlsm").Worksheets("T")
'
Dim rBHeading As Range: Set rBHeading = wsB.Range("A2:ZA2")
Dim rEHeading As Range: Set rEHeading = wsE.Range("A2:ZA2")
Dim rLHeading As Range: Set rLHeading = wsL.Range("A2:ZA2")
Dim rIHeading As Range: Set rIHeading = wsI.Range("A2:ZA2")
Dim rTHeading As Range: Set rTHeading = wsT.Range("A2:ZA2")
'
Dim rB As Range: Set rB = wsB.Range("A:ZA")
Dim rE As Range: Set rE = wsE.Range("A:ZA")
Dim rL As Range: Set rL = wsL.Range("A:ZA")
Dim rI As Range: Set rI = wsI.Range("A:ZA")
Dim rT As Range: Set rT = wsT.Range("A:ZA")
'
Dim mf_iA_TEXT As String: mf_iA_TEXT = "iA"
'
If Application.CountIf(rBHeading, "iA") = 1 Then
    Vrange = rB
    Mrange = rBHeading
ElseIf Application.CountIf(rEHeading, "iA") = 1 Then
    Vrange = rE
    Mrange = rEHeading
ElseIf Application.CountIf(rLHeading, "iA") = 1 Then
    Vrange = rL
    Mrange = rLHeading
ElseIf Application.CountIf(rIHeading, "iA") = 1 Then
    Vrange = rI
    Mrange = rIHeading
Else
    Vrange = rT
    Mrange = rTHeading
End If
'
Dim mf_iA As Variant: mf_iA = Application.Match(mf_iA_TEXT, Mrange, 0)
'
With ActiveSheet
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    MsgBox lastrow
End With
'
For i = 2 To lastrow
    wsMaster.Cells(i, 2) = Application.VLookup(wsMaster.Cells(i, 1), Vrange, mf_iA, 0)
Next i
'
End Sub

我也尝试用 case 语句来完成这个,但我觉得我在上面的代码中更进一步了。如果您可以让我知道这段代码是否愚蠢,或者我是否可以解决内存不足错误,我将不胜感激。如果我能让它起作用,我将用更多的专栏复制这个过程,以防万一。谢谢!!

为了让您开始,前 56 行代码可以写成,

    Dim v As Long, vWSs As Variant, Mrange As Range, Vrange As Range, mf_iA as long
    Dim wsMaster As Worksheet: Set wsMaster = Workbooks("LBImportMacroTemplate.xlsm").Worksheets("MasterTab")
    Dim mf_iA_TEXT As String: mf_iA_TEXT = "iA"

    vWSs = Array("B", "E", "L", "I", "T")
    With Workbooks("LBImportMacroTemplate.xlsm")
        mf_iA = 0: Set Mrange = Nothing: Set Vrange = Nothing
        For v = LBound(vWSs) To UBound(vWSs)
            If CBool(Application.CountIf(.Sheets(vWSs(v)).Range("A2:ZA2"), mf_iA_TEXT)) Then
                Set Mrange = .Sheets(vWSs(v)).Range("A2:ZA2")
                Set Vrange = .Sheets(vWSs(v)).Range("A:ZA")
                ' added the column number assignment on the next line
                mf_iA = application.match(mf_iA_TEXT, Mrange, 0)
                Exit For
            End If
        Next v
    End With

    if mf_iA = 0 then msgbox "Stop here! " & mf_iA_TEXT & "not found!"
    'assumed that Mrange and Vrange are not nothing at this point
    ' and that mf_iA is the numerical column index number for mf_iA_TEXT
    'do something with them


    Set Mrange = Nothing
    Set Vrange = Nothing
    Set wsMaster = Nothing

这会将您带到 If/ElseIf/End If 的末尾,您可以在那里继续处理。最后三个只是提醒您在完成范围和工作簿对象后手动将它们设置为空。