Excel VBA Vlookup Runtime Error 1004

…衆ロ難τιáo~ 提交于 2019-12-13 20:08:35

问题


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'.

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.

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. How can i fix this?


回答1:


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. Then I added the If IsNull check for the result.

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.



来源:https://stackoverflow.com/questions/20971739/excel-vba-vlookup-runtime-error-1004

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!