Hi,
I'm trying to write a code such that when the user presses the button GRAPH, it graphs the data in the file for each column
Capture.PNG
I run into a debug error on this line:
Range(Cells(1, NCol), Cells(LastRow, NCol)).Select
Full code:
Private Sub cmdGraph_Click()
gFolderpath.Text = chkLastChar(gFolderpath.Text)
ChDir gFolderpath.Text
Workbooks.Open Filename:=gFolderpath.Text & gFilename
Dim nLeft As Long, nTop As Long
Dim strChrt As String
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
nLeft = 20: nTop = 0
For NCol = 1 To LastCol
Range(Cells(1, NCol), Cells(LastRow, NCol)).Select
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Range(Cells(1, NCol), Cells(LastRow, NCol))
strChrt = Trim(Replace(ActiveChart.Name, ActiveSheet.Name, ""))
ActiveSheet.Shapes(strChrt).Left = nLeft
ActiveSheet.Shapes(strChrt).Top = nTop
'~~> Increment the next `Top` placement for the chart
nTop = nTop + ActiveSheet.Shapes(strChrt).Height + 20
Next NCol
End Sub
It's weird because I wrote this code before and it worked before. Now I'm trying to modify it responds through a button on a form so that it is more user friendly.
This is my old code that worked. I can't seem to see the difference.
Sub MergeWorkbooks_V2()
Dim NCol As Long
Dim NRow As Long
Dim LastRow As Long
Dim LastCol As Long
Dim NSheets As Long
Dim LastSheet As Integer
' Modify this folder path to point to the files you want to use.
FolderPath = "C:\Users\VCHU\Desktop\Sample_LTE_0021_3-1261\"
FileName = Dir(FolderPath & "Sheet1.xlsx")
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "Sheet1.xlsx")
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
' Number of sheets
LastSheet = 2
Dim nLeft As Long, nTop As Long
Dim strChrt As String
For NSheets = 1 To LastSheet
Sheets(NSheets).Select
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
nLeft = 20: nTop = 0
For NCol = 1 To LastCol
Range(Cells(1, NCol), Cells(LastRow, NCol)).Select
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Range(Cells(1, NCol), Cells(LastRow, NCol))
strChrt = Trim(Replace(ActiveChart.Name, ActiveSheet.Name, ""))
ActiveSheet.Shapes(strChrt).Left = nLeft
ActiveSheet.Shapes(strChrt).Top = nTop
'~~> Increment the next `Top` placement for the chart
nTop = nTop + ActiveSheet.Shapes(strChrt).Height + 20
Next NCol
Next NSheets
End Sub
DataProcessing_Tool.xlsm
Bookmarks