+ Reply to Thread
Results 1 to 12 of 12

Using counter variable as cell reference

  1. #1
    Forum Contributor
    Join Date
    09-23-2008
    Location
    Rhode Island
    MS-Off Ver
    Excel for Mac 2011 and 2007 on PC
    Posts
    265

    Using counter variable as cell reference

    I'm in the macros dark ages because I can't get any of my VBE Macros to work. I guess I could use a serious tutorial is anyone has pointers.

    MEantime, I open old macro sheets that were constructed by placing programs in a column of cells in a macro worksheet that I learned in Excel 2.x

    The syntax is slight different and the VBE style doesn't use the RETURN() funtion to end the macro anymore, I believe, but otherwise this macro that I'm having trouble with will, I hope, be recognizable by the assembled wisdom.

    I have a list of records that I will ultimately apply a pivot table command to thanks to help I received here. But first, the data that came in without delimiters had text of varying and unpredictable length and the fixed width import turned it into several fields based on no deciherable algorithm I can find. So I am manually reformatting these entries and filling down. There are between 200 and 600 consecutive entries that are identical in the text fields. At first I thought that each ran for the same number of entries so I wrote a macro that just jumped 607 lines, but for varying reasons, some of the groupings do not have the full 607 entries.

    So I'm trying to restructure the macro so if I'm at the first occurence of a particular text value I can have the macro run through succeeding rows in that column until it finds a value that is not equal to the original and then jump there. By using a split window and running the macro on the bottom window this gives me the opportunity to easily select my fill down range instead of scrolling and trying to catch the change in text as it flies by my eye.

    My proposed code looks like this:

    =FOR("counter",1,607,1)
    =IF(SELECTION()="R["counter"]C",NEXT(),SELECT("R["counter"]C")
    =RETURN()

    My problem is that I can't seem to get the quote marks right to call the variable and EXCEL doesn't like the formula I've written in line 2 of the code (unoriginally I'm using "counter" which is the sample variable excel uses in tutorials on FOR() NEXT() loops.) My notion is to start "counter" at 1 so the first time through the IF function compares the SELECTION to R[1]C. If they are equal, the true value is NEXT(). Counter is incremented to 2, so the IF function compares the SELECTION to R[2]C and so forth, until it encounters a different value. Then the False value is to SELECT that cell using the counter variable. Of course this approach would actually select not the last same value, but the 1st of a different value in that column. This could be accounted for by making the selection R["counter"-1]C, but the point is that it will put the lower screen view right in the vicinity of where I need to be and then I can take it quickly from there.

    It may be that there is an easier or more logical way to do this by loading the value in the current selection and using a FIND.COLUMN that looks for the not equal, <> value.

    So, I'm certainly open to other ways up the mountain. For now I'm going to be pasting them into an old previously named macro in an old macro sheet because I just cannot get the VBE macro thing to work for me. I would be glad to read tutorial on that but also trying to forge forward on this project so I can finish it for the end of the day.

    Brian
    Last edited by riwiseuse; 10-19-2010 at 11:26 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Using counter variable as cell reference

    Hi Brian,
    I can't tell what program you are using - you should update your profile so we can tell.

    In Excel VBA the way to run through a bunch of cellis is - here is an example
    Please Login or Register  to view this content.
    I hope this shows you what you need.

  3. #3
    Forum Contributor
    Join Date
    09-23-2008
    Location
    Rhode Island
    MS-Off Ver
    Excel for Mac 2011 and 2007 on PC
    Posts
    265

    Re: Using counter variable as cell reference

    Quote Originally Posted by MarvinP View Post
    Hi Brian,
    I can't tell what program you are using - you should update your profile so we can tell.
    MArvin. sorry about that. i'll try to get it better up to speed. I run both 2004 and 2008 on a Mac but all of programming experience other than direct cell programming dates to Excel 2.2 for the Mac.

    The Virtual Basic stuff is like greek to me although alot of the functions cross.

    But assuming I can divine some meaning, my goal here is to find the next cell down the column from my current selection that does NOT match the content of the current selection.

    I cannot seem to get any of these macros to run by pasting this stuff in virtual basic.I always seem to get errors or off with the clock running. the only macros I've been able to get to work are the old macro sheets I still have around from Excel 2.2.

    the later versions still recognize these as macros and carries out the functions in them so I assume that between the Macro command and the VBE you get more or less a similar compiled macro. I just do not seem to be able to carry it off in the VBE. If there is a good tutorial, i'm glad to start from scratch although I still have luck rewriting these old macros to perform current selection tasks.

    Forgetting what platform I'm using for writing, just taking the code you offered, what do I have to do to substitute the content of the currently selected sell for the test string "Is this It?".

    Then the next problem is that I don't know how many cells I want to run through looking. you gave for RowCtr 1-100 for instance. Rather I want to keep looking (by column, i.e. running down the column of the selection) for the first cell that doesn't match.

    And I'm only really trying to count rows because the info I'm matching is all in one column. It looks as if you have offered a generic array searcher for a two dimensional application, which is helpful to my understanding but I assume I could do without a ColCtr.

    Then once I find the first cell in the column that Doesn't match, I want to select it. So I assume I need some kind of language that can turn RowCtr into a relative reference, e.g.

    SELECT(R["RowCtr"]C]

    although that is old syntax as it seems Virtual basic doesn't use the capitalized functions. Don't know if the variable name needs quotes but obviously I'm trying to call the numerical value of the variable as a relative row reference

    Finally, what does DIM RowCtr As Double do?

    Thanks for dropping in. Hope I was clear enough in trying to integrate your suggestions and the questions I have about them.

    Brian

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Using counter variable as cell reference

    Hi Brian,
    As soon as I saw "Mac" I knew my answers were wrong. I'm on a Microsoft Windows machine, running Office for the PC, not Mac. I think Microsoft didn't do Visual Basic for Applications (VBA) for the Mac Office versions in 2007. I do believe they will put it back in for the 2010 verson of Mac Office. See:
    http://www.informationweek.com/news/...leID=208803359
    When VBA behind Mac Office 2010 appears you should get a lot closer to having our PC code working.

    I think you should wait for the Mac 2010 Office with Excel before you start your learning.

    Sorry for the confusion. You need to back into your profle and tell us what version of Excel you are using. Look at other posts and it will show. Your profile now says Rhode Island but not version of Excel.

    Sorry - I guess I can't help.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,247

    Re: Using counter variable as cell reference

    There are probably better ways, but:
    Please Login or Register  to view this content.
    should work, I think.
    Remember what the dormouse said
    Feed your head

  6. #6
    Forum Contributor
    Join Date
    09-23-2008
    Location
    Rhode Island
    MS-Off Ver
    Excel for Mac 2011 and 2007 on PC
    Posts
    265

    Re: Using counter variable as cell reference

    Quote Originally Posted by romperstomper View Post
    There are probably better ways, but:
    Please Login or Register  to view this content.
    should work, I think.
    Brilliant. No such thing as a better way, and glad to find someone who can give me cell code until I get VB friendly.

    Works like a charm and my apologies, I mentioned in the new thread that I left this one but I should have come back and posted the link. I really appreciate the follow through and was unsure of politenesses where I felt that my question had changed. While one can change the title of their response to a thread, I was trying to think of how to help better title the thread for the question I had. So my apologies for spreading this out when you were just about to bring it together.

    I thought maybe ACTIVE.CELL would work but did not know about the OFFSET function. otherwise I would have still need and R[1]C[1] reference in onehalf of the IF formula.

    In my text on macro language which is Excel 2.2, there is no END.IF and the IF function is entered with all its arguments in a single cell as you might find in a worksheet nowadays.

    But your columnar form works fine, I pasted it in. Redefined the length of the macro using the INSERT>NAME>DEFINE command. And it was off to the races.

    In terms of understanding how it works, I'm missing one piece of logic as to which of the cells after the IF in this kind of construction is the true or false result, or if the IF/END.IF just continues until it gets a true result.

    The relevant code lines are

    Please Login or Register  to view this content.
    So the first cell is the test argument with no results arguments. It is looking for a mismatch between the selected cell and that offset below it by the counter variable of the loop. My confusion is that I would have assumed the 1st macro cell down would be the true value. That is the cell whose function is to move the active cell selection down the sheet. But that movement ceases when the test argument is true, so it seems that my assumption that this cell is the true value and BREAK function is the false value is not a correcting reading of this IF/END.IF approach.

    So maybe the way this works from my guesswork approach is that a True result for the argument makes the macro jumot to the END.IF, and the False value causes it just to contineu through SELECTing the next row in that column and then the BREAK function causes it to jump to the NEXT() command and the loop repeats.

    The problem with that anaylsis is, how does the true result leading to END.IF get by the NEXT command following it so that the loop stops?

    Brian

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,247

    Re: Using counter variable as cell reference

    The syntax is basically:
    =IF()
    ' stuff if true
    =ELSE() ' optional
    ' stuff to do if False
    =END.IF()

    the BREAK()
    tells it to jump out of the loop once the TRUE part is reached. (Since we aren't really interested in doing anything if the condition is not met, there is no ELSE part in this example)

  8. #8
    Forum Contributor
    Join Date
    09-23-2008
    Location
    Rhode Island
    MS-Off Ver
    Excel for Mac 2011 and 2007 on PC
    Posts
    265

    Re: Using counter variable as cell reference

    Quote Originally Posted by romperstomper View Post
    the BREAK()
    tells it to jump out of the loop once the TRUE part is reached. (Since we aren't really interested in doing anything if the condition is not met, there is no ELSE part in this example)
    R,

    Got it. So the BREAK() Function jumps out of the FOR/NEXT loop, Not just out of the IF. That is what I missed.

    BTW,

    is my cultural clock as retro as my coding or is that handle a reference to Romper Room.

    Brian

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,247

    Re: Using counter variable as cell reference

    RomperStomper is actually a film (my avatar used to be a pic from the film, which was more obvious. Or so I thought, though some people seemed to think I really was a tattooed skinhead in a vest... )

  10. #10
    Forum Contributor
    Join Date
    09-23-2008
    Location
    Rhode Island
    MS-Off Ver
    Excel for Mac 2011 and 2007 on PC
    Posts
    265

    Re: Using counter variable as cell reference

    Quote Originally Posted by romperstomper View Post
    There are probably better ways, but:
    Please Login or Register  to view this content.
    should work, I think.
    RS -
    I know I marked this solved, but I noticed one thing I forgot to ask. If the True Value is in the code lines between the IF and END.IF (because ELSE is omitted it is only true values), what are there still ltrue and false values in the function line for the IF?

    Could you omit those, and did you put them in for the purpose of evaluating that line of code and the state of counter at that point while debugging, but they otherwise do not affect the actual operation of the whole macro?

    Thanks,

    Brian.

    PS - guess your new Avatar is a little more laid back (unless that kid has a tattoo somewhere)

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,247

    Re: Using counter variable as cell reference

    There aren't true or false parts in the IF function.

  12. #12
    Forum Contributor
    Join Date
    09-23-2008
    Location
    Rhode Island
    MS-Off Ver
    Excel for Mac 2011 and 2007 on PC
    Posts
    265

    Re: Using counter variable as cell reference

    Quote Originally Posted by romperstomper View Post
    There aren't true or false parts in the IF function.
    My bad, those are the arguments for OFFSET.

    Brian

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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