+ Reply to Thread
Results 1 to 16 of 16

improve loop

Hybrid View

hgchas improve loop 04-14-2020, 03:50 PM
mehmetcik Re: improve loop 04-14-2020, 04:16 PM
Kaper Re: improve loop 04-14-2020, 04:29 PM
hgchas Re: improve loop 04-15-2020, 02:59 PM
Kaper Re: improve loop 04-15-2020, 03:36 PM
hgchas Re: improve loop 04-15-2020, 04:00 PM
Marc L Hi ! Try this ! 04-15-2020, 11:15 PM
hgchas Re: Hi ! Try this ! 04-17-2020, 12:36 AM
Marc L Re: Hi ! Try this ! 04-17-2020, 07:23 AM
Kaper Re: improve loop 04-17-2020, 04:14 AM
Marc L Re: improve loop 04-17-2020, 09:01 AM
hgchas Re: improve loop 04-17-2020, 09:41 AM
Marc L Re: improve loop 04-17-2020, 08:21 PM
Marc L Re: improve loop 04-17-2020, 11:52 AM
hgchas Re: improve loop 04-17-2020, 12:31 PM
xladept Re: improve loop 04-17-2020, 05:41 PM
  1. #1
    Registered User
    Join Date
    06-05-2019
    Location
    texas
    MS-Off Ver
    2013
    Posts
    53

    improve loop

    hello guys-I am trying to make this loop run a certain way. I have posted it. any help is appreciated.

    Sub searchandfind()
    
    Dim data As Worksheet 
    Dim report As Worksheet 
    Dim name As String
    Dim finalrow As Long 
    Dim i As Variant 
    
    Set data = Sheet3
    Set report = Sheet4
    name = report.Range("D2").Value
    
    data.Select
    finalrow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 2 To finalrow 'I WOULD LIKE TO LOOP THIS FOR EX: COMPLETE ONE ROW THEN CONTINUE TO THE NEXT ROW AND STOP WHEN THERE ARE NO MORE ROWS
        If Cells(i, 1) = name Then 
            Range(Cells(i, 2), Cells(i, 12)).Copy 'IN CASE, HOW COULD I SPECIFY SPECIFIC COLUMNS, FOR EX: (i,2), (i,5), (i,7) 
            reportsheet.Select 
            Range("Z100").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats 
            data.Select 
            End If
    Next i
    
    report.Select 
    
    Range("B2").Select
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: improve loop

    The best way to improve your loop is to get rid of it.

    In Pseudocode:

    Copy all your data to sheet2

    Insert a formula in an Empty column to to return a 1 if column A <> Name

    Copy Paste Values

    Use select Special to select all the 1's in our helper column.

    Resize the selection to Delete all your flagged Rows

    There is no loop so this should be fast.

    
    Sub Macro1()
    
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    
    Range("A1:G" & LR).Copy Sheets("Sheet2").Range("A1")
    
    Sheets("Sheet2").Select
    
    With Range("H1:H" & LR)
     .FormulaR1C1 = "=IF(RC[-7]<>""Name"",1,"""")"
    .Value = .Value
    .SpecialCells(xlCellTypeConstants, 1).Select
    End With
    Selection.EntireRow.Delete
    [A1].Select
    End Sub
    Attached Files Attached Files
    Last edited by mehmetcik; 04-14-2020 at 04:52 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: improve loop

    Or test such code:
    Sub searchandfindwithfind()
    
    Dim data As Worksheet
    Dim report As Worksheet
    Dim name As String
    Dim finalrow As Long
    
    'new variables
    Dim i As Long, j As Long, sourcecell As Range
    
    Set data = Sheet3
    Set report = Sheet4
    name = report.Range("D2").Value
    
    data.Select
    finalrow = Cells(Rows.Count, 1).End(xlUp).Row
    
    'new code
    
    Set sourcecell = Range("A2:A" & finalrow).Find(what:=name, after:=Range("A" & finalrow), LookIn:=xlValues, lookat:=xlWhole)
    If Not sourcecell Is Nothing Then
      i = sourcecell.Row
      j = i
      Do
        Union(Cells(i, 2), Cells(i, 5), Cells(i, 12)).Copy 'IN CASE, HOW COULD I SPECIFY SPECIFIC COLUMNS, FOR EX: (i,2), (i,5), (i,7)
      ' what if there are more than 100 results, may be:
        report.Range("Z" & report.Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
        Set sourcecell = Range("A2:A" & finalrow).FindNext(sourcecell) '(what:=name, after:=Range("A" & finalrow), LookIn:=xlValues, lookat:=xlWhole)
        i = sourcecell.Row
      Loop Until i = j
    End If
    
    report.Select
    
    Range("B2").Select
    
    'new code
    Set report = Nothing
    Set data = Nothing
    
    End Sub
    Best Regards,

    Kaper

  4. #4
    Registered User
    Join Date
    06-05-2019
    Location
    texas
    MS-Off Ver
    2013
    Posts
    53

    Re: improve loop

    EDIT: I have added a sample of attachment of what I am attempting to accomplish

    hello kaper- I am testing your suggested code. It is not looping to the next record in this section:

    Set sourcecell = Range("A2:A" & finalrow).Find(what:=name, after:=Range("A" & finalrow), LookIn:=xlValues, lookat:=xlWhole)
    If Not sourcecell Is Nothing Then 'IT DOES NOT GO TO THE NEXT RECORD FROM HERE;RETURNS ONLY ONE
      i = sourcecell.Row
      j = i
      Do
        Union(Cells(i, 2), Cells(i, 5), Cells(i, 12)).Copy 
      ' what if there are more than 100 results, may be:
        report.Range("Z" & report.Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
        Set sourcecell = Range("A2:A" & finalrow).FindNext(sourcecell) '(what:=name, after:=Range("A" & finalrow), LookIn:=xlValues, lookat:=xlWhole)
        i = sourcecell.Row
      Loop Until i = j
    End If
    Attached Files Attached Files
    Last edited by hgchas; 04-15-2020 at 03:58 PM.

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: improve loop

    have you seen the yellow banner at the top of the page? Please follow the advice

  6. #6
    Registered User
    Join Date
    06-05-2019
    Location
    texas
    MS-Off Ver
    2013
    Posts
    53

    Re: improve loop

    hi-I have edited the previous reply with a sample attachment.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    According to post #4 attachment a VBA beginner starter demonstration :

    PHP Code: 
    Sub Demo1()
        
    Sheet2.UsedRange.Columns(1).Copy Sheet2.[K1]
        
    Sheet1.[C1:D1].Copy Sheet2.[C1]
        
    Sheet1.UsedRange.AdvancedFilter xlFilterCopySheet2.[K1].CurrentRegionSheet2.[A1:D1]
        
    Sheet2.[K1].CurrentRegion.Clear
    End Sub 

    The same demonstration revamped :

    PHP Code: 
    Sub Demo1r()
        
    With Sheet2.UsedRange.Columns
            
    .Item(1).Copy .Item(11)
             
    Sheet1.[C1:D1].Copy .Range("C1")
             
    Sheet1.UsedRange.AdvancedFilter xlFilterCopy, .Item(11), .Range("A1:D1")
            .
    Item(11).Clear
        End With
    End Sub 
    ► Do you like it ? ► ► So thanks to click on bottom left star icon « Add Reputation » ! ◄ ◄
    Last edited by Marc L; 04-15-2020 at 11:33 PM.

  8. #8
    Registered User
    Join Date
    06-05-2019
    Location
    texas
    MS-Off Ver
    2013
    Posts
    53

    Re: Hi ! Try this !

    Hi Marc-thank you for your feedback! I ran the code but I am looking to run a index/match function through each row between two datasets. The first post code worked but only returned one row and I am wanting to loop through the datasets to find commonalities

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Hi ! Try this !


    Quote Originally Posted by hgchas View Post
    Hi Marc-thank you for your feedback! I ran the code but I am looking to run a index/match function through each row between two datasets. The first post code worked but only returned one row and I am wanting to loop through the datasets to find commonalities
    The more data, the less efficient than using an advanced filter …

  10. #10
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: improve loop

    The example you posted in post #4 is quite different than what has been posted in post #1
    Moreover, I suspect that example data is oversimplified (see below for the attachment suggestions), because for such data with unique ID, a simple VLOOKUP would do the job.
    Use in C2 in sheet 2:
    Formula: copy to clipboard
    =VLOOKUP(A2,Sheet1!A:D,3,0)
    and the same in D2, but with 4 (instead of 3) as third argument.

    ---
    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. (If there are typical cases like: all unique values/duplicates could occur, day/night, nobody present/several persons at once, before/on/past due, empty cells between data, mixed text/numbers, etc. - please show them all, or at least indicate in text). The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution(s) is/are also shown (mock up the results manually).

    3. Make sure that all confidential/restricted information (either personal or business) like real e-mails, social security numbers, bank accounts, etc. is removed first!!

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: improve loop


    On my side my codes well work, return all the expected data according to your post #4 attachment …

  12. #12
    Registered User
    Join Date
    06-05-2019
    Location
    texas
    MS-Off Ver
    2013
    Posts
    53

    Re: improve loop

    hi-I have attached a workbook with sample data. I used the index/match function to show what I am wanting to accomplish. thanks in advance!
    Attached Files Attached Files

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: improve loop

    Quote Originally Posted by hgchas View Post
    I have attached a workbook with sample data. I used the index/match function to show what I am wanting to accomplish.
    Again no formula within the attachment neither a technical explanation so no logic to apply, a guess challenge, no thanks …

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: improve loop


    That's very weird to attach a sample with a different layout than the original (post #4)
    or you must be very confident with your Excel / VBA skills to amend any code !

    According to the last attachment, explain the actual layout of Sheet2,
    as there is no formula what exactly to do, attach the expected result, it may help, a lot …

  15. #15
    Registered User
    Join Date
    06-05-2019
    Location
    texas
    MS-Off Ver
    2013
    Posts
    53

    Re: improve loop

    hi marc-thanks for the feedback, I appreciate it! I have attached a different working sample with more data to reflect what I am wanting to accomplish. I hope I can convey what I am trying to do.
    Attached Files Attached Files

  16. #16
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: improve loop

    This works when the keys are properly entered - In your sample there were 3 East 95's for instance.

    But try it and see whether it works for you:

    Sub hgcas(): Dim K, A, B, T, Key As String, r As Long, s As Long
    Dim w1 As Worksheet, w2 As Worksheet
    Set w1 = Sheets("Sheet1"): Set w2 = Sheets("Sheet2"): A = w1.UsedRange
    K = w2.Range("H1").CurrentRegion: B = w2.Range("A1").CurrentRegion
    T = w2.Range("L1").Resize(UBound(K, 1), UBound(B, 2) + 2)
    With CreateObject("Scripting.Dictionary")
                For r = 2 To UBound(K)
    Key = K(r, 1) & K(r, 2): .Item(Key) = r
                For s = 2 To UBound(A): Key = A(r, 1) & A(r, 3)
    If .Exists(Key) Then
    T(r, 1) = B(r, 1): T(r, 2) = K(r, 1): T(r, 3) = B(r, 2): T(r, 4) = B(r, 3)
    T(r, 6) = B(r, 4): T(r, 7) = B(r, 5): T(r, 5) = T(r, 7) / T(r, 6): Exit For
    End If: Next s: Next r
    End With: w2.Range("L1").Resize(UBound(K, 1), UBound(B, 2) + 2) = T
    End Sub
    Last edited by xladept; 04-18-2020 at 12:05 AM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

+ 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] Improve Speed: VBA Code very slow due to changing cell values in for loop
    By Nick_G in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-29-2020, 03:25 PM
  2. Improve code so it doesn't loop through worksheets seperately?
    By carissa7 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-18-2018, 04:52 AM
  3. How to improve my code ?
    By ozstrik3r69 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-23-2017, 08:51 AM
  4. Does this code needs improvement?
    By amartinez988 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-25-2016, 09:21 AM
  5. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  6. Need your help to improve
    By baba4005 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 04-19-2012, 03:32 PM
  7. Help to improve macro
    By unni5959 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-19-2005, 07:05 PM

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