+ Reply to Thread
Results 1 to 16 of 16

Reference Dynamic Column in Formula Not Using Relative References.

  1. #1
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Reference Dynamic Column in Formula Not Using Relative References.

    What are the alternative options to using relative references?

    I was previously using:
    Please Login or Register  to view this content.
    However this is no good now. I'm using variables and I would like to reference a variable column in the If statement. I can no longer count these columns as they are changing. What I would like to do is refer to the column dynamically as a variable.
    I do not think the IF statement allows variables in it's commands, what would be a good alternative to my requirements?
    If it's been helpful please mark as helpful

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Concatenate the column variable into the formula.


    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Thanks AlphaFrog, I've entered as the following:
    Please Login or Register  to view this content.
    However this is showing as #NAME in the specified column, so I'm not sure if it's recognising the actual variable columns.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    The syntax of your attempt doesn't match either of the examples I gave. Maybe if you just coped one of the examples and just change the variable names to suit.

  5. #5
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Thanks AlpahFrog,

    E.g. 1
    Range("U3:U120").Formula = "=IF(RC" & Col1 & "=""12"", RC" & Col2 & "*0.888, """")"

    This returns #NAME in the cells.

    E.g. 2
    Range("U3:U120").Formula = "=IF(" & Col1 & "3=12, " & Col2 & "3*0.888, """")"

    Returns blank cells, where the forumla should be run and results should be returned. I'm not sure what 3 stands for in the formula, part of me thought it may be the row number but I"m hoping that's not the case. I then altered your formula removing the 3's but that returned blank cells:

    Range("U3:U120").Formula = "=IF(" & Col1 & "=12, " & Col2 & "*0.888, """")"

    Thanks again for your input.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    In your situation, what do Col1 and Col2 equal; a column letter or a column number?

    When you used...
    Range("U3:U120").Formula = "=IF(" & Col1 & "3=12, " & Col2 & "3*0.888, """")"
    ...what was the formula in cell U3 ? Was it the formula you wanted?


    The 3 does represent the first row the formula references. Excel will automatically adjust the formula for each cell below.

    The two versions of codes I gave you in post #2 duplicates exactly the formula that your code in post #1 creates.

  7. #7
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Thanks AlphaFrog, Col1 and Col2 represent Letters which are set as strings.

    When you used...
    Range("U3:U120").Formula = "=IF(" & Col1 & "3=12, " & Col2 & "3*0.888, """")"
    ...what was the formula in cell U3 ? Was it the formula you wanted?

    Post#1 - Range("U3:U120").Formula = "=IF(RC[-17]=""12"", RC[-11]*0.888, """")" References each cell dynamically by relatively referencing the column range U3:U120; to all cells in the column.

    Something must be up as the new formula is not working, I really didn't think one could combine the column as a variable when posting a formula throughout the column range.

    Thanks again for your input, greatly appreciated and I'm obviously not in your league at programming, just trying to provide quality feedback for diagnosis purposes.

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

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Why not just use A1 notation instead of R1C1?

    Please Login or Register  to view this content.
    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


  9. #9
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    As mentioned in Post#1 the columns are always changing therefore A1 notation cannot be used.

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

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    If you are talking about a variable on the worksheet itself, you probably need to use INDIRECT in your formula.

    I'm not sure I fully understand your requirement.

    A sample workbook with an explanation referring to the "variable column" might be useful.

  11. #11
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Quote Originally Posted by Dal123 View Post
    When you used...
    Range("U3:U120").Formula = "=IF(" & Col1 & "3=12, " & Col2 & "3*0.888, """")"
    ...what was the formula in cell U3 ? Was it the formula you wanted?

    Post#1 - Range("U3:U120").Formula = "=IF(RC[-17]=""12"", RC[-11]*0.888, """")" References each cell dynamically by relatively referencing the column range U3:U120; to all cells in the column.
    Answer this to help diagnose the problem.

    What is the value of both Col1 and Col3 ?

    What is the result formula in U3 (even if it's wrong)?

  12. #12
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Quote Originally Posted by AlphaFrog View Post
    Answer this to help diagnose the problem.

    1 - What is the value of both Col1 and Col3 ?

    2 - What is the result formula in U3 (even if it's wrong)?
    Thank you both for your responses;

    1 - Col1's value is "D", Col3 is not used in this formula, Col3 is not used in this formula, I think you mean Col2 where the value is "J".

    2 - As stated in Post#5 results posted for both updates; results are the same with the code advised.

    I do not think the number 3 should be in the formula as this formula is actioned over the column range of rows 3 - 120. When removing the row 3 the formula is at least actioned over the rows 3 - 120 and not just row 3 which is the reason of my Post#3 where my code did not exactly match yours for this specific reason.

    As the formula is being spread over the entire column; Excel will not adjust it to suit each row independantly as the formula was previously referenced relatively to a column.

    TMS I think you're making a good point with Indirect; this is the first I'm learning about Indirect so I'm looking forward to learning about it.
    Last edited by Dal123; 02-26-2017 at 07:54 AM.

  13. #13
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Ok a couple of days researching into Indirect is most interesting and a very powerful function. Watching these tutorials I think you're right TMS, a combination of VLookUp, Indirect and If statement is what I need.

    Is it possible to use all of these in one formula or must I split the computation into different ones?
    Last edited by Dal123; 02-19-2017 at 12:49 PM.

  14. #14
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Could anyone advise as to where to find deep developed info on concatenating in vba?

    I've found https://www.techonthenet.com/excel/formulas/concat2.php but it only delves a little into the vba side. What's really throwing me is doubling up quotations.

    I've read many articles and have been searching for 4 hours, thanks guys.

  15. #15
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Double up quotation marks in VBA works like this...

    In VBA, a normal string begins and ends with a quotation mark like this...
    MyString = "This is a string."

    If you want a quotation mark as part of the string, use two quotation marks for each literal quotation mark you want within the string. That way, VBA knows its a literal quotation and not the beginning or end of the string.

    In vba...
    MyString = "This is a ""string"" with quotes in it."

    The result string would be ...
    This is a "string" with quotes in it.


    Putting a formula in a cell with VBA works the same
    Range("A1").Formula = "=IF(B1="""", """", C1)"

    Result formula in A1...
    =IF(B1="", "", C1)

  16. #16
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Reference Dynamic Column in Formula Not Using Relative References.

    Thanks for all your help and patience with this AlphaFrog, you just helped me grasp concatenating VBA.

    I feel that there's some guides missing on concatenating in VBA; a fundamental skill which I've found tricky to grasp.

+ 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] copy to row and in column with Relative and Absolute Cell References
    By Kamboj in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-18-2014, 02:07 AM
  2. How to count a range relative to a dynamic reference
    By xargon666 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-08-2014, 04:31 PM
  3. Replies: 5
    Last Post: 09-21-2012, 02:08 PM
  4. Replies: 4
    Last Post: 08-01-2012, 01:34 PM
  5. Select range dynamically using relative row and column references
    By gtol in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-01-2012, 06:43 AM
  6. Replies: 2
    Last Post: 05-26-2009, 05:55 PM
  7. Insert a formula with relative cell reference based on a number in a column
    By Dcritelli in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-16-2007, 05:54 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