简体   繁体   中英

VBA in Excel - Changing a VLOOKUP formula in a column to insert name of sheet based on static cell value

I have a workbook with multiple sheets, and new sheets will be added regularly, titled by [mmm yy] format. In my main sheet ("ContactList"), I have an IF formula with a 'nested' VLOOKUP formula in three columns to pull the respective numbers from the appropriate sheet, and I have a cell that has the date in the format I want. I want my script to look at the cell with the date in it, and use that cell's value to update the columns of VLOOKUP formulas to match that. For example, in February, the cell on my main sheet will say "Feb 20", so my VLOOKUP formulas will look in the sheet titled "Feb 20". In March, that cell will update, and I want my script (preferably automatically but tied to a button is alright) to update all the VLOOKUP functions to now be looking in the "Mar 20" sheet.

I feel like I've been trying a million things and keep getting various errors, but I'm just stuck now. My latest attempt was to set the parts of the formula as variables, then set other variables to be those parts parsed together.

        Sub Update_Counts()
        Dim rng As Range
        Dim cellnum As Integer
        Dim curr As Object
        Dim v1 As String, v2 As String, v3 As String, v4 As String, v5a As String, v5b As String, v5c As String
        Dim v6a As String, v6b As String, v6c As String, strFormC As String, strFormMR As String, strFormMD As String

        v1 = "=IF(VLOOKUP("
        v2 = Cells(cell.Row, "A")
        v3 = ",'"
        v4 = Cells(1, 6).Value
        v5a = "'!B7:F50, 2, FALSE) = 0, 'EMPTY', VLOOKUP("
        v5b = "'!B7:F50, 3, FALSE) = 0, 'EMPTY', VLOOKUP("
        v5c = "'!B7:F50, 4, FALSE) = 0, 'EMPTY', VLOOKUP("
        v6a = "'!B7:F50, 2, FALSE))"
        v6b = "'!B7:F50, 3, FALSE))"
        v6c = "'!B7:F50, 4, FALSE))"

        strFormC = v1 & v2 & v3 & v4 & v5a & v2 & v3 & v4 & v6a
        strFormMR = v1 & v2 & v3 & v4 & v5b & v2 & v3 & v4 & v6b
        strFormMD = v1 & v2 & v3 & v4 & v5c & v2 & v3 & v4 & v6c

        Set curr = Worksheets("ContactList").Cells(cellnum, 6)
        Set rng = Sheets("ContactList").Range("F3:H55")

            For cellnum = 3 To 55
                If Cells(2, 6).Value = "Commercial Total" Then
                 curr.Value = strFormC
                ElseIf Cells(2, 7).Value = "Medicare" Then
                 curr.Value = strFormMR
                ElseIf Cells(2, 8).Value = "Medicaid" Then
                 curr.Value = strFormMD
                End If
            Next cellnum
    End Sub

That's what I have thus far. I'm currently getting "Run-time error '424'; Object Required". I had thought having curr as an object would allow me to get through it, but I think my cellnum value is the "needs to be an object" portion of the For statement. However, I'm not sure how to get the cell values in there without how it's set up. I had tried a "For Each" loop but got a myriad of issues there as well. I wasn't able to find any examples of people wanting to update their cells' formulas by including a cell value, but perhaps I just wasn't looking in the right spot. Any advice is much appreciated!

As I put in my comment, you don't need to use VBA to achieve this.

If I'm reading your code correctly , I think you just need the following formula in cell A3 and copy it down:

=IF(VLOOKUP($A3,INDIRECT("'"&$F$1&"'!B7:F50"),IF($F$2="Commercial Total",2,IF($G$2="Medicare",3,IF($H$2="Medicaid",4,1))),FALSE)=0,"EMPTY",VLOOKUP($A3,INDIRECT("'"&$F$1&"'!B7:F50"),IF($F$2="Commercial Total",2,IF($G$2="Medicare",3,IF($H$2="Medicaid",4,1))),FALSE))

This is on the assumptions I've made from reading your code that:

Cell F1 contains the name of the sheet you want to get the data from.

One of Cells F2, G2 and H2 will contain the keywords Commercial Total, Medicare and Medicaid respectively.


If you want some code though, this is how I'd build the LOOKUP flexibly:

Sub Update_Counts()

    Dim rng As Range

    With Sheets("ContactList")

        Set rng = .Range("F3:F55")

        lookupformula = "=IF(VLOOKUP(A<rownum>,'<sheetname>'!B7:F50,<colnum>,FALSE)=0,""EMPTY"",VLOOKUP(A<rownum>,'<sheetname>'!B7:F50,<colnum>,FALSE))"

        For Each c In rng.Cells

            sheetname = .Cells(1, 6).Value
            thisrow = c.Row

            If .Cells(2, 6).Value = "Commercial Total" Then
                colnum = 2
            ElseIf .Cells(2, 7).Value = "Medicare" Then
                colnum = 3
            ElseIf .Cells(2, 8).Value = "Medicaid" Then
                colnum = 4
            End If

            finalformula = Replace(Replace(Replace(lookupformula, "<rownum>", thisrow), "<sheetname>", sheetname), "<colnum>", colnum)

            c.Formula = finalformula

        Next

    End With

End Sub

I figured it out from @CLR 's answer, thank you! I was struggling because I figured out the date value to stop the script from trying to import a new document, but the If/ElseIf criteria were getting a little muddied. Since the headers all always existed, it was inputting the last condition across the board, meaning I was getting column 3 data for all three columns. I am sure there's a neater way to do it, but at least for me, I'm happy with splitting the three functions and having a button run all three.

Private Sub CommandButton1_Click()
Update_Counts_COM
Update_Counts_MR
Update_Counts_MD
End Sub

Sub Update_Counts_COM()

    Dim rng As Range

    With Sheets("ContactList")

        Set rng = .Range("F3:F21,F24:F27,F30:F51")

        lookupformula = "=IFERROR(IF(VLOOKUP(A<rownum>,'<sheetname>'!B7:F59,<colnum>,FALSE)=0,""EMPTY"",VLOOKUP(A<rownum>,'<sheetname>'!B7:F59,<colnum>,FALSE)), ""Not Found"")"

        For Each c In rng.Cells

            sheetname = .Cells(1, 6).Value
            thisrow = c.Row

            If .Cells(2, 6).Value = "Commercial Total" Then
                colnum = 2

            End If

                finalformula = Replace(Replace(Replace(lookupformula, "<rownum>", thisrow), "<sheetname>", sheetname), "<colnum>", colnum)
                c.Formula = finalformula
        Next
    End With

End Sub

Sub Update_Counts_MR()

    Dim rng As Range

    With Sheets("ContactList")

        Set rng = .Range("G3:G21,G24:G27,G30:G51")

        lookupformula = "=IFERROR(IF(VLOOKUP(A<rownum>,'<sheetname>'!B7:F59,<colnum>,FALSE)=0,""EMPTY"",VLOOKUP(A<rownum>,'<sheetname>'!B7:F59,<colnum>,FALSE)), ""Not Found"")"

        For Each c In rng.Cells

            sheetname = .Cells(1, 6).Value
            thisrow = c.Row

            If .Cells(2, 7).Value = "Medicare" Then
                colnum = 3

            End If

                finalformula = Replace(Replace(Replace(lookupformula, "<rownum>", thisrow), "<sheetname>", sheetname), "<colnum>", colnum)
                c.Formula = finalformula
        Next
    End With

End Sub

Sub Update_Counts_MD()

    Dim rng As Range

    With Sheets("ContactList")

        Set rng = .Range("H3:H21,H24:H27,H30:H51")

        lookupformula = "=IFERROR(IF(VLOOKUP(A<rownum>,'<sheetname>'!B7:F59,<colnum>,FALSE)=0,""EMPTY"",VLOOKUP(A<rownum>,'<sheetname>'!B7:F59,<colnum>,FALSE)), ""Not Found"")"

        For Each c In rng.Cells

            sheetname = .Cells(1, 6).Value
            thisrow = c.Row

            If .Cells(2, 8).Value = "Medicaid" Then
                colnum = 4

            End If

                finalformula = Replace(Replace(Replace(lookupformula, "<rownum>", thisrow), "<sheetname>", sheetname), "<colnum>", colnum)
                c.Formula = finalformula
        Next
    End With

End Sub

was what ended up working for me! Thanks everyone :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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