Solved

Hello Gurus

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

912 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

21 Experts available now in Live!

Get 1:1 Help Now