【问题标题】:Excel VBA Chart Maximum Scale ErrorExcel VBA图表最大比例错误
【发布时间】:2017-08-17 02:33:38
【问题描述】:

此代码有两个问题,我需要一些帮助,我是 vba 新手,不知道问题的原因是什么。

第一个问题是,当Me.Range("L7") = Format(Me.Range("C4")....) 被格式化时,它会将日期置于美国格式“mm/dd/yyyy”中。但是,下一行 Me.Range("L8") 是否放入正确的“dd/mm/yyyy”格式?

第二个问题是,当我更改图表最大轴时,出现类型不匹配错误,但最小轴比例更改工作正常吗?

任何帮助表示赞赏。

Private Sub CommandButton1_Click()
answer = MsgBox("This will prepare the workbook for the next month, are you sure?", vbYesNo)
If answer = vbNo Then Exit Sub

Range("c34") = "=DATE($B$2,$A$2,A34)" 'enters formula into cell c34
Range("a2") = Month(Date) - 1 'changes month to last month
Range("a3") = Year(Date)
If Month(Date) - 1 <> Month(Range("c34")) Then
    Range("C34").Clear 'checks if last date in column is in same month, if not, clear
End If

myLR = ThisWorkbook.Sheets("Data Input").Cells(Rows.Count, 3).End(xlUp).Row 'grabs date in last row
Me.Range("L7") = Format(Me.Range("c4"), "dd/mm/yyyy") 'gets start date of month and formats it
Me.Range("L8") = Format(Me.Cells(myLR, 3).Value, "dd/mm/yyyy") 'gets last date of month and formats it
Range("K7") = "First Day of Month"
Range("K8") = "Last Day of Month"

'Chart section
    Sheets("Site 5").Select
    ActiveChart.ChartArea.Select
    ActiveChart.Axes(xlValue).MajorGridlines.Select
    ActiveChart.Axes(xlCategory).MinimumScale = Sheets("data input").Range("L7")
    ActiveChart.Axes(xlCategory).MaximumScale = Sheets("data input").Range("L8")
    ActiveChart.Axes(xlCategory).Select
    Selection.TickLabels.NumberFormat = "d/mm/yyyy"
End Sub

【问题讨论】:

    标签: vba excel charts axis


    【解决方案1】:

    不适合用格式化函数改变单元格的值。 最好通过 NumberFormatLocal 更改单元格的格式。

    Private Sub CommandButton1_Click()
    answer = MsgBox("This will prepare the workbook for the next month, are you sure?", vbYesNo)
    If answer = vbNo Then Exit Sub
    
    Range("c34") = "=DATE($B$2,$A$2,A34)" 'enters formula into cell c34
    Range("a2") = Month(Date) - 1 'changes month to last month
    Range("a3") = Year(Date)
    
    If Month(Date) - 1 <> Month(Range("c34")) Then
        Range("C34").Clear 'checks if last date in column is in same month, if not, clear
    End If
    
    myLR = ThisWorkbook.Sheets("Data Input").Cells(Rows.Count, 3).End(xlUp).Row 'grabs date in last row
    'Me.Range("L7") = Format(Me.Range("c4"), "dd/mm/yyyy") 'gets start date of month and formats it
    Me.Range("L7") = Me.Range("c4")
    Me.Range("L7").NumberFormatLocal = "dd/mm/yyyy"
    'Me.Range("L8") = Format(Me.Cells(myLR, 3).Value, "dd/mm/yyyy") 'gets last date of month and formats it
    Me.Range("L8") = Me.Cells(myLR, 3).Value
    Me.Range("L8").NumberFormatLocal = "dd/mm/yyyy"
    
    Range("K7") = "First Day of Month"
    Range("K8") = "Last Day of Month"
    
    'Chart section
        Sheets("Site 5").Select
        ActiveChart.ChartArea.Select
        ActiveChart.Axes(xlValue).MajorGridlines.Select
        ActiveChart.Axes(xlCategory).MinimumScale = Sheets("data input").Range("L7")
        ActiveChart.Axes(xlCategory).MaximumScale = Sheets("data input").Range("L8")
        ActiveChart.Axes(xlCategory).Select
        Selection.TickLabels.NumberFormat = "d/mm/yyyy"
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 1970-01-01
      相关资源
      最近更新 更多