+ Reply to Thread
Results 1 to 10 of 10

Dynamic Range in Formulas VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    06-14-2021
    Location
    America
    MS-Off Ver
    Microsoft Excel for Office 365 MSO
    Posts
    32

    Dynamic Range in Formulas VBA

    Hi,

    How can I make the range in a formula to work with a dynamic range?

    * 'R2C5:R640C5' & 'R2C5:R45C5' (shown below) is a variable range on the spreadsheet and not a fixed one.
    * Attached example only has what's applicable to the questions.

    1. COUNTIF: Used to count the number of repetitions on each cell value (Item#). That'll be Column A in the attached example.

    ActiveCell.FormulaR1C1 = "=COUNTIF(R2C5:R640C5,RC[-6])"
    Range("K2").Select
    Selection.AutoFill Destination:=Range("K2:K" & Range("C" & Rows.Count).End(xlUp).Row)
    Range(Selection, Selection.End(xlDown)).Select

    2. INDEX & MATCH: Used to lookup and match 'Item#' and return 'HA#'. That'll be lookup Column A and match with Column B in Sheet#2, then return Column A from Sheet#2 to Column C.

    Range("L2").Select
    ActiveCell.FormulaR1C1 = _
    "=INDEX('HA Data'!R2C4:R45C4,MATCH('Sample Data'!RC[-7],'HA Data'!R2C5:R45C5,0),1)"
    Range("L2").Select
    Selection.AutoFill Destination:=Range("L2:L" & Range("C" & Rows.Count).End(xlUp).Row)
    Range(Selection, Selection.End(xlDown)).Select

    Hopefully this makes sense.
    Attached Files Attached Files
    Last edited by Maxpsb47; 10-13-2022 at 09:34 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,122

    Re: Dynamic Range in Formulas VBA

    Try:

    Option Explicit
    
    '1. COUNTIF: Used to count the number of repetitions on each cell value (Item#). That'll be Column A in the attached example.
    
    Sub Test1()
    
    Dim lLR As Long
    lLR = Range("C" & Rows.Count).End(xlUp).Row
    
    Dim lLR_HA As Long
    lLR_HA = Sheets("HA Data").Range("A" & Rows.Count).End(xlUp).Row
    
    Range("K2:K" & lLR).Formula = _
        "=COUNTIF($E$2:$E$" & lLR_HA & ",E2)"
    
    End Sub
    
    '2. INDEX & MATCH: Used to lookup and match 'Item#' and return 'HA#'. That'll be lookup Column A and match with Column B in Sheet#2, then return Column A from Sheet#2 to Column C.
    
    Sub Test2()
    
    Dim lLR As Long
    lLR = Range("C" & Rows.Count).End(xlUp).Row
    
    Dim lLR_HA As Long
    lLR_HA = Sheets("HA Data").Range("A" & Rows.Count).End(xlUp).Row
    
    Range("L2:L" & lLR).Formula = _
        "=INDEX('HA Data'!$D$2:$D$" & lLR_HA & ",MATCH('Sample Data'!E2,'HA Data'!$E$2:$E$" & lLR_HA & ",0),1)"
    
    End Sub
    
    
    '1. COUNTIF: Used to count the number of repetitions on each cell value (Item#). That'll be Column A in the attached example.
    
    Sub Test3()
    
    ActiveCell.FormulaR1C1 = "=COUNTIF(R2C5:R640C5,RC[-6])"
    Range("K2").Select
    Selection.AutoFill Destination:=Range("K2:K" & Range("C" & Rows.Count).End(xlUp).Row)
    Range(Selection, Selection.End(xlDown)).Select
    
    End Sub
    
    '2. INDEX & MATCH: Used to lookup and match 'Item#' and return 'HA#'. That'll be lookup Column A and match with Column B in Sheet#2, then return Column A from Sheet#2 to Column C.
    
    Sub Test4()
    
    Range("L2").Select
    ActiveCell.FormulaR1C1 = _
    "=INDEX('HA Data'!R2C4:R45C4,MATCH('Sample Data'!RC[-7],'HA Data'!R2C5:R45C5,0),1)"
    Range("L2").Select
    Selection.AutoFill Destination:=Range("L2:L" & Range("C" & Rows.Count).End(xlUp).Row)
    Range(Selection, Selection.End(xlDown)).Select
    
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    06-14-2021
    Location
    America
    MS-Off Ver
    Microsoft Excel for Office 365 MSO
    Posts
    32

    Re: Dynamic Range in Formulas VBA

    Thanks for the reply!

    The INDEX & MATCH works perfectly, but not the COUNTIF part. It's only returning 0's & 1's.

    So the value in column E go as 'Item# 1, Item# 2, and so on'. There should be 3 of each 'Item#'. I'm just using the COUNTIF formula to check this throughout the spreadsheet.

  4. #4
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Dynamic Range in Formulas VBA

    Quote Originally Posted by Maxpsb47 View Post
    So the value in column E go as 'Item# 1, Item# 2, and so on'. There should be 3 of each 'Item#'. I'm just using the COUNTIF formula to check this throughout the spreadsheet.
    Sorry but you keep mentioning column E. It turns out that neither of the two workbook sheets you uploaded to the forum contain any information in column E.

    And that's why I asked you to upload another workbook to the Forum but with that information corrected.

  5. #5
    Registered User
    Join Date
    06-14-2021
    Location
    America
    MS-Off Ver
    Microsoft Excel for Office 365 MSO
    Posts
    32

    Re: Dynamic Range in Formulas VBA

    Sorry for the confusion. I was avoiding having to upload the whole spreadsheet. Thank for the help though.

  6. #6
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Dynamic Range in Formulas VBA

    Quote Originally Posted by Maxpsb47 View Post
    Thanks for the reply!
    The INDEX & MATCH works perfectly...
    Like TMS, it's worth thanking you for the rep.

  7. #7
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Dynamic Range in Formulas VBA

    Hello.
    The workbook you uploaded to the Forum doesn't help to show you how to use COUNTIF.

    However, it is possible to make the use of the other formula dynamic (I added IFERROR). For example like this:

    PHP Code: 
    Sub Example21()
    Dim sTxt1$, sTxt2$

    With Range("'HA Data'!A1").CurrentRegion
      sTxt1 
    = .Address(external:=True)
      
    sTxt2 = .Columns(2).Address(external:=True)
    End With

    With Sheets
    ("Sample Data")
      
    With .Range("C2:C" & .Cells(Rows.Count"A").End(xlUp).Row)
        .
    Formula "=IFERROR(INDEX(" sTxt1 ", MATCH(A2, " sTxt2 ", 0), 1), ""---"")"
        
    .Value = .Value
      End With
    End With
    End Sub 

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,122

    Re: Dynamic Range in Formulas VBA

    Your formula, as coded in R1C1 style, refers to column E. if you want to count entries in column A . . .

    Range("K2:K" & lLR).Formula = _
        "=COUNTIF($A$2:$A$" & lLR_HA & ",A2)"
    And the sample formula in the workbook also refers to column E although it is IN column E.

    You do not have any data in column E other than the sample formulae.

    If you make a sample workbook, stick to the structure of the live file.

  9. #9
    Registered User
    Join Date
    06-14-2021
    Location
    America
    MS-Off Ver
    Microsoft Excel for Office 365 MSO
    Posts
    32

    Re: Dynamic Range in Formulas VBA

    Ok, I figured it out. I just needed to change the 1LR_HA to 1LR in the COUNTIF formula. Both the 'Sample Data' & 'HA Data' worksheet have different row counts. Now it works.

    Thanks again man. Appreciate the help!

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,122

    Re: Dynamic Range in Formulas VBA

    You're welcome. Thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Dynamic chart from a range containing formulas
    By chrismeeky in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 08-01-2018, 05:39 PM
  2. Dynamic range names and Formulas
    By rschoenb in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 11-21-2014, 11:49 AM
  3. Insert cells, fill formulas down, set formulas for dynamic range
    By Snickers65 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2013, 01:43 PM
  4. [SOLVED] Dynamic range - ignoring formulas
    By HuskerBronco in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-23-2013, 01:31 PM
  5. [SOLVED] Formulas' on Dynamic Range
    By knocks420 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-09-2012, 02:34 PM
  6. dynamic range of formulas
    By dreamz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2006, 03:53 PM
  7. dynamic range defined in VBA for use in formulas
    By yo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-19-2005, 02:06 PM

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