+ Reply to Thread
Results 1 to 3 of 3

hard code the number of rows used with R1C1 macro recording

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    hard code the number of rows used with R1C1 macro recording

    In cell AU2 I have the following formula I want to use in a macro. I want to be able to hard code the number of rows being using (relative value)
    =IF(H2<>"","",IFERROR(INDEX($I$2:$I$23,MATCH(VLOOKUP(AP2,$AP$2:$AT$23,5,0),$AT$2:$AT$23,0)),""))
    With the macro recorder I get the following formula:
    Sub EXAMPLE()
    
    Dim LastRow As Long
    
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        AgentRow = 21
        Range("AU2:AU" & LastRow).FormulaR1C1 = "=IF(RC[-39]<>"""","""",IFERROR(INDEX(R2C9:R[21]C9,MATCH(VLOOKUP(RC[-5],R2C42:R[21]C46,5,0),R2C46:R[21]C46,0)),""""))"
        Range("AU2:AU" & LastRow) = Range("AU2:AU" & LastRow).Value
        
    End Sub
    I want to hard code the "R[21]" portion of formula. I tried changing code to following but it is not working:
    Sub EXAMPLE()
    
    Dim LastRow As Long
    
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        AgentRow = 21
        Range("AU2:AU" & LastRow).FormulaR1C1 = "=IF(RC[-39]<>"""","""",IFERROR(INDEX(R2C9:R["AgentRow"]C9,MATCH(VLOOKUP(RC[-5],R2C42:R["AgentRow"]C46,5,0),R2C46:R["AgentRow"]C46,0)),""""))"
        Range("AU2:AU" & LastRow) = Range("AU2:AU" & LastRow).Value
        
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Re: hard code the number of rows used with R1C1 macro recording

    I was able to write the code without having to use the R1C1 formula. I put the actual formula in the macro but am not able to insert the AgentCount within the formula.

    Current formula:
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        Range("AU2:AU" & LastRow) = "=IF(H2<>"""","""",IFERROR(INDEX($I$2:$I$10,MATCH(VLOOKUP(AP2,$AP$2:$AT$10,5,0),$AT$2:$AT$10,0)),""""))"
        Range("AU2:AU" & LastRow) = Range("AU2:AU" & LastRow).Value
    I am trying to update to:
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        AgentCount = 10
        Range("AU2:AU" & LastRow) = "=IF(H2<>"""","""",IFERROR(INDEX($I$2:$I & AgentCount,MATCH(VLOOKUP(AP2,$AP$2:$AT & AgentCount,5,0),$AT$2:$AT & AgentCount,0)),""""))"
        Range("AU2:AU" & LastRow) = Range("AU2:AU" & LastRow).Value
    Thanks in advance for any comments.

  3. #3
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Re: hard code the number of rows used with R1C1 macro recording

    Sorry I did not clarify on my last post. I didn't use the macro recorder with the code supplied. I used the formuala desired with slight modifications to make it work in VBA. I want to incorporate the AgentCount, which is in column H (Note: the agent count changes every week). I have attached a sample of spreadsheet with desired results (column AX). Thanks for any comments.
    Sub EXAMPLE()
    
    Dim LastRow As Long
    
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        AgentCount = Cells(Rows.Count, 8).End(xlUp).Row 'Number of Agents (count)
        Range("AU2:AU" & LastRow) = "=IF(H2<>"""","""",IFERROR(INDEX($I$2:$I$12,MATCH(VLOOKUP(AP2,$AP$2:$AT$12,5,0),$AT$2:$AT$12,0)),""""))"
        Range("AU2:AU" & LastRow) = Range("AU2:AU" & LastRow).Value
        
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1