+ Reply to Thread
Results 1 to 10 of 10

How to create a loop for recorded macro?

  1. #1
    Registered User
    Join Date
    12-29-2012
    Location
    Baku
    MS-Off Ver
    Excel 2010
    Posts
    10

    How to create a loop for recorded macro?

    Dear All, I am a beginner in VBA and trying to learn it with real examples. One of the current examples I am working with is to re-arrange the table by adding some string information to it. What I have done is tried to record it and to put some looping but unsuccessful. I attach spreadsheet into this thread and would highly appreciate if anyone gives hint on this. Many thanks!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: How to create a loop for recorded macro?

    Fist of all, i don't see any VBA code in your file.

    Second you don't show us the desired result (so i don't know what you're up to).

    so i take a guess. I made this with Data => sort => sort on column 2 and after that sort on column 3.

    see the attached file.

    please respond if this is what you're up to.
    Attached Files Attached Files
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Registered User
    Join Date
    12-29-2012
    Location
    Baku
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: How to create a loop for recorded macro?

    Many thanks for your answer. You are right I did not attache the right file. Here is the second attempt. I also put recorded macro and For Next loop in it. As you see it creates format not for each rows as I want but to the rows paralel to the active cell. Thus this loop does not include every row value in the new format. Please advise what could be done here.

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

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: How to create a loop for recorded macro?

    Problaby I can't help you enough with your VBA problem.

    Never the less in your 2nd responce you don't add the excel file (so we can test the code).

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to create a loop for recorded macro?

    Hi, Partisano,

    maybe give this code a try:
    Please Login or Register  to view this content.
    You may turn off Application.ScreenUpdating for speed reasons if wanted.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  6. #6
    Registered User
    Join Date
    12-29-2012
    Location
    Baku
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: How to create a loop for recorded macro?

    HaHobe many thanks for response! The code you suggest gives same results. If you look at the results you will see the step is different, so that it gives answer not for each row. For example it changes the data for the range of B2:G2 and instead of using B3:G3, it goes and uses B9:G9. Issue is with step. Do you have any Idea how I can resolve this issue? Many thanks !

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to create a loop for recorded macro?

    Hi, Partisano,

    the really great thing about attachments is that the person who uploads the file can describe the situation as is and the wanted result. I believed you wanted a code that would deliver the design you had put in. If thatīs not what you want maybe you can describe what the result should look like: is it every single line in Range B:G broken up like the example or a different step for getting the data from B:G to be displayed in Range L:R?

    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    12-29-2012
    Location
    Baku
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: How to create a loop for recorded macro?

    Hi Holger,

    You are very helpful and I believe at this early stage of my learning I am progressing due to great ideas of forum users, such as yourself! :-) You're absolutely right, the way I described it is very limited and you are absolutely right again, I would like to get same format from each single row on the left (B:G Range) to the left side (like example). And both codes, the one you sent and the one I recorded are not giving that result, for some reason it prints out only 9 steps in B:G rows. I hope I managed to describe the problem!

    Ciao,
    Farid

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: How to create a loop for recorded macro?

    Hi, Partisano,

    I used two variables for that: lngRow loops through all the rows in Range B (thatīs Step 1), lngTarg is the Row starting to reflect the data (that would be Step 6):
    Please Login or Register  to view this content.
    This is just a quick way for a possible solution.

    Ciao,
    Holger

  10. #10
    Registered User
    Join Date
    12-29-2012
    Location
    Baku
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: How to create a loop for recorded macro?

    This is exactly what I expected! Thank you so much!

+ 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