+ Reply to Thread
Results 1 to 19 of 19

writing an AND formula in VBA

  1. #1
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    writing an AND formula in VBA

    what would the code look like to link all values in column S starting with S2 with all values in R starting in R2 and dump them consecutively into column AF starting with AF2? i know i could write a non-VBA formula in cell AF2 of =S2&R2, but the sheet this is going into clears out the previous data every time a main macro is run and i don't feel like typing the formula in new every time. i tried to record the code to see what it would look like and then manually write it into my 'mother macro' but it didn't work.
    Last edited by juniperjacobs; 01-25-2011 at 10:28 AM. Reason: Solved!

  2. #2
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: writing an AND formula in VBA

    i figured this out, but now i need it to read column R, starting with R2, and anytime the value is greater than 1, link R&N
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: writing an AND formula in VBA

    ok, now i've got some loop involved, but it says it's not supported:
    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,604

    Re: writing an AND formula in VBA

    not tested:

    Please Login or Register  to view this content.
    Last edited by protonLeah; 01-22-2011 at 11:18 PM. Reason: corrected offset parm to column vs row
    Ben Van Johnson

  5. #5
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: writing an AND formula in VBA

    can i ask you why the formula need to be so long? i think i have the right idea in post #3, it's just giving me an error after it puts the formula into the first cell.

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,604

    Re: writing an AND formula in VBA

    Your do loop should look something like this:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: writing an AND formula in VBA

    still not sure i get it, firstly, i lied about the cells that need to be linked, it should be: =r2&n2 in column Af2 and so on until there's nothing left in column R.

  8. #8
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: writing an AND formula in VBA

    and it looks like this vba requires that you be in an active cell before you run ,no?

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: writing an AND formula in VBA

    Please Login or Register  to view this content.



  10. #10
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: writing an AND formula in VBA

    Snb,
    why is the range only to 2000? my data will be going a lot longer than that, it will be going until there's nothing left in column R, also, please read full thread b/c the columns changed, and i also think i was pretty close in post 3

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: writing an AND formula in VBA

    Feel free to test the code and adjust it to your needs.
    e.g.
    Please Login or Register  to view this content.
    Please do not judge a suggestion you didn't test.

  12. #12
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: writing an AND formula in VBA

    but i'm trying to understand why you quantified the range? if anything shouldn't it go to the end of the sheet?

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: writing an AND formula in VBA

    I quanified the range to let function the code.
    When using 'evaluate' in bracketform [...] you have to quantify. You can take the range very large as a safety margin, because if nothing is in column R the result will be an empty cell: "".
    You asked for a dense code, this is one.

  14. #14
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: writing an AND formula in VBA

    the code works, but i'm wondering why a loop wouldn't be betteR?

  15. #15
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: writing an AND formula in VBA

    Sometimes it's a matter of taste, sometimes it's a matter of speed:

    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: writing an AND formula in VBA

    do you think your code in post 11 would be faster, it seemed to take a second when i ran it, and i ran it on a pretty small range?

  17. #17
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: writing an AND formula in VBA

    You are the one to tell....

    What's your expected number of rows ?

    I think the fastest method is:
    (if A1 is not empty)

    Please Login or Register  to view this content.
    Last edited by snb; 01-23-2011 at 04:30 PM.

  18. #18
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: writing an AND formula in VBA

    Quote Originally Posted by juniperjacobs View Post
    but i'm trying to understand why you quantified the range?
    You should resist taking the codes we suggest at face value and attempt to use them as is or post back here instead of diving in and pulling it apart for yourself. We expect you to analyze them, understand them, then change them to suit your needs.



    ...if anything shouldn't it go to the end of the sheet?
    I would suggest that this is usually a bad idea. Putting formula through an entire sheet for the convenience of it probably asking for problems at some point.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  19. #19
    Registered User
    Join Date
    08-19-2010
    Location
    new york
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: writing an AND formula in VBA

    i wanted help figuring out a looping code that was saying the object was mismatched

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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