Solved

Hello Gurus

Posted on 2013-11-25
2
157 Views
Last Modified: 2013-11-25
Hello All,

i have a VBA question  -

A)      These are 3 folders in a drive with folder names following the naming convention like below…

C:\WW\2014.09.09 DA 10 XLS
C:\WW\2014.10.09 DA 11 XLS
C:\WW\2014.11.12 DA 12 XLS

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

B)      Now there is an excel file (“MAIN”) that has info like below – this basically tells which account belongs in which cycle respectively….

Account      | Cycle
Ac1      |         10
Ac2      |         10
Ac3      |         11
      

-------------------------------------------------------------------------------------------------
C)      Now in a certain separate folder  (C:\RAW\) - there are multiple xls files (specific to accounts) with naming convention such that it has the [account] names included in their filename like this:

Fl_Ac1.xls
fl_Ac2.xls
fl_Ac3.xls

----------------------------------------------------------------------------------------------------
I need VBA to do the following – what macro can I give in the file of step B so that on pressing the button, it loops through the multiple files in folder C:\RAW\ and then looks up the account name in list of sheet A (step B) and then locates the cycle ID, to finally put that file in the respective destination folder in C:\WW\ ---

For example – if the code loops through the file   fl_Ac1.xls – it looks up the cycle ID for Ac1 in “MAIN” file and finds that cycle is 10 – then it copies the file – fl_Ac1.xls into the folder  C:\WW\2014.09.09 DA 10 XLS folder….


Thank you
Rayne
Book1.xlsx
0
Comment
Question by:Rayne
2 Comments
 
LVL 14

Accepted Solution

by:
luconsta earned 500 total points
ID: 39674219
Hi,

You could try the following approach - because the file naming convention you've mentioned, you could go parsing the ACCOUNTS (not files) and then copy the files for each parsed account:

Sub SortAndCopy()

    Dim oCell, oRange As Range
    Dim cAcc, cCycle, cDstFolder As String
    
    'Set the range for accounts (ONLY A COLUMN)
    Set oRange = Worksheets("Sheet1").Range("A2:A5")
    
    For Each oCell In oRange
        cAcc = oCell.Value
        cCycle = Range("B" & oCell.Row).Value
        Debug.Print cAcc & " / " & cCycle
        
        Select Case cCycle
            Case 10
                cDstFolder = "C:\WW\2014.09.09 DA 10 XLS"
            
            Case 11
                cDstFolder = "C:\WW\2014.10.09 DA 11 XLS"
                
            Case 12
                cDstFolder = "C:\WW\2014.11.12 DA 12 XLS"
        End Select
        
        On Error Resume Next
        FileCopy "C:\RAW\FL_" & cAcc & ".XLS", cDstFolder & "\FL_" & cAcc & ".XLS"
    Next oCell
End Sub

Open in new window

0
 

Author Closing Comment

by:Rayne
ID: 39674237
Thank you :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question