?
Solved

Hello Gurus

Posted on 2013-11-25
2
Medium Priority
?
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
luconsta earned 1500 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

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…
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

752 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