+ Reply to Thread
Results 1 to 7 of 7

Code to loop through input sheet and whenever a cell matches copy to a new sheet

Hybrid View

wolfdemon Code to loop through input... 06-17-2021, 06:14 PM
TMS Re: Code to loop through... 06-17-2021, 06:20 PM
wolfdemon Re: Code to loop through... 06-17-2021, 06:23 PM
Sintek Re: Code to loop through... 06-18-2021, 11:01 AM
wolfdemon Re: Code to loop through... 06-18-2021, 12:08 PM
Sintek Re: Code to loop through... 06-18-2021, 01:35 PM
Sintek Re: Code to loop through... 06-19-2021, 02:15 AM
  1. #1
    Registered User
    Join Date
    07-08-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    12

    Code to loop through input sheet and whenever a cell matches copy to a new sheet

    Hello,

    I am trying to figure this out and I cant. Very new to VBA and was hoping someone can help me.

    I have attached sample sheet. The way I want it to work is, I will say I want code OK1 & OK2 information to be pulled from the input tab. The code will loop through the input sheet and whenever that code matches it will copy over column A & J to output sheet. Once OK1 once is done, it will enter space and then it will do it for OK2.

    Help is really appreciated!!!!
    Attached Files Attached Files

  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
    48,115

    Re: Code to loop through input sheet and whenever a cell matches copy to a new sheet

    Why can't you just sort the input data into the order you want it?
    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
    Registered User
    Join Date
    07-08-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Code to loop through input sheet and whenever a cell matches copy to a new sheet

    There are over 50,000 lines and it will take me a long time to do it manually. Plus i have to do this everyday hence figured vba should be faster
    Last edited by wolfdemon; 06-17-2021 at 06:27 PM.

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Code to loop through input sheet and whenever a cell matches copy to a new sheet

    say I want code OK1 & OK2 information to be pulled from the input tab.
    One way...
    Sub J3v16()
    Dim Data, Code, Temp, i As Long, ii As Long, x As Long
    Data = Sheets("Input").Cells(1).CurrentRegion: Code = Array("OK1", "OK2")
    ReDim Temp(1 To UBound(Data) * UBound(Data), 1 To 3)
    For i = LBound(Code) To UBound(Code)
        x = IIf(i = 0, x + 1, x + 2)
        Temp(x, 1) = "Code": Temp(x, 2) = Code(i)
        x = x + 1: Temp(x, 2) = "Ticker": Temp(x, 3) = "Type"
        For ii = 2 To UBound(Data)
            If Data(ii, 7) = Code(i) Then
                x = x + 1: Temp(x, 2) = Data(ii, 1): Temp(x, 3) = Data(ii, 10)
            End If
        Next ii
    Next i
    Sheet2.Range("A" & Rows.Count).End(xlUp).Resize(x, 3).Value = Temp
    End Sub
    Last edited by Sintek; 06-18-2021 at 11:17 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  5. #5
    Registered User
    Join Date
    07-08-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Code to loop through input sheet and whenever a cell matches copy to a new sheet

    But it crashes for 50K lines saying out of memory at the ReDim temp (1 to Ubound(Data) * UBound(Data), 1 to 3) line. Any thoughts how to fix this?
    Last edited by wolfdemon; 06-18-2021 at 12:57 PM.

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Code to loop through input sheet and whenever a cell matches copy to a new sheet

    My bad...
    ReDim Temp(1 To UBound(Data) * UBound(Data,2), 1 To 3)

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Code to loop through input sheet and whenever a cell matches copy to a new sheet

    Thanks for rep + ...

+ 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. excel loop to copy data from sheet 1 to sheet 2 based on matches
    By vpan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2021, 07:46 PM
  2. code to copy paste the rows to a new sheet if matches 2 categories
    By Elainefish in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-19-2018, 01:45 AM
  3. Replies: 6
    Last Post: 05-29-2017, 04:16 PM
  4. [SOLVED] Vba code to copy unique email id data from input sheet to output sheet
    By UPA in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-09-2017, 07:19 AM
  5. [SOLVED] Copy cell range from “input sheet” to “output sheet”
    By nur2544 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-23-2014, 01:57 PM
  6. Pull data from a specific sheet that matches cell value on Input Sheet
    By vaciaravino in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-12-2012, 08:38 PM
  7. Replies: 1
    Last Post: 10-30-2009, 10:58 AM

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