Hello,
I am creating a macro to open a file and create a chart automatically. I started by using Record Macro to get the general steps I needed. I then added code at the beginning to have the user select the file (thanks to TeachExcel for showing me how to do that!). Now when I run it, I get an Object required error at a step in the recorded part when I changed the y-axis label. The bolded line is where the error is occuring. Would appreciate some help understanding what I'm doing wrong!
thanks!
Sub GenerateChart()
'
' GenerateChart Macro
'
' First get the filename from the user
userSelectedFile = Application.GetOpenFilename
' Check if the user selected a file.
' - False is returned if the user did not select a file.
If userSelectedFile = False Then
' If here, means that no file was selected.
MsgBox "No file selected."
Else
' If here, a file was selected and it will be contained within the variable that
' we used above, userSelectedFile.
' Open the file here or do whatever you want with it.
MsgBox "File selected: " & userSelectedFile
'
Workbooks.OpenText Filename:=userSelectedFile _
, Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, _
Comma:=True, Space:=True, Other:=True, OtherChar:="]", FieldInfo:= _
Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7 _
, 1), Array(8, 1), Array(9, 1), Array(10, 1)), TrailingMinusNumbers:=True
Range("A:A,C:C").Select
Range("C1").Activate
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("LOG_TDK!$A:$A,LOG_TDK!$C:$C")
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Seconds"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Seconds"
With Selection.Format.TextFrame2.TextRange.Characters(1, 7).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 7).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 10
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.Axes(xlCategory).AxisTitle.Select
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Days"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Days"
With Selection.Format.TextFrame2.TextRange.Characters(1, 4).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 4).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 10
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.ChartTitle.Select
ActiveChart.ChartTitle.Text = "Therapy by Days"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Therapy by Days"
With Selection.Format.TextFrame2.TextRange.Characters(1, 15).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 15).Font
.BaselineOffset = 0
.Bold = msoFalse
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(89, 89, 89)
.Fill.Transparency = 0
.Fill.Solid
.Size = 14
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Spacing = 0
.Strike = msoNoStrike
End With
Range("P21").Select
ActiveWorkbook.SaveAs Filename:= _
userSelectedFile, FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
End If
End Sub