+ Reply to Thread
Results 1 to 7 of 7

VBA Code isn't using the entire data set

  1. #1
    Registered User
    Join Date
    01-21-2022
    Location
    Dublin, Ireland
    MS-Off Ver
    16.0
    Posts
    11

    VBA Code isn't using the entire data set

    hi, this code is working to move any item in the data set with "A380-800" in column 2 to a new sheet however the entirety of column 2 isn't being captured. I have to run the code 3 or 4 times before all of the rows with "A380-800" in column 2 are moved to the new sheet. The data set is quite large at up to 800 or 900 rows.

    I used the following piece of code:

    Sub Button2_Click()
    Application.ScreenUpdating = False
    For Each myCell In Sheet1.Columns(2).Cells
    If myCell.Value = "A380-800" Then
    myCell.EntireRow.Copy Worksheets("A380-800s").Range("A" & Rows.Count).End(3)(2)
    myCell.EntireRow.Delete
    End If
    Next

    End Sub
    Last edited by ldaly4; 08-11-2022 at 05:56 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: VBA Code isn't using the entire data set

    When you are deleting rows, you need to work from the bottom up, otherwise you will skip rows. So, rather than use For Each, you should use a row counter and use something like For i = lLR To 2 Step -1 and refer to Range("B" & i)
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: VBA Code isn't using the entire data set

    I'm guessing it "skips" the ones where A380-800 appears twice in a row because you're deleting the row and then it skips over that one. Try like this instead:

    Please Login or Register  to view this content.
    WBD
    Office 365 on Windows 11, looking for rep!

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,220

    Re: VBA Code isn't using the entire data set

    Another solution, without the loop. It should be faster.
    Please Login or Register  to view this content.
    Artik

  5. #5
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: VBA Code isn't using the entire data set

    Hello Idaly,

    It's always a good idea to use the AutoFilter on large data sets rather than a loop construct, so you could refine the use of the Autofilter as follows:-

    Please Login or Register  to view this content.
    I hope that this helps.

    Cheerio,
    vcoolio.

  6. #6
    Registered User
    Join Date
    01-21-2022
    Location
    Dublin, Ireland
    MS-Off Ver
    16.0
    Posts
    11

    Re: VBA Code isn't using the entire data set

    Thank you everyone for the help!

  7. #7
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: VBA Code isn't using the entire data set

    You're welcome Idaly. Glad we could help.

    Cheerio,
    vcoolio.

+ 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. Replies: 1
    Last Post: 05-04-2019, 06:31 PM
  2. VBA code to convert all data + formatting of entire sheet to VBA code
    By jocanon in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-14-2016, 06:56 PM
  3. [SOLVED] Count and Sum Entire Job IF contains certain code
    By CrocHntr in forum Excel General
    Replies: 4
    Last Post: 04-20-2015, 12:49 PM
  4. This code not deleting entire row
    By biznez in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-20-2015, 11:38 AM
  5. Replies: 1
    Last Post: 07-28-2014, 05:18 PM
  6. Code selects entire rows upto one range and same code selects only columns for other.
    By sriharigk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-09-2012, 05:19 AM
  7. Code to select the entire row
    By makku in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-29-2011, 07:02 AM

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