L
Legacy 93538
Guest
(Linked to previous posted VBA Loops not pasting in correct location)
Hi
I have created a piece of code which loops througha folder and opens all the files and copies and pastes data fom a range into a certain location. However i need to change it so when it loops the file it checks the names of the file and if has certain text in paste it into different sheets.
I need it to do the following:
If it is "Graphing_MTH_Actual_Curr_Year" & "*.csv" paste data on the MTH Sheet
If it is "Graphing_MTH_Actual_Prev_Year" & "*.csv" paste data on the MTHPrevious Sheet
If it is "Graphing_YTD_Actual_Curr_Year" & "*.csv" paste data on the YTD Sheet
If it is "Graphing_YTD_Actual_Prev_Year" & "*.csv" paste data on the YTDPrevious Sheet
If it is "Graphing_R12_Actual_Curr_Year" & "*.csv" paste data on the R12 Sheet
If it is "Graphing_R12_Actual_Prev_Year" & "*.csv" paste data on the R12Previous Sheet
I have tried doing it with multiple if statements but it just crashes. I would try variables but i am unsure of how to use the.
Does anyone know how to change this loop so it checks the file name on the list above and pastes onto the correct sheet?
Thanks to anyone who can help.
Jessicaseymour
Hi
I have created a piece of code which loops througha folder and opens all the files and copies and pastes data fom a range into a certain location. However i need to change it so when it loops the file it checks the names of the file and if has certain text in paste it into different sheets.
I need it to do the following:
If it is "Graphing_MTH_Actual_Curr_Year" & "*.csv" paste data on the MTH Sheet
If it is "Graphing_MTH_Actual_Prev_Year" & "*.csv" paste data on the MTHPrevious Sheet
If it is "Graphing_YTD_Actual_Curr_Year" & "*.csv" paste data on the YTD Sheet
If it is "Graphing_YTD_Actual_Prev_Year" & "*.csv" paste data on the YTDPrevious Sheet
If it is "Graphing_R12_Actual_Curr_Year" & "*.csv" paste data on the R12 Sheet
If it is "Graphing_R12_Actual_Prev_Year" & "*.csv" paste data on the R12Previous Sheet
I have tried doing it with multiple if statements but it just crashes. I would try variables but i am unsure of how to use the.
Does anyone know how to change this loop so it checks the file name on the list above and pastes onto the correct sheet?
Code:
strFldr = "C:\Documents and Settings\SeymourJ\My Documents\Tasks\"
strFile = Dir(strFldr & "Graphing_MTH_Actual_Curr_Year" & "*.csv")
Application.Calculation = xlCalculationManual
lNextrow = 2
If Len(strFile) > 0 Then
Do
Set wbCsv = Workbooks.Open(Filename:=strFldr & "\" & strFile)
Set wsMyCsvSheet = wbCsv.Sheets(1)
With Workbooks("Book1Template.xlsx").Sheets("MTH")
wsMyCsvSheet.Range("A2:M14").Copy
Workbooks("Book1Template.xlsx").Sheets("MTH").Cells(lNextrow, 2).PasteSpecial
End With
lNextrow = lNextrow + 14
'close it
wbCsv.Close
'go to next file
strFile = Dir
Application.StatusBar = strFile
Loop Until Len(strFile) = 0
End If
Thanks to anyone who can help.
Jessicaseymour