+ Reply to Thread
Results 1 to 15 of 15

Macro search for number with variable steps

  1. #1
    Registered User
    Join Date
    04-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2000
    Posts
    8

    Macro search for number with variable steps

    I have an excel spreadsheet that I am trying to format. Each time the spreadsheet is of the same format but with differing amount of numbers. I.e. sometimes it will be a set of 3, the next time might be 6.
    I want to write a macro that will find a set piece of text, move it offset(-1,2) and then find the next one.
    For example....

    | Column |
    |MF1 |
    |MF2 |
    . |
    . |
    . |
    |MFn |

    The text to search for is MF1, then MF2 until it does not find it.

    So I thought it out something like (in non VB code!):
    Find MF1, Select
    Selection.Cut
    ActiveCell.Offset(-1, 2).Select
    ActiveSheet.Paste
    Find MF2, Select
    etc.
    Until Can't find next MFn.

    Cheers in advance.
    Cei
    Last edited by mudraker; 04-17-2009 at 07:56 AM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macro search for number with variable steps

    Is there only one instance of MF1, MF2 etc?

    Will it always be sequential - i.e. MF1, MF2, MF3 or could you have MF2, MF4, MF7?

  3. #3
    Registered User
    Join Date
    04-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2000
    Posts
    8

    Re: Macro search for number with variable steps

    There is only one instance of MF1, MF2 etc and they will always be in sequential order. There are numbers elsewhere in the sheet though .

    Thanks

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macro search for number with variable steps

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

  5. #5
    Registered User
    Join Date
    04-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2000
    Posts
    8

    Re: Macro search for number with variable steps

    Hey Stephen,

    That doesn't seem to do anything, it returns an error when I step through on
    SearchFormat:=False - I am running excel 2000 is that why?
    When I removed that from the macro it runs ok but it just twice through the loop and ends without finding anything when my spreadsheet in this case had MF1, MF2.

    Just to add to the problem, I forgot to mention that it is likely that there might be more text after the MFn. Does this change the code dramatically or can I put the string after the i in "".

    Cheers for the help so far.
    Cei

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macro search for number with variable steps

    Cei - please attach a small sample workbook showing your data as is.

  7. #7
    Registered User
    Join Date
    04-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2000
    Posts
    8

    Re: Macro search for number with variable steps

    Stephen,

    I have attached a sample of what of I am trying to do. The idea is to basically move the text from rows into columns. I have already written the macro to add the headings (responsibilities), that was pretty simple. But so I need to move the MF1 Pete - to the same row as MF1 Dave - under the Pete responsibilities column. The column and cell numbers won't always be the same, but it will always be MF1 Pete -, MF1 Dave - and the movement will always be across to the right 2 cells and up 1 cell.

    Hope that is enough information, if you need anymore then let me know.
    Cheers
    Cei
    Attached Files Attached Files

  8. #8
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macro search for number with variable steps

    OK, this isn't quite as I envisaged from your post. Could you add to your attachment how you want things to end up as I'm not quite sure?

  9. #9
    Registered User
    Join Date
    04-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2000
    Posts
    8

    Re: Macro search for number with variable steps

    Sorry for the confusion! Here's the proposed end result, though the moving of the MF is not important (just to show you which cell is moving).

    Cheers
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macro search for number with variable steps

    This works for your example but not sure it will cater for all eventualities.
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    04-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2000
    Posts
    8

    Re: Macro search for number with variable steps

    Hey Stephen, that code cannot work on my version of excel. I am running excel 2000 and the argument search format.
    is not supported in this version. Is there a way around this (bar getting an updated version of Excel!)?
    Thanks for the help
    Cei
    Last edited by ceiussandicus; 04-09-2009 at 08:22 AM. Reason: mistake in trying to enter code!

  12. #12
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macro search for number with variable steps

    Can you just remove that bit? If it doesn't work check you haven't any predefined formats by pressing Ctrl+F from the worksheet and clearing formats.

  13. #13
    Registered User
    Join Date
    04-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2000
    Posts
    8

    Re: Macro search for number with variable steps

    No that doesn't seem to work, when I remove the argument it just seems to loop through twice and then end, without actually moving anything. So the code I have ended up with is below, all I have done is remove argument that fails.
    I have tried to remove formatting. If I press Ctrl+F in the worksheet it brings up the find dialog box.

    Any more ideas?

    Please Login or Register  to view this content.
    Cheers
    Cei

  14. #14
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macro search for number with variable steps

    I'm sort of stumped actually. Are you running it on the example you posted? If a different file it might be a problem with the sheet reference. Or another issue with Excel 2000.

  15. #15
    Registered User
    Join Date
    04-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2000
    Posts
    8

    Re: Macro search for number with variable steps

    Ok, not quite sure why it didn't work. Tried it on an excel 2007 version both with the argument and without and it worked. So tried it on the spreadsheet again and it worked. The reason it wasn't working seems to be that there was another macro in the worksheet, not that I was running it. Maybe that isn't the reason...

    Anyway it is working now, so that is all that matters.
    Thanks so much for the help, sorry its taken so long!

    Cheers
    Cei

+ 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