我想绘制一些数据,我的代码非常简单,我的想法是只有两个范围,我想要绘制的列,以实现这一点,我收到四个收件箱框如下:< / p>
first = InputBox("first range", "Enter first range")
first1 = InputBox("second range", "Enter second range")
two = InputBox("second range", "Enter second range")
two1 = InputBox("second range", "Enter second range")
我的完整代码如下:
Sub ALL()
Dim ws As Worksheet
Dim rng As Range
Dim objChrt As ChartObject
Dim chrt As Chart
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
first = InputBox("first range", "Enter first range")
first1 = InputBox("second range", "Enter second range")
two = InputBox("second range", "Enter second range")
two1 = InputBox("second range", "Enter second range")
Set rng = .Range("first:first1,two:two1")
.Shapes.AddChart
Set objChrt = .ChartObjects(.ChartObjects.Count)
Set chrt = objChrt.Chart
With chrt
.ChartType = xlColumnClustered
.SetSourceData Source:=rng
End With
End With
End Sub
我的代码看起来运行良好,但是当我想在宏中运行它时,我输入以下范围
A2
A4
B2
B4
在相应的框中,我按回车键,我在以下行中收到错误:
设置rng = .Range(“first:first1,two:two1”)
我无法解决这个错误,我真的很感激任何克服这种情况的建议,谢谢你们。
答案 0 :(得分:1)
您的错误是由于将所有变量传递到双引号内,因此它们计算为字符串。
在InputBox中稍作更改,您可以从工作表本身中选择整个范围。
Set rng = Application.InputBox(prompt:="Test", Type:=8)
请参阅:https://msdn.microsoft.com/en-us/library/office/ff839468.aspx
Sub test()
Dim rng As Range
first = "A2"
first1 = "A4"
two = "B2"
two1 = "B4"
'/Error
Set rng = Range("first:first1,two:two1")
'/Valid
Set rng = Range(first & ":" & first1, two & ":" & two1)
'/ Robust way
Set rng = Application.InputBox(prompt:="Sample", Type:=8)
End Sub