VBA 使用范围作为消息框的图表

VBA Chart using a Range as a messagebox

我正在尝试创建图表,但有一个弹出框要求您select范围,因为这每个月都会更改。

我下面的代码要求一个范围然后它不创建图表。 任何帮助都会有帮助

Sub test()
'Set up the variables.
Dim rng As Range

'Use the InputBox dialog to set the range for MyFunction
Set rng = Application.InputBox("Range:", Type:=8)
  Exit Sub
 'Call MyFunction
ActiveCell.Value = MyFunction(rng)
End Sub

Function MyFunction(rng As Range) As Double
MyFunction = ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=rng
End Sub
End Function

尝试像这样删除 Exit SubEnd Sub

Sub test()
    'Set up the variables.
    Dim rng As Range

    'Use the InputBox dialog to set the range for MyFunction
    Set rng = Application.InputBox("Range:", Type:=8)

     'Call MyFunction
    ActiveCell.Value = MyFunction(rng)
End Sub

Function MyFunction(rng As Range) As Double

    MyFunction = ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.SetSourceData Source:=rng
End Function

第一个退出子阻止 test 调用 MyfunctionEnd Sub 在一个函数中没有 earthly business

RefEdit 控件可能会提供更好的用户体验,用于选择范围。它比输入框 return 无效范围的可能性更小。