Trying to write a script and having trouble getting it to work.
Export.CSV
Every month a CSV file will be made available. The number of rows are variable every month, but the columns contain data from A to AS. The name of this export will change and reflect the month the data is from.
Report.XLSM
I have another spreadsheet into which I would like to import this data with a macro.
Macro
I would like to run a macro in Report.XLSM, have it prompt for the location of the report containing the data (Export.csv), copy all of the data, paste it into the "Data" table on the Data sheet in the Report.XLSM workbook.
Here is what I have so far. I am a newbie, so this might be pretty messed up.
Thanks for any help you can offer.
Sub copyData()
Dim fn
Dim wbFrom As Workbook
Dim ws As Worksheet
Dim rCopy As Range
Dim sSht As String
On Error Resume Next
fn = Application.GetOpenFilename
If fn = False Then
MsgBox "Nothing Chosen", vbCritical, "Select workbook"
Else: Workbooks.Open fn
Set wbFrom = ActiveWorkbook
For Each ws In wbFrom.Worksheets
With ws
sSht = .Name
'determine range to copy
Set rCopy = .Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
End With
If Not rCopy Is Nothing Then rCopy.Copy _
ThisWorkbook
Sheets("Data").Select
Range("Table1[[#Headers],[Project Number]]").Select
Selection.End(xlDown).Select
Next ws
End If
On Error GoTo 0
Set rCopy = Nothing
Set wbFrom = Nothing
End Sub
Export.CSV
Every month a CSV file will be made available. The number of rows are variable every month, but the columns contain data from A to AS. The name of this export will change and reflect the month the data is from.
Report.XLSM
I have another spreadsheet into which I would like to import this data with a macro.
Macro
I would like to run a macro in Report.XLSM, have it prompt for the location of the report containing the data (Export.csv), copy all of the data, paste it into the "Data" table on the Data sheet in the Report.XLSM workbook.
Here is what I have so far. I am a newbie, so this might be pretty messed up.
Thanks for any help you can offer.
Sub copyData()
Dim fn
Dim wbFrom As Workbook
Dim ws As Worksheet
Dim rCopy As Range
Dim sSht As String
On Error Resume Next
fn = Application.GetOpenFilename
If fn = False Then
MsgBox "Nothing Chosen", vbCritical, "Select workbook"
Else: Workbooks.Open fn
Set wbFrom = ActiveWorkbook
For Each ws In wbFrom.Worksheets
With ws
sSht = .Name
'determine range to copy
Set rCopy = .Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
End With
If Not rCopy Is Nothing Then rCopy.Copy _
ThisWorkbook
Sheets("Data").Select
Range("Table1[[#Headers],[Project Number]]").Select
Selection.End(xlDown).Select
Next ws
End If
On Error GoTo 0
Set rCopy = Nothing
Set wbFrom = Nothing
End Sub