Solved

Hello Gurus

Posted on 2013-11-25
2
153 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now