+ Reply to Thread
Results 1 to 15 of 15

Macro Looping Inexperience :-(

  1. #1
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Macro Looping Inexperience :-(

    Hi all,

    Thanks for taking the time to read this.

    I am trying to solve this problem on a macro I have been working on and getting nowhere.

    The Macro is below:

    Please Login or Register  to view this content.
    The 'Store Variables' section runs through a worksheet storing the contents of cell (i, 1) as variables PAYNUM(i). To my eyes this works okay.

    The 'Compare Variables' section is meant to run through the range (j, 1) from j = 6 to 1000 and do the following:
    1. Compare the contents of cell (j, 1) to variable PAYNUM(i)
    2. Run through the cells (j, 1) until either the PAYNUM(i) variables is matched OR (j, 1) hits a empty cell - if this is true it inputs PAYNUM(i) into that empty cell.
    3. Repeat steps 1 - 2 for each PAYNUM(i) variable.


    Can someone help me progress in the right direction? If the above is confusing basically just the process for:
    1. Storing variables in a range of cells on one sheet
    2. Comparing variables in a range of cells on another sheet and either:
    - If variable is matched go to next variable
    - If variable is not matched (i.e. runs through the list into a blank cell) input that variable and go to next variable.


    Any help will be greatly appreciated!


    Coeus.
    Last edited by Leith Ross; 07-11-2013 at 07:36 PM. Reason: Added Code Tags

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

    Re: Macro Looping Inexperience :-(

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 07-11-2013 at 07:54 PM.
    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
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Macro Looping Inexperience :-(

    Hi AlphaFrog!

    Many many thanks for your reply - I will be putting it into use today!

    Would you be willing to add commentary to explain each line of code not explained?

    There are some terms I am not used to using / understand what it does. If you could explain? I will research today but thought it would be easier to understand linked to this example:

    - i As Long

    - .Range

    - End(xlUp)

    - UBound(PAYNUM, 1)

    - IsError(Application.Match(PAYNUM(i, 1), .Columns(2), 0))


    As you might have guessed from my previous examples I am only really used to looping through cells using 'For i = 1 to 1000 ... Next i' so the way this has been done is quite foreign to me


    Again many thanks for your help - greatly appreciated!


    Coeus

  4. #4
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Macro Looping Inexperience :-(

    Hi again,

    I have researched a bit but any input youd be willing to input would be great still!

    I have expanded on the original macro to run through 12 worksheets to match / input the PAYNUM from.

    I get a 'Type Mismatch' error though when trying to get it to loop?

    Could someone have a look and advise - it would be greatly appreciated!


    Sub REFRESH_DATA()


    'Defines PAYNUM as the variable for payroll numbers.
    Dim PAYNUM As Variant, i As Long

    'Turns off screen updating.
    Application.ScreenUpdating = False


    For j = 1 To 12
    '****************************** STORE VARIABLES ******************************

    'Sources sheet to store variables from.
    With Sheets("Data - Month " & j)
    'Stores each value in column A as a variable.
    PAYNUM = .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Value

    End With


    '****************************** COMPARE VARIABLES *****************************


    'Sources sheet to compare to.
    With Sheets("Summary")

    'Loop through each PAYNUM variable.
    For i = 1 To UBound(PAYNUM, 1)

    'Tests if the PAYNUM variable matches any value in column B.
    If IsError(Application.Match(PAYNUM(i, 1), .Columns(2), 0)) Then

    'If no match to PAYNUM variable is found adds value to the first empty cell in column B.
    .Range("B" & Rows.Count).End(xlUp).Offset(1).Value = PAYNUM(i, 1)

    End If

    Next i

    End With

    Next j

    '****************************** CLOSE *****************************


    'Turns on screen updating.
    Application.ScreenUpdating = True


    End Sub

  5. #5
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro Looping Inexperience :-(

    I don't think there is anything wrong with the code so I think it could be an issue with your data. You might need to add some error handling. For example, UBound(PAYNUM,1) will return a type mismatch error when PAYNUM is not an array, so if column A has only one cell wth data, or has no data at all this will return the error you describe. Try this:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Macro Looping Inexperience :-(

    Hi again all!

    Thanks for the help and feedback to date really appreciate it! Learning so much!

    I am a bit stuck with a slight modification to the macro code - basically instead of pasting a single cell as PAYNUM I would like it to paste a range of cells whilst still comparing the first cell?

    Can someone highlight what I'm doing wrong?



    Sub REFRESH_DATA()


    'Defines PAYNUM as the variable for payroll numbers.
    Dim PAYNUM As Variant, i As Long

    'Turns off screen updating.
    Application.ScreenUpdating = False


    For j = 1 To 12
    '****************************** STORE VARIABLES ******************************

    'Sources sheet to store variables from.
    With Sheets("Data - Month " & j)
    'Stores each value in column A as a variable.
    PAYNUM = .Range("A1", .Range("A:D" & Rows.Count).End(xlUp)).Value

    End With


    '****************************** COMPARE VARIABLES *****************************


    'Sources sheet to compare to.
    With Sheets("Summary")
    If IsArray(PAUNUM) = True Then
    'Loop through each PAYNUM variable.
    For i = 1 To UBound(PAYNUM)

    'Tests if the PAYNUM variable matches any value in column B.
    If IsError(Application.Match(PAYNUM(i, 1), .Columns(2), 0)) Then

    'If no match to PAYNUM variable is found adds value to the first empty cell in column B.
    .Range("B" & Rows.Count).End(xlUp).Offset(1).Value = PAYNUM(i, 1)

    End If

    Next i
    End If
    End With

    Next j

    '****************************** CLOSE *****************************


    'Turns on screen updating.
    Application.ScreenUpdating = True


    End Sub

  7. #7
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Macro Looping Inexperience :-(

    Hi all!

    Can anyone help me on the above - been fiddling and researching cannot get the thing to work! :-(


    Coeus.

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

    Re: Macro Looping Inexperience :-(

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Macro Looping Inexperience :-(

    Thanks AlphaFrog!


    I will try it out today and let you know how I get on.

    Then I need to start pouring over it and understand the code!


    Coeus.

  10. #10
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Macro Looping Inexperience :-(

    Hi AlphaFrog!


    I've just tried that code and no data is now pulling in compared to my original :-(


    Is it because of the values it is trying to match the loop on is now a range?


    Would you mind taking another look?


    Regards,

    Coeus.

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

    Re: Macro Looping Inexperience :-(

    Quote Originally Posted by Coeus View Post
    no data is now pulling in compared to my original
    Not sure what that means, but it worked for me. Can you explain exactly what you tried and exactly what happened when you tried it? Did you copy all the new code and replace all the original code? Post an example workbook with your code.

  12. #12
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Macro Looping Inexperience :-(

    Hi AlphaFrog,

    I have attached a copy of my worksheet as it stands.

    My code is as follows:

    Please Login or Register  to view this content.

    Many many thanks for your continued support AlphaFrog! I greatly appreciate it :D


    Coeus.
    Attached Files Attached Files
    Last edited by Coeus; 07-18-2013 at 03:20 PM.

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

    Re: Macro Looping Inexperience :-(

    There is a typo in the code.

    Change this...
    If IsArray(PAUNUM) = True Then

    to this...
    If IsArray(PAYNUM) = True Then

  14. #14
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Macro Looping Inexperience :-(

    AlphaFrog has a keen eye.

    Many many thanks for your help with this project - it has made a task very efficient. Promise you I am going to learn every line of this code and understand it completely!

    My very last request. I tried reversing the 'For J = 1 to 12' to 'For J = 12 to 1'. It didn't work and the 12th sheet will contain the most recent data for a code, if not the 11th, if not the 10th etc. Is there a way to set this?

    I think it may be to do with the 'With Sheets("Data - Month " & j)' line?


    Hope you've had a good day!


    Coeus.

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

    Re: Macro Looping Inexperience :-(

    You're welcome.

    Try this...
    For J = 12 to 1 Step -1

    For syntax help on any function, in the VBA Editor highlight the function keyword (For in this case) and press F1

    I don't have an exceptionally keen eye. I debugged the code using program breaks and discovered the For i -Next loop wasn't executing because of the If IsArray(PAUNUM) line.
    Last edited by AlphaFrog; 07-18-2013 at 04:38 PM.

+ 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