+ Reply to Thread
Results 1 to 6 of 6

Help needed with VBA to iterate through a worksheet

  1. #1
    Registered User
    Join Date
    01-25-2023
    Location
    UK
    MS-Off Ver
    Windows 10 Enterprise
    Posts
    5

    Question Help needed with VBA to iterate through a worksheet

    I am trying to make a macro that will check an export and copy certain cells if specific requirements are met.

    In a nutshell, I want to iterate through the cells in column P. If Px=0, then go on to cell P(x+1) and check the value. When Px=1 then I need to iterate through column N to find N(x+y)=1. Once this condition is met I want to copy the range Ax:I(x+y) to another sheet within the same workbook.

    After copying I want the iteration to go on, checking the conditions every time and copying when necessary, until it reaches the end. Every time it copies to the next sheet, I would like the copy to be underneath the previous copy.

    My question is, can this be done with VBA? I haven't touched VBA in decades, and so am quite rusty. I'm not asking that anyone solve this, but rather that they point me in the right direction. Which VBA functions should I considering using, etc.?
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,916

    Re: Help needed with VBA to iterate through a worksheet

    What is the significance of "1" in column P as your post "suggests" that column C could be used to delimit your data (assumimg I have understood your requirement). Adding expected results in Sheet1 would help
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Registered User
    Join Date
    01-25-2023
    Location
    UK
    MS-Off Ver
    Windows 10 Enterprise
    Posts
    5

    Re: Help needed with VBA to iterate through a worksheet

    I'm only interested in suppliers that have a date attributed to them (in the attached example, the supplier on line 7 has a date in cell I7; the suppliers on lines 10 & 14 have no date, so they don't interest me). Given that the export has no formatting I had to come up with the system of 0s and 1s so that eventually the loop can identify which cells in column I have dates in them. Does that make sense?

  4. #4
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,278

    Re: Help needed with VBA to iterate through a worksheet

    Quote Originally Posted by Br4c4r4 View Post
    ... to iterate through the cells in column P. If Px=0, then go on to cell P(x+1) and check the value. When Px=1 then I need to iterate through column N to find N(x+y)=1. Once this condition is met I want to copy the range Ax:I(x+y) to another sheet within the same workbook ...
    The data in your example is not very representative, but try the code below (you must have a "Result" sheet in your workbook):
    (not tested)
    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Help needed with VBA to iterate through a worksheet

    Quote Originally Posted by Br4c4r4 View Post
    Does that make sense?
    Hello. These helper formulas are useful in explaining the problem, but should not necessarily be used in a macro.

    Here is another way to solve your problem:

    PHP Code: 
    Sub Macro8()
    Dim ws As WorksheetLR&, aQ&, i&, As RangeAs Range
    Application
    .ScreenUpdating False
    With ActiveSheet
      LR 
    = .Cells(Rows.Count"G").End(xlUp).Row
      a 
    Application.Transpose(Evaluate("IF( (C7:C" LR "<>"""")*(I7:I" LR "<>""""), Row(C7:C" LR "))"))
      
    Filter(aFalseFalse): UBound(a)
      If 
    = -1 Then Exit Sub
      Set ws 
    Worksheets.Add(After:=Worksheets(.Name))
      For 
    0 To Q
        Set C 
    ws.Cells(Rows.Count"G").End(xlUp)(2, -5)
        
    C.Range("A1:I1") = ">----------<"
        
    Set D = .Range("G" a(i), "G" LR).Find(What:="Amount BOL-No"LookIn:=xlValuesLookAt:=xlPartSearchDirection:=xlNext)
        .
    Range("A" a(i), "I" D.Row).Copy C(2)
      
    Next
    End With
    ws
    .Columns("a:i").AutoFit
    End Sub 
    Attached Files Attached Files
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  6. #6
    Registered User
    Join Date
    01-25-2023
    Location
    UK
    MS-Off Ver
    Windows 10 Enterprise
    Posts
    5

    Re: Help needed with VBA to iterate through a worksheet

    Wow, thank you very much guys! This was way more than I expected! I will definitely look at these codes and study them. Thanks once again! :-)

+ 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. new to vba-looking to iterate
    By student2110 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-30-2019, 02:03 PM
  2. Iterate through worksheet and add to listview
    By DPashley in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-30-2016, 10:53 AM
  3. [SOLVED] VBA code needed to iterate to common solution
    By hassanm in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 07-09-2015, 09:28 AM
  4. iterate
    By Gaurav_92 in forum Excel General
    Replies: 1
    Last Post: 06-03-2014, 01:42 AM
  5. Macro help needed to collate data from one worksheet into a second worksheet
    By Pearl18 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-01-2009, 07:20 AM
  6. need to iterate, or something...
    By jacquesmckenna in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-28-2008, 11:34 AM
  7. Can't iterate thru a row with For
    By peter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2005, 01:06 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