+ Reply to Thread
Results 1 to 4 of 4

VBA Loop, Find, Copy, Repeat Loop

Hybrid View

sparx VBA Loop, Find, Copy, Repeat... 10-01-2013, 05:15 PM
JBeaucaire Re: VBA Loop, Find, Copy,... 10-01-2013, 06:35 PM
sparx Re: VBA Loop, Find, Copy,... 10-02-2013, 05:43 AM
JBeaucaire Re: VBA Loop, Find, Copy,... 10-02-2013, 06:25 AM
  1. #1
    Forum Contributor
    Join Date
    11-23-2004
    Location
    Bristol
    MS-Off Ver
    2016
    Posts
    235

    VBA Loop, Find, Copy, Repeat Loop

    Hi everybody, I have a query and hope somebody on here can help me, its regards VBA loops.

    I will try to clearly explain what I am trying to do.

    Currently,

    I import into (sheet1) via some vba a list of all the files found within a folder on my PC.
    All filenames include their file extension ( I am only interested in the pdf files not xls ).
    I then compare (sheet1) against a master database (sheet2) via formulas to highlight any new files.
    I then sort the data in (sheet1) so all the new pdf filenames are at the top or bottom of the list
    then select and copy those filenames into a specific column in (sheet2) where they stay.

    I have to manually delete the file extension at this point in (sheet2) which I would also like to
    automate soon.

    Propose Use.

    As this is a long winded process, I am trying to automate the whole thing but am finding it a little
    hard to resolve.

    So to list in order what I am trying to do is this.

    1. Import folder list into (sheet1) - this is already working
    2. Compare (sheet1) against (sheet2) and list differences - this is already working
    3. VBA to loop down (sheet1) column A, all cells with the word "New" copy the result of (sheet1), column B into (sheet2) column C
    4. Remove the extension part of the filename which was just copied.
    5. Repeat the process until completed.

    I have attached some code I have been trying to work on also an example file.

    Thanks

    Sub test()
    
        Dim lastRow As Long
        Dim rng As Range
        Dim firstCell As String
        Dim findString As String
        Set ws = Worksheets("Sheet1")
        findString = "New"
        
        lastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
        For i = 1 To lastRow
            
            Set rng = Sheets("Sheet1").Range("A:A").Find(What:=findString, LookIn:=xlValues, lookAt:=xlPart, SearchOrder:=xlByRows)
    
            If Not rng Is Nothing Then firstCell = rng.Address
            Do While Not rng Is Nothing
    
                rng.Offset(0, 2) = rng ' This line copies the data into next column
                
                Set rng = Sheets("sheet1").Range("A:A").FindNext(rng)
                If Not rng Is Nothing Then
                If rng.Address = firstCell Then Exit Do
                End If
    
            Loop
        
        Next
    
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Loop, Find, Copy, Repeat Loop

    Like so:

    Sub test()
    Dim LR As Long, i As Long
    Dim findString As String
    
        Set ws = Worksheets("Sheet1")
        findString = "New"
    
        LR = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
        For i = 1 To LR
            If ws.Range("A" & i) = findString Then
                With ws.Range("B" & i)
                    If Right(.Value, 3) = "pdf" Then
                        Sheets("Sheet2").Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Left(.Value, Len(.Value) - 4)
                    End If
                End With
            End If
        Next i
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    11-23-2004
    Location
    Bristol
    MS-Off Ver
    2016
    Posts
    235

    Re: VBA Loop, Find, Copy, Repeat Loop

    Thankyou - I was going around the loop.

    Thankyou so much - now appears solved.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Loop, Find, Copy, Repeat Loop

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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] 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
  2. Find on Sheet1, copy to Sheet2, Find Next Loop
    By ker9 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2011, 02:13 PM
  3. Loop a column on Sheet1 and loop a row on Sheet2 to find a match
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-13-2009, 02:09 PM
  4. loop/repeat copy/paste Macro
    By vizbasic in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-20-2008, 10:19 AM
  5. [SOLVED] [SOLVED] find, delete, repeat loop
    By kimbroughton@westvirginia.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2006, 02:25 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