+ Reply to Thread
Results 1 to 9 of 9

How to run VBA code repeatedly?

  1. #1
    Registered User
    Join Date
    03-14-2015
    Location
    London, England
    MS-Off Ver
    14.4.8
    Posts
    4

    Exclamation How to run VBA code repeatedly?

    So, I have written this subroutine code in VBA which basically looks for data (looks for dates - 2004 to 2008) on one worksheet (called Panel Data) , if it finds the said data, it cuts & pastes the whole row onto a new worksheet (called VBA). So, the problem is - I have more than 10,000 data points so I can't keep pressing run 10,000 times and I would like the code to run until it cuts & pastes ALL the data points that match my criteria onto the new worksheet. How do you suggest I do this? What additions should I make to my code so that it runs itself till all the relevant data has been transferred to the VBA worksheet? I have tried a while loop but I don't know where/how exactly should I incorporate it into my code. I could have tried more but this is urgent. Hence my post here.

    Sub Riskcon()
    Dim LR As Long
    Range("A2").EntireRow.Insert Shift:=xlDown
    LR = Sheets("Panel Data").Cells(Rows.Count, "C").End(xlUp).Row
    LR1 = Sheets("VBA").Cells(Rows.Count, "A").End(xlUp).Row + 1
    With Sheets("Panel Data").Range("C2:C" & LR)
    .AutoFilter
    .AutoFilter Field:=1, Criteria1:="2004", _
    Operator:=xlOr, Criteria2:="2005"
    .SpecialCells(xlCellTypeVisible).EntireRow.Copy Destination:=Sheets(" VBA").Range("A" & LR1)
    .SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    End Sub

    Thank you very much!

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: How to run VBA code repeatedly?

    It should copy the complete set of selected rows in one shot, no?
    It will be nice to attach a sample of your data to work with.
    BTW use code tags
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    03-14-2015
    Location
    London, England
    MS-Off Ver
    14.4.8
    Posts
    4

    Re: How to run VBA code repeatedly?

    Quote Originally Posted by PCI View Post
    It should copy the complete set of selected rows in one shot, no?
    It will be nice to attach a sample of your data to work with.
    BY THE WAY use code tags
    First, thank you very much for your prompt reply! I have added code tags for my code below and screenshots of the relevant Excel sheets.

    Please Login or Register  to view this content.
    This is the VBA worksheet. You can see that the code is working perfectly and transferring the data as required. It adds blank lines overtime you run it but that's fine. I'll take it out at the end.
    Screen Shot 2015-03-14 at 11.13.41.png


    This is the Panel Data worksheet. If you zoom in, you can see that column C has certain dates. If a row has any date from 2004 to 2008, I would like to transfer it to the VBA worksheet. I have added only 2004 & 2005 to my code to see if that works fine. If it does, i'll add 2006-2008 as well.
    Screen Shot 2015-03-14 at 11.13.31.png
    Last edited by Sid108; 03-14-2015 at 07:20 AM. Reason: Clarification

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: How to run VBA code repeatedly?

    Sid108,
    Thank you for the information back.
    " It adds blank lines" it's a bit curious as it would copy only cells with values
    Is there still some issues?
    If yes, please attach an Excel file to prepare the macros
    PCI

  5. #5
    Registered User
    Join Date
    03-14-2015
    Location
    London, England
    MS-Off Ver
    14.4.8
    Posts
    4

    Re: How to run VBA code repeatedly?

    Quote Originally Posted by PCI View Post
    Sid108,
    Thank you for the information back.
    " It adds blank lines" it's a bit curious as it would copy only cells with values
    Is there still some issues?
    If yes, please attach an Excel file to prepare the macros
    PCI
    The extra blank lines that it adds isn't much of a problem since I can't afford to spend time to fix that. I would like to make the code to till all the data is transferred to the VBA worksheet. Some looping somewhere - I just don't know how to add it - I don't know how to define the criteria for the while loop. For usual functions, it would be something like do while i < 5 or something like that but since here the condition is 'till any date from 2004 to 2008 is in on the Panel Data worksheet', it's different.

    EDIT : I tried attaching my .xlsm file here but it exceeds the max limit of 1MB
    Last edited by Sid108; 03-14-2015 at 08:42 AM.

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: How to run VBA code repeatedly?

    There is no need for " while loop"
    " I tried attaching my .xlsm file here but it exceeds the max limit of 1MB"
    Delete some rows to fit into the limit

  7. #7
    Registered User
    Join Date
    03-14-2015
    Location
    London, England
    MS-Off Ver
    14.4.8
    Posts
    4

    Re: How to run VBA code repeatedly?

    Quote Originally Posted by PCI View Post
    There is no need for " while loop"
    " I tried attaching my .xlsm file here but it exceeds the max limit of 1MB"
    Delete some rows to fit into the limit

    done. i attached the file.
    Attached Files Attached Files

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to run VBA code repeatedly?

    Hi,

    Seems to me that the fastest way to do this is add a helper column that determines whether that particular row should be copied. Then use the filter technique to first filter the helper column and then use the following macro with the filtered data. If this is a regular event you could also include the Filter coding.

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: How to run VBA code repeatedly?

    My suggestion

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA code to repeatedly copy and paste selection
    By phbryan in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-13-2014, 11:20 PM
  2. VBA Code for running Solver repeatedly
    By n_ant in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-05-2013, 09:21 AM
  3. cut entire row which contain X to the other row which contain Y repeatedly
    By calif in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-12-2013, 11:48 AM
  4. Repeatedly Using 'ShowAllData'
    By JONNY981 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-29-2009, 11:24 AM
  5. Best way to code a repeatedly used array?
    By ajm218 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-28-2008, 03:38 PM

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