Consulting

Results 1 to 7 of 7

Thread: VBA Macro. I Think I'm Close But Something Is Not Right

  1. #1

    Question VBA Macro. I Think I'm Close But Something Is Not Right

    I have an Excel sheet that has date in this format:

    Group ID Complete Path Filename
    0 C:\Folder1\R0CVNM6.pdf R0CVNM6.pdf
    0 J:\FolderA\f303919232.pdf f303919232.pdf
    1 C:\Folder1\R0YC44P.pdf R0YC44P.pdf
    1 J:\FolderA\c34563.pdf c34563.pdf
    1 H:\FolderZ\t342332.pdf t342332.pdf
    1 K:\FolderS\j73838a.pdf j73838a.pdf

    etc...

    The Group ID goes up to 51374 but there are 115590 rows in this spreadsheet with the Group ID having a minimum of 2 identical numbers (i.e. two 0's, four 1's, etc.). The maximum count having the same Group ID is 21.

    I would like my script to go through each row 0 through 115590 and take the 1st row of each Group ID and move that file to a folder and all the rest with the same Group ID, move it to a different folder. Then move on to the next Group ID and do the same. Repeat...

    Here is my first attempt, but it only copied 1 file and then skipped the rest.

    Sub MoveFiles()    
    zz = 2
        For j = 0 To 20 '51374
            aa = Range("A" & zz)
            bb = Range("B" & zz)
            cc = Range("C" & zz)
            If j = aa And fth = 0 Then
                fth = 1
                Name bb As "J:\kfolder\" & cc
            End If
            If j = aa And fth = 1 Then
                Name bb As "J:\ffolder\" & cc
            End If
            zz = zz + 1
        Next j
    End Sub
    There has to be a better approach to this.

    Also, some of these filenames (very few) have special characters and Excel errors on these. I would like Excel to just ignore and maybe flag or highlight these.

    I do know how to use Kill to delete the final files, but I want to get the code working first before deleting a bunch of files.

    Thank you,
    Zune

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,103
    Location
    Welcome to VBAX Zunebuggy. What does the variable fth stand for?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Quote Originally Posted by Aussiebear View Post
    Welcome to VBAX Zunebuggy. What does the variable fth stand for?
    In my mind, "First Time Here". It's just a flag.

  4. #4
    I was trying to use that so it only moves the first unique Group ID to the kfolder and the rest with that same Group ID to the ffolder.

  5. #5
    I see that I need to reset this back to zero every time the Group ID number changes. My Excel sheet is sorted by Group ID if that makes it easier. I did check and there are 2 or more of rows with the same Group ID. Many have 2 of the same Group ID, some have 3, some have 4, some have 5, all the way up to the maximum count is 21. I guess if they each just had two of the same Group IDs it would be easy. I could just For Next it sending Odd numbered rows to one folder and the even numbered rows to another, but the sheet is more complicated than that.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,743
    Location
    I think that you're over thinking it


    Option Explicit
    
    
    Sub MoveFiles()
        Dim rFiles As Range
        Dim r As Long
        
        Set rFiles = ActiveSheet.Cells(1, 1).CurrentRegion
    
    
        With rFiles
            For r = 2 To .Rows.Count
                If .Cells(r, 1).Value = .Cells(r - 1, 1).Value Then '   same group
                    '  Name CStr(.Cells(r, 2).Value) As "J:\kfolder\" & .Cells(r, 3).Value
                    Debug.Print "Group = " & .Cells(r, 1).Value & " -- Move " & CStr(.Cells(r, 2).Value); " to " & "J:\kfolder\" & .Cells(r, 3).Value
                Else
                    '   Name CStr(.Cells(r, 2).Value) As "J:\ffolder\" & .Cells(r, 3).Value '   row r is one group and row r-1 is another
                    Debug.Print "Group = " & .Cells(r, 1).Value & " -- Move " & CStr(.Cells(r, 2).Value); " to " & "J:\ffolder\" & .Cells(r, 3).Value
                End If
            Next r
        End With
     End Sub

    Debug output

    Group = 0 -- Move C:\Folder1\R0CVNM6.pdf to J:\ffolder\R0CVNM6.pdf
    Group = 0 -- Move J:\FolderA\f303919232.pdf to J:\kfolder\f303919232.pdf
    Group = 1 -- Move C:\Folder1\R0YC44P.pdf to J:\ffolder\R0YC44P.pdf
    Group = 1 -- Move J:\FolderA\c34563.pdf to J:\kfolder\c34563.pdf
    Group = 1 -- Move H:\FolderZ\t342332.pdf to J:\kfolder\t342332.pdf
    Group = 1 -- Move K:\FolderS\j73838a.pdf to J:\kfolder\j73838a.pdf

    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    Thank you. That worked very well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •