+ Reply to Thread
Results 1 to 9 of 9

help with VBA formulas R1C1

  1. #1
    Forum Contributor
    Join Date
    09-30-2015
    Location
    LA my baby
    MS-Off Ver
    2013
    Posts
    727

    help with VBA formulas R1C1

    Hello. I created this code to write a formula = A1&" "&A2 but I would like it move to the next column as the loop progresses. I know my code below is wrong. Can someone assist? Thanks.

    Please Login or Register  to view this content.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,194

    Re: help with VBA formulas R1C1

    I have not tested this but if it doesn't work I would be happy to fix it if you attach your file.

    Don't use smart quotes (“ ”) in code. Those won't work in VBA and make the code hard to read here.
    Please Login or Register  to view this content.
    In R1C1 notation, if you omit the number for row/column it means "same row/column that the formula is in".
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,522

    Re: help with VBA formulas R1C1

    First, I would suggest something like this to become familiar with R1C1 notation: https://support.office.com/en-ca/art...es_in_formulas

    Second, I note that the statement that concatenates your formula (Curcell.formula="=R1C1&"" ""&R1C2") is for the .formula property, which uses A1 notation. If you want to use R1C1 notation, you need to use the .formulaR1C1 property. https://msdn.microsoft.com/EN-US/lib.../ff823188.aspx

    I also note in this statement, that you have put the concatenate & operate inside of the quotes, which makes it part of the text string, with no operator to tell VBA to put those strings together. You need to become more familiar with text manipulation and concatenation. I expect it should be something like "R1C1" & """ """ & "R1C2". Because you are trying to include a space (with its accompanying quotation marks in the formula), you will probably need to use multiple quotation marks, and I have never really had to deal with "quotes within text strings" like this, so I may not have got the number of quotation marks right.

    I also note that you are using absolute references in your R1C1 reference. Your A1 "equivalent" is using relative references. Either is syntactically correct, but you need to know which you intend.

    Finally, you are looping through each cell individually and writing the formula in each cell. One of the big advantages of R1C1 notation for this sort of thing is that the formula text string will not change across the row. This means that you can put the formula string together once, then paste the formula string into the entire range in one step.
    Please Login or Register  to view this content.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Forum Contributor
    Join Date
    09-30-2015
    Location
    LA my baby
    MS-Off Ver
    2013
    Posts
    727

    Re: help with VBA formulas R1C1

    @Mr. Jazzer: Thanks. Didn't know that about omitting the number. Good to know. What do you mean by smart code? I thought I was just putting the quotes in quotes.

    @ Mr. Shorty. I'll read that link. Thanks. Why do you have to use formula.R1C1? Mr Jazzer is not doing that. Yes, I mean to use relative references. Mr Jazzer let me know that if you omit the number it turns into a relative reference. I decided to just go with A1 notation. Ah, interesting about the non-changing formula. I remember reading that. Thanks!!

    I ended up just doing this and it worked. I guess the formula moves along with the loop.
    Please Login or Register  to view this content.
    I helped you out with the code tags because you used them in your first post so I assume this is an oversight. But please use them, it's a rule. -6SJ
    Last edited by 6StringJazzer; 12-29-2015 at 04:16 PM.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: help with VBA formulas R1C1

    You shouldn't need to loop.

    If you wanted the same formula, with the same references, in all cells in the row you can use absolute references.
    Please Login or Register  to view this content.
    Last edited by Norie; 12-29-2015 at 02:21 PM.
    If posting code please use code tags, see here.

  6. #6
    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,985

    Re: help with VBA formulas R1C1

    You don't need a loop@

    Please Login or Register  to view this content.

    Regards, TMS
    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


  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,522

    Re: help with VBA formulas R1C1

    @ Mr. Shorty. I'll read that link. Thanks. Why do you have to use formula.R1C1? Mr Jazzer is not doing that.
    I stand (somewhat) corrected. I did not know, nor have I seen any documentation that describes the .formula property automatically "reading" R1C1 notation, but it appears to "work". I would still be cautious using R1C1 notation with the .formula property, because of the potential for ambiguity. Without testing, how does the .formula property interpret "=RC1 & RC2"? In R1C1 notation, RC1 refers to this row column 1. In A1 notation, RC1 refers to column RC row 1. It may work, but I think there is value in making sure that code is "unambiguous", so that Excel/VBA cannot misinterpret something.

    All of which is kind of moot, since it sounds like you have decided to go with A1 notation anyway.
    Last edited by MrShorty; 12-29-2015 at 02:47 PM.

  8. #8
    Forum Contributor
    Join Date
    09-30-2015
    Location
    LA my baby
    MS-Off Ver
    2013
    Posts
    727

    Re: help with VBA formulas R1C1

    @ TMS and Norie. Thanks. I'll use both of your approaches (although I don't want the formula to be fixed Norie but the concept is still the same). Dankeshun.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,194

    Re: help with VBA formulas R1C1

    I'm glad you seem to have solved your problem. There are a lot of loose ends.

    Quote Originally Posted by stephme55 View Post
    What do you mean by smart code? I thought I was just putting the quotes in quotes.
    Here is what I meant by the "smart quotes". That is what Microsoft Word calls them when they curl in. Those are not legal in VBA, you have to use the plain double quotes. I can't copy your code and paste it into VBA without changing the quotes. Your original code:
    Please Login or Register  to view this content.
    Should be
    Please Login or Register  to view this content.
    Quote Originally Posted by MrShorty View Post
    I expect it should be something like "R1C1" & """ """ & "R1C2". Because you are trying to include a space (with its accompanying quotation marks in the formula), you will probably need to use multiple quotation marks, and I have never really had to deal with "quotes within text strings" like this, so I may not have got the number of quotation marks right.
    The original was correct, because he wants the & to be inside the quotes as part of the formula.

    Quote Originally Posted by stephme55 View Post
    I ended up just doing this and it worked. I guess the formula moves along with the loop.
    Please Login or Register  to view this content.
    If this is what you want then it's very unclear what you were looking for in your original question. You said "I would like it move to the next column as the loop progresses." Didn't you mean that you wanted the formula to from referencing column A, to B, to C, etc.? The solution you give here just puts exactly the same formula everywhere.


    Quote Originally Posted by MrShorty View Post
    I did not know, nor have I seen any documentation that describes the .formula property automatically "reading" R1C1 notation, but it appears to "work".
    Actually I overlooked this and should have used .FormulaR1C1. I am surprised this worked, and even if it does I agree with MrShorty that it is not a good practice.
    Last edited by 6StringJazzer; 12-30-2015 at 10:20 AM.

+ 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] one question about Modules in the editor and one about R1C1 formulas
    By sailingbikeruk in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-22-2015, 05:04 AM
  2. Multiple Loops, R1C1 & Array Formulas with VBA
    By 5150 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-02-2014, 02:18 PM
  3. [SOLVED] Convert A1 formulas to R1C1 Formula Array
    By Wheelie686 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-28-2013, 07:38 AM
  4. Updating formulas using R1C1 syntax
    By lfeder in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-11-2013, 04:06 PM
  5. Fixing cells in R1C1 formulas in VBA Query
    By Jessica.Bush in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-25-2011, 11:58 AM
  6. Evaluate function with R1C1 formulas
    By eiem in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-16-2010, 05:45 AM
  7. R1C1 Formulas
    By thundermocos in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-17-2007, 03:21 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