jamosticker
New Member
- Joined
- Jul 2, 2018
- Messages
- 1
Problem:
1: I have like 20 excel files in a folder
2: I created another excel file specifically to plot all the data in each of the excel files inside the folder together.
3: What I need to do is to create a vba code for the chart that pulls data from each of the 20 excel files in the folder and plot it together automatically.
Please kindly help with this.
Thanks.
I tried using this code that I got from somewhere but it's not working
"Sub TestLoop()
'Declarations
Dim fileName As Variant
Dim myFilePath As String
Dim myChart As Chart
Dim Wkbk As Variant
myFilePath = "C:\Users\jaoadegbite\Desktop\an"
fileName = Dir(myFilePath)
While fileName <> ""
Debug.Print fileName 'This example will print the file name to the immediate window
Set Wkbk = Workbooks.Open(myFilePath & fileName) 'Open Workbook
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered).Chart 'Add Chart
myChart.SetSourceData Source:=ActiveSheet.Range("A2:A30") 'Set DataSource Range
Wkbk.Close SaveChanges:=True 'Close file and save changes
fileName = Dir 'Set the fileName to the next file
Wend
End Sub
1: I have like 20 excel files in a folder
2: I created another excel file specifically to plot all the data in each of the excel files inside the folder together.
3: What I need to do is to create a vba code for the chart that pulls data from each of the 20 excel files in the folder and plot it together automatically.
Please kindly help with this.
Thanks.
I tried using this code that I got from somewhere but it's not working
"Sub TestLoop()
'Declarations
Dim fileName As Variant
Dim myFilePath As String
Dim myChart As Chart
Dim Wkbk As Variant
myFilePath = "C:\Users\jaoadegbite\Desktop\an"
fileName = Dir(myFilePath)
While fileName <> ""
Debug.Print fileName 'This example will print the file name to the immediate window
Set Wkbk = Workbooks.Open(myFilePath & fileName) 'Open Workbook
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered).Chart 'Add Chart
myChart.SetSourceData Source:=ActiveSheet.Range("A2:A30") 'Set DataSource Range
Wkbk.Close SaveChanges:=True 'Close file and save changes
fileName = Dir 'Set the fileName to the next file
Wend
End Sub