我试图将工作表变量声明为" Dim D作为工作表,设置D =工作表(" shdata")并且我得到运行时错误9,下标超出范围。我不知道为什么这不起作用,我的代码有问题吗?
VBA代码:
Sub unicornhorn()
'Generates user friendly graphs from raw unicorn input data
Dim columns As Integer
Dim rows As Integer
Dim Outputs(50) As String
Dim LastRow As Integer
Dim xaxis As Range
Dim yaxis As Range
Dim nLeft As Double: nLeft = 20
Dim nTop As Double: nTop = 20
Dim start As Double: start = start + 2
Dim finish As Double: finish = finish + 2
Dim Curves(14) As String
Curves(0) = "260nm"
Curves(1) = "280nm"
Curves(2) = "214nm"
Curves(3) = "Cond"
Curves(4) = "Cond%"
Curves(5) = "Conc"
Curves(6) = "pH"
Curves(7) = "Pressure"
Curves(8) = "Flow"
Curves(9) = "Temp"
Curves(10) = "Fractions"
Curves(11) = "inject"
Curves(12) = "logbook"
Curves(13) = "P960_Press"
Curves(14) = "P960_Flow"
Dim D As Worksheet
Set D = Worksheets("shdata")
'defines data range
columns = shCurves.Cells(1, shCurves.columns.Count).End(xlToLeft).Column
rows = shCurves.Cells(shCurves.rows.Count, 1).End(xlUp).Row
For Z = 0 To columns
'loops through array for different curves
Outputs(Z) = shCurves.Cells(2, Z * 2 + 1).Value
'Finds last row in column for current curve
LastRow = Cells(D.rows.Count, Z * 2 + 1).End(xlUp).Row
For k = 0 To 14
'finds curve identifyer within string
If InStr(Outputs(Z), Curves(k)) = 1 Then
'Sets curve to proper name
Outputs(Z) = Curves(k)
'Plot graph
D.Shapes.AddChart(xlColumnClustered, _
Left:=D.Range("C11").Left, _
Top:=D.Range("C11").Top, _
Width:=D.Range(Cells(1, 1), Cells(1, 15)).Width, _
Height:=D.Range("C11:C30").Height).Select
ActiveChart.SetSourceData Source:=Range("A2:B11")
Exit For
End If
Next k
Next Z
End Sub