Hello to all,
I would like to build a macro in order to open files according to different criteria (I will develop it) and then copy and paste a sheet from this file to my master file.
You can see the macro I recorded below which will be a base to explain my problem.
The main problem is to play with the variables, I am just discovering VBA language and confess I am a bit lost...
The first variable would be the “YearFile” corresponding here to : 2012_06
I build this variable under Excel with the following formula: (YearHotel_H1 corresponding to a name of a cell where I choose the year I need, and G6 corresponding to the current year. Then I say that if I choose a year before the current one I have to go in the file with the name YYYY_12, and if it is the current year I choose then the file will follow the format YYYY_MM (MM
=IF(YearHotel_H1<G6,YearHotel_H1&"_"&"12",IF(YearHotel_H1=G6,YearHotel_H1&"_"&IF(M3<10,"0"&MONTH(1&G3),MONTH(1&G3))))
The second variable would be the “RegionFile” here is: “Asia”
The formula under excel is: (Region_H1 corresponding to the name of my cell)
=IF(Region_H1="Asia","Asia","Pacific")
Then the 3rd and last variable would be the “HotelFile”: Here is H1555 – 2012_06
The formula on excel would be: (HotelCode_H1 is also a name of cell)
=HotelCode_H1 & " " & "-" & YearFile & “.xls”
How can I write it in VBA ?
Sub TestImportFiles()
'
' TestImportFiles Macro
'
'
Workbooks.Open Filename:= _
"S:\Finance\Hotel Performance review\2012_06\Asia\H1555 - 2012_06.xls", _
UpdateLinks:=0
Sheets("Monthly Upload File").Select
Cells.Select
Range("A3").Activate
Selection.Copy
Windows("P&L Final Test Macro.xlsx").Activate
Sheets("Actual_H1").Select
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Range("B40").Select
Windows("H1555 - 2012_06.xls").Activate
Range("I19").Select
ActiveWindow.Close
Range("B23").Select
Sheets("Inputs").Select
Range("D17").Select
End Sub
Then to sum up, I would like to have a macro which automatically copy and paste the sheet according to the year and the hotel I chose previously on Excel (thanks to tab list)
Thank you very much for your help !
Best
Bookmarks