Hey, guys I am having a problem with a macro and was hoping you guys could find the problem. This macro worked in excel 2003 but now that I'm trying it in excel 2007 it is giving an error.
Sub AddFiles()
Dim rng As Range
Dim lastRow As Integer
reportfile = ActiveWorkbook.Name
Worksheets(1).Activate
Range("A1").Activate
'
'Kopieer lĂŞers oor
With Application.FileSearch
.LookIn = "C:\Users\Pjoub06\Documents\Greenplan\Report\Excel Results + Graphs\Results"
.Filename = "*.csv"
If .Execute > 0 Then 'workbook exists
For i = 1 To .FoundFiles.Count
Workbooks.OpenText Filename:=.FoundFiles(i), DataType:=xlDelimited, comma:=True
wbText = ActiveWorkbook.Name
Worksheets(1).Activate
Set rng = Range("A1").CurrentRegion
rng.Copy
Windows(reportfile).Activate
Worksheets(i).Activate
ActiveCell.PasteSpecial
Worksheets(i).Name = Left(wbText, Len(wbText) - 4)
Worksheets.Add After:=Worksheets(i)
Windows(wbText).Activate
Application.CutCopyMode = False
Workbooks(wbText).Close SaveChanges:=False
Next i
Else 'there is not a workbook'
MsgBox "no workbook"
End If
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
The red text is where the error is occuring. The idea of the macro is to import all the files into a single one (using multiple spreadsheets however) so that I can draw graphs/chart with the information. Plz help (btw I'm noob at this)
Sub AddFiles()
Dim rng As Range
Dim lastRow As Integer
reportfile = ActiveWorkbook.Name
Worksheets(1).Activate
Range("A1").Activate
'
'Kopieer lĂŞers oor
With Application.FileSearch
.LookIn = "C:\Users\Pjoub06\Documents\Greenplan\Report\Excel Results + Graphs\Results"
.Filename = "*.csv"
If .Execute > 0 Then 'workbook exists
For i = 1 To .FoundFiles.Count
Workbooks.OpenText Filename:=.FoundFiles(i), DataType:=xlDelimited, comma:=True
wbText = ActiveWorkbook.Name
Worksheets(1).Activate
Set rng = Range("A1").CurrentRegion
rng.Copy
Windows(reportfile).Activate
Worksheets(i).Activate
ActiveCell.PasteSpecial
Worksheets(i).Name = Left(wbText, Len(wbText) - 4)
Worksheets.Add After:=Worksheets(i)
Windows(wbText).Activate
Application.CutCopyMode = False
Workbooks(wbText).Close SaveChanges:=False
Next i
Else 'there is not a workbook'
MsgBox "no workbook"
End If
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
The red text is where the error is occuring. The idea of the macro is to import all the files into a single one (using multiple spreadsheets however) so that I can draw graphs/chart with the information. Plz help (btw I'm noob at this)