+ Reply to Thread
Results 1 to 5 of 5

Using Dynamic cell reference in cell formula created by VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2013
    Location
    Chicago, United States of America
    MS-Off Ver
    Excel 2010
    Posts
    35

    Using Dynamic cell reference in cell formula created by VBA

    Hello!

    Currently, I am attempting to write VBA code that will open a user selected file. In the file, I need 3 things calculated to be imported into the original sheet:
    1) Sum of the Qty column when column State="CA""
    2) Sum of the Qty column when column State="TX"
    3) Sum of the Qty column when column Member= "Walgreens"

    Unfortunately, The Qty, State, and Member columns do not stay in the same location each week. I have code that finds their column locations

    Dim Qty_Column As Integer
        Dim State_Column As Integer
        Dim Member_Column As Integer
        Set Rng = Range("A1:Z2")
        Qty_Column = Rng.Find("*Qty*").Column
        State_Column = Rng.Find("*State*").Column
        Member_Column = Rng.Find("*Member*").Column
    I am having trouble writing cell formulas that can utilize those cell locations

    If someone could help me out in writing formulas that will work with the variables above, or if anyone knows of a better method, that would be fantastic.

    Thank you for your time,

    Grazian2

    Some Ideas that failed:
     'Tried and failed : ActiveCell.Value = Evaluate(""=SUMIF(C[" & State_Column & "],""CA"",C[" & Qty_Column & "])"")
        ActiveCell.FormulaR1C1 = "=SUMIF(C[" & State_Column & "],""CA"",C[" & Qty_Column & "])"

    All Code:
    Sub ChargeBacks_Pull()
    '
    ' ChargeBacks_Pull Macro
    '
        Range("B47:E47").Select
        Selection.ClearContents
        
        ChargeFile = Application.GetOpenFilename
        Workbooks.Open (ChargeFile)
    
        'Finds first open row and pastes new data there
        OpenRow = Cells(Rows.Count, "B").End(xlUp).Offset(1).Row
        
        'Finds the Quy column
        Dim Qty_Column As Integer
        Dim State_Column As Integer
        Dim Member_Column As Integer
        Set Rng = Range("A1:Z2")
        Qty_Column = Rng.Find("*Qty*").Column
        State_Column = Rng.Find("*State*").Column
        Member_Column = Rng.Find("*Member*").Column
    
        'Uses the avobe references to calculate the sum of the QTY category for 3 conditions
        Cells(OpenRow, 1).Select
        'Tried and failed : ActiveCell.Value = Evaluate(""=SUMIF(C[" & State_Column & "],""CA"",C[" & Qty_Column & "])"")
        ActiveCell.FormulaR1C1 = "=SUMIF(C[" & State_Column & "],""CA"",C[" & Qty_Column & "])"
            Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
            Selection.Copy
        Windows("Dashboard_Template").Activate
        Worksheets("Totals").Activate
        Range("B47").Select
        ActiveSheet.Paste
        
        Windows(Dir(ChargeFile)).Activate
        Cells(OpenRow + 1, 1).Select
        ActiveCell.FormulaR1C1 = "=SUMIF(C[" & State_Column & "],""TX"",C[" & Qty_Column & "])"
           Selection.Copy
           Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
               :=False, Transpose:=False
            Selection.Copy
        Windows("Dashboard_Template").Activate
        Worksheets("Totals").Activate
        Range("C47").Select
        ActiveSheet.Paste
            
        Windows(Dir(ChargeFile)).Activate
        Cells(OpenRow + 2, 1).Select
        ActiveCell.FormulaR1C1 = "=SUMIF(C[Member_Column], ""*Walgreens*"",C[Qty_Column] )"
            Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
            Selection.Copy
        Windows("Dashboard_Template").Activate
        Worksheets("Totals").Activate
        Range("D47").Select
        ActiveSheet.Paste
        
    End Sub

  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
    44,868

    Re: Using Dynamic cell reference in cell formula created by VBA

    Some different approaches:

    Option Explicit
    
    Sub test()
    
    Dim rng As Range
    Dim Qty_Column As Long
    Dim State_Column As Long
    Dim Member_Column As Long
    Set rng = Range("A1:Z2")
    Qty_Column = rng.Find("*Qty*").Column
    State_Column = rng.Find("*State*").Column
    Member_Column = rng.Find("*Member*").Column
    
    ' =SUMIF(D:D,"CA",F:F)
    Range("H1").Formula = "=SUMIF(D:D,""CA"",F:F)"
    
    ' =SUMIF(D:D,"CA",F:F)
    Range("H2").Formula = "=SUMIF(" & Columns(State_Column).Address(0, 0) & ",""CA""," & Columns(Qty_Column).Address(0, 0) & ")"
    
    '=SUMIF($D:$D,"CA",$F:$F)
    Range("H3").Formula = "=SUMIF(" & Columns(State_Column).Address & ",""CA""," & Columns(Qty_Column).Address & ")"
    
    'value
    With Range("H4")
        .Formula = "=SUMIF(" & Columns(State_Column).Address & ",""CA""," & Columns(Qty_Column).Address & ")"
        .Value = .Value
    End With
    
    'value
    Range("H5").Value = Application.Evaluate("=SUMIF(" & Columns(State_Column).Address & ",""CA""," & Columns(Qty_Column).Address & ")")
    
    End Sub


    Regards, TMS


    Edit: Test Data

    HTML Code: 
    Last edited by TMS; 07-24-2013 at 05:23 PM.
    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
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Using Dynamic cell reference in cell formula created by VBA

    Sub M_snb()
      msgbox join(Array(Application.Sum(Columns(Range("A1:Z2").Find("*Qty*").Column)), Application.Sum(Columns(Range("A1:Z2").Find("*State*").Column)), Application.Sum(Columns(Range("A1:Z2").Find("*Member*").Column))),vblf)
    End Sub
    Last edited by snb; 07-24-2013 at 05:23 PM.



  4. #4
    Registered User
    Join Date
    06-19-2013
    Location
    Chicago, United States of America
    MS-Off Ver
    Excel 2010
    Posts
    35

    Re: Using Dynamic cell reference in cell formula created by VBA

    Thank you for the replies!!

    TMshucks, the second to last formula declaration worked great! Thank you for your help.

    With Range("H4")
        .Formula = "=SUMIF(" & Columns(State_Column).Address & ",""CA""," & Columns(Qty_Column).Address & ")"
        .Value = .Value
    End With

    SNB, thank you for your reply as well!

  5. #5
    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
    44,868

    Re: Using Dynamic cell reference in cell formula created by VBA

    You're welcome. Thanks for the rep.


    I like that approach as you can always comment out the ".Value = .Value" to see if the formula is what you were expecting. But, horses for courses


    Regards, TMS

+ 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. check formula integrity: replace dynamic cell reference with fixed value?
    By orchidee in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-07-2013, 06:18 PM
  2. Use Match or Lookup to create dynamic cell reference in VBA R1C1 formula
    By rmunsun1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-20-2012, 06:10 AM
  3. Dynamic Formula Ranges with reference to values in another cell
    By dunda1985 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-17-2011, 05:08 AM
  4. Replies: 0
    Last Post: 06-16-2011, 09:46 AM
  5. Dynamic Cell Reference in VBA Formula
    By mworonuk in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-26-2009, 01:41 PM

Tags for this Thread

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