+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29
  1. #16
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,490

    Re: Using Find in a Loop

    Hello matrex,

    The macro I wrote will run on Excel 2000 and later. The error you are getting is due to your version of Excel being earlier than 2000 or you are missing the scrrun.dll or it has become damaged. This is the Microsoft Scripting Runtime library which has the object reference to the Dictionary object. When the error occurs, due you see any "missing" references in your project?. Go to Tools > References... in the VBIDE to see which libraries are missing in your project. Let me know two things: What system you are using (versions of Windows and Excel), and if you are missing the scrrun.dll from your VBA project.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  2. #17
    Forum Contributor
    Join Date
    06-07-2008
    Posts
    111

    Re: Using Find in a Loop

    Not sure how it can be misunderstood! I thought my explanation and example were simplified enough not to create any misundertanding. I will however clarify further:

    In the example there are four instances of item AAA in column A. The corresponding quantities of these four instances are 2, 1, 4 and 3 (which add up to 10). So I wanted the macro to combine these four items into one AAA item with 10 quantity and delete the other three AAA's.

    AAA 2
    BBB 2
    AAA 1
    AAA 4
    FFF 3
    GGG 2
    GGG 1
    RRR 4
    AAA 3
    BBB 1

    Please let me if it is still not clear.

  3. #18
    Forum Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: Using Find in a Loop

    Dude did you fix the error?
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

  4. #19
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2003, 2007
    Posts
    355

    Re: Using Find in a Loop

    Quote Originally Posted by matrex View Post
    In the example there are four instances of item AAA in column A. The corresponding quantities of these four instances are 2, 1, 4 and 3 (which add up to 10). So I wanted the macro to combine these four items into one AAA item with 10 quantity and delete the other three AAA's.
    ok, I had a mistake to understand your problem

    Plz try testing the version 2nd SUB, like this:
    Code:
    Public Sub findx()
    Const StartRow = 2
    Dim c, firstAddress, iR As Long, eR As Long, Q, Sh0 As Worksheet
    
    Set Sh0 = Application.ActiveSheet
    
    eR = Sh0.Range("A" & Rows.Count).End(xlUp).Row
    Dim aCo() As Byte: ReDim aCo(eR) As Byte: For iR = 0 To eR: aCo(iR) = 0: Next
    
    For iR = StartRow To eR
        If Sh0.Cells(iR, "A").Value2 = "" Then
            aCo(iR) = 2
        Else
            If aCo(iR) = 0 Then
                firstAddress = Sh0.Cells(iR, "A").Address
                Q = Sh0.Cells(iR, "A").Offset(, 1).Value2
                
                With Sh0.Range("a" & iR & ":a" & eR)
                    Set c = .Find(Sh0.Cells(iR, "A").Value2, LookIn:=xlValues, LookAt:=xlWhole)
                    Do While Not c Is Nothing And c.Address <> firstAddress
                        aCo(c.Row) = 1
                        Q = Q + c.Offset(, 1).Value2
                        Set c = .FindNext(c)
                    Loop
                End With
               Sh0.Cells(iR, "A").Offset(, 1).Value = Q
            End If
        End If
    Next iR
    For iR = eR To StartRow Step -1
       If aCo(iR) = 1 Then Sh0.Rows(iR).Delete
    Next iR
    
    End Sub

  5. #20
    Forum Contributor
    Join Date
    06-07-2008
    Posts
    111

    Re: Using Find in a Loop

    Quote Originally Posted by pike View Post
    Dude did you fix the error?
    pike, yes I managed to get around the sheet error, but my problem is I can't get it to work if the columns are switched (Items are in column B and the Quantities are column A, and column C is not free). Can you please help. It has to do with Advanced Filter line. Thanks.

  6. #21
    Forum Contributor
    Join Date
    06-07-2008
    Posts
    111

    Re: Using Find in a Loop

    Quote Originally Posted by Leith Ross View Post
    Hello matrex,

    The macro I wrote will run on Excel 2000 and later. The error you are getting is due to your version of Excel being earlier than 2000 or you are missing the scrrun.dll or it has become damaged. This is the Microsoft Scripting Runtime library which has the object reference to the Dictionary object. When the error occurs, due you see any "missing" references in your project?. Go to Tools > References... in the VBIDE to see which libraries are missing in your project. Let me know two things: What system you are using (versions of Windows and Excel), and if you are missing the scrrun.dll from your VBA project.
    I have both Excel 2000 and 2007. In both versions I cannot access the Tools > References. Says "Error accessing the system registry". Always had this problem and could never figure out how to fix it. Tried assigning permissions in the regostry, reinstalling everything etc. No joy.

    Anyway, thanks for your help.

  7. #22
    Forum Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: Using Find in a Loop

    Hey matrex
    All the codes posted will solve the first scenario
    for the next sheet[s] we will need more information
    like the locations of data columns in the sheets or are they random ?
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

  8. #23
    Forum Contributor
    Join Date
    06-07-2008
    Posts
    111

    Re: Using Find in a Loop

    Quote Originally Posted by tigertiger View Post
    ok, I had a mistake to understand your problem

    Plz try testing the version 2nd SUB, like this:
    Code:
    Public Sub findx()
    Const StartRow = 2
    Dim c, firstAddress, iR As Long, eR As Long, Q, Sh0 As Worksheet
    
    Set Sh0 = Application.ActiveSheet
    
    eR = Sh0.Range("A" & Rows.Count).End(xlUp).Row
    Dim aCo() As Byte: ReDim aCo(eR) As Byte: For iR = 0 To eR: aCo(iR) = 0: Next
    
    For iR = StartRow To eR
        If Sh0.Cells(iR, "A").Value2 = "" Then
            aCo(iR) = 2
        Else
            If aCo(iR) = 0 Then
                firstAddress = Sh0.Cells(iR, "A").Address
                Q = Sh0.Cells(iR, "A").Offset(, 1).Value2
                
                With Sh0.Range("a" & iR & ":a" & eR)
                    Set c = .Find(Sh0.Cells(iR, "A").Value2, LookIn:=xlValues, LookAt:=xlWhole)
                    Do While Not c Is Nothing And c.Address <> firstAddress
                        aCo(c.Row) = 1
                        Q = Q + c.Offset(, 1).Value2
                        Set c = .FindNext(c)
                    Loop
                End With
               Sh0.Cells(iR, "A").Offset(, 1).Value = Q
            End If
        End If
    Next iR
    For iR = eR To StartRow Step -1
       If aCo(iR) = 1 Then Sh0.Rows(iR).Delete
    Next iR
    
    End Sub

    tigertiger, yes this seems to work. Takes some time to finish but does the job. Thank you very much.

  9. #24
    Forum Contributor
    Join Date
    06-07-2008
    Posts
    111

    Re: Using Find in a Loop

    Quote Originally Posted by pike View Post
    Hey matrex
    All the codes posted will solve the first scenario
    for the next sheet[s] we will need more information
    like the locations of data columns in the sheets or are they random ?
    pike, as I mentioned in my last post, it works if the data (i.e. Item Description') is in column A and the the numbers to be added (i.e. Quantity') are in column B, but I would like to modify the code for the case when these columns are switched (Item Description in B and Quantity in A).

  10. #25
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,129

    Re: Using Find in a Loop

    Takes some time to finish but does the job.
    Leith's code aggregates 10,000 rows in less than a second.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  11. #26
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,490

    Re: Using Find in a Loop

    Hello matrex,

    After some digging, I found this article. If you have Crystal Reports 8.0 you should read it.

    'Error Accessing the System Registry' Message When Displaying VB/VBA References
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  12. #27
    Forum Contributor
    Join Date
    06-07-2008
    Posts
    111

    Re: Using Find in a Loop

    Quote Originally Posted by Leith Ross View Post
    Hello matrex,

    After some digging, I found this article. If you have Crystal Reports 8.0 you should read it.

    'Error Accessing the System Registry' Message When Displaying VB/VBA References
    Hi Leith. Been there done that! In fact this is what I was referring to when I said assigning permission in the registry. Long procedure but unfortunately did not resolve the issue.

  13. #28
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,490

    Re: Using Find in a Loop

    Hello matrex,
    In my previous post, I thought your were referring to network permission settings in the registry. I hadn't yet read the procedure outlined in the article. At this point, I have no ideas as to what is causing your problem. If I find anything else, I'll post it.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  14. #29
    Forum Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: Using Find in a Loop

    you can play around and combin the two codes and by add an if statment to see what the first column?

    Code:
    Sub ptesti()
        Dim lRows As Long
        Application.ScreenUpdating = False
        With ActiveSheet.UsedRange
            lRows = .Rows.Count
         .Columns(3).Insert
         .Columns(3).FormulaR1C1 = "= SUMIF(R2C[-1]:R" & lRows & "C[-1],RC[-1],R2C[-2]:R" & lRows & "C[-2])"
         .Columns(3).Value = .Columns(3).Value
         .Columns(4).Insert
         .Columns(4).FormulaR1C1 = "=RC[-3]"
         .Columns(4).Value = .Columns(4).Value
         .Columns(1).Delete
         .Resize(, 2).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Resize(, 2), copytorange:=Range("E1"), unique:=True
         .Columns(2).Delete
        End With
    Application.ScreenUpdating = True
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

Thread Information

Users Browsing this Thread

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

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.2.0