简体   繁体   English

Excel VBA Vlookup运行时错误1004

[英]Excel VBA Vlookup Runtime Error 1004

I'm trying to create a function that will run a loop that tests to see whether or not an organization (var 'org') has a campaign that has actually started yet, hence 'If (result <= Now()'. There is an unspecified number of campaigns which I'm finding in the spreadsheet with 'CountIf' and is given to the module as 'total'. 我正在尝试创建一个函数,该函数将运行一个循环,该循环进行测试以查看组织(var'org')是否已开始实际开展的活动,因此为'If(result <= Now()'。是我在电子表格中使用“ CountIf”找到的未指定数量的广告系列,并以“总计”的形式提供给模块。

In the spreadsheet when the cell which needs to have a valid campaign finds that the campaign randomly guessed in another cell isn't valid, it goes to the VBA function, giving the function both the organization ID and the total number of campaigns under that organization. 在电子表格中,当需要有效广告系列的单元格发现在另一个单元格中随机猜测的广告系列无效时,它将转到VBA功能,为该功能提供组织ID和该组织下的广告系列总数。

My codes: 我的代码:

Sub Macro()
    Dim x
    x = MacIDGen(111, 11)
End Sub

Function MacIDGen(org, total)

    Dim iteration As Boolean, result As Range

    For current = 1 To total
        result = Application.WorksheetFunction.VLookup(org & " " & current, ActiveWorkbook.Sheets("Donations").Range("C:E"), 3, False)
        If (result <= Now()) Then
            MacIDGen = org & " " & current & " Test successful"
            current = total
        End If
    Next current

End Function

Spreadsheet structure: 电子表格结构:

Org ID- Org Camp Count- Camp No.- Valid Camp No.
62      1               1         62 1
14      2               1         14 1
2       4               4         2 4
79      5               4         79 4

During debugging in VBA editor the runtime error 1004 crops up and when executing in the spreadsheet the function seeming does nothing and the cell adopts last valid value before fairly quickly refreshing cells. 在VBA编辑器中进行调试期间,会出现runtime error 1004 ,而在电子表格中执行该功能时,似乎没有执行任何操作,并且该单元格采用了最后一个有效值,然后才快速刷新单元格。 How can i fix this? 我怎样才能解决这个问题?

So for those who may stumble across this later, here's my working code: 因此,对于那些以后可能会偶然发现的人,这是我的工作代码:

Function MacIDGen2(org As Integer, total As Integer)

Dim iteration As Boolean, trueArray() As String, totalTrue As String, randArrNo As Integer, result
ReDim trueArray(total)
totalTrue = 0

For current = 0 To total - 1
    On Error Resume Next
    result = Application.WorksheetFunction.VLookup(org & " " & current + 1, ActiveWorkbook.Sheets("Campains").Range("C:E"), 3, False)
    On Error GoTo 0
    If (Not IsNull(result)) Then
        If (result <= Now()) Then
            trueArray(totalTrue) = current + 1
            totalTrue = totalTrue + 1
        End If
    End If

Next current

If (totalTrue > 0) Then
    randArrNo = WorksheetFunction.RandBetween(0, totalTrue - 1)
    MacIDGen2 = org & " " & trueArray(randArrNo)
Else
    MacIDGen2 = 0
End If

End Function

Basically the 'On Error Resume Next' fixed the issue. 基本上,“ On Error Resume Next”解决了该问题。 Then I added the If IsNull check for the result. 然后我添加了If IsNull检查结果。

I've also slightly improved the code in that it now randomly selects any one of the valid campaigns. 我还对代码进行了一些改进,因为它现在可以随机选择任何有效的广告系列。 Before it would simply pick the first valid campaign it finds. 在此之前,只需选择它找到的第一个有效广告系列即可。

Those with a keen eye may also notice that the sheet I'm referencing in the updated version is different to the one in my original code. 那些敏锐的眼睛可能还会注意到,我在更新版本中引用的工作表与原始代码中的工作表不同。 The original sheet was wrong and I was referencing the same sheet I was calling the module from, ending in a circular reference. 原始工作表是错误的,我引用的是我从中调用模块的同一工作表,以循环引用结尾。 This tiny little discrepancy cost me over a couple of hours of hair tearing confusion. 这个微小的差异使我在几个小时的头发撕裂困惑中丧生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM