这个问题可能很容易解决,但我无法弄清楚如何做到这一点,快速网络搜索没有任何结果。所以这是我的代码:
Option Explicit
'Description: This macro is used to number plot all specimens into the stress-strain curve since this has become a task that
'has to be done very frequently
Sub PlotAllSpecimens_Tensile()
Dim ws As Worksheet, wb As Workbook
Dim xrng As Range, yrng As Range, namerng As Range
Dim CH As Chart, CHcond As Chart, CHdry As Chart
Dim Material As String, state As String, Temperatur As String, name As String
Dim i As Integer, j As Integer, idry As Integer, icond As Integer, k As Integer
Dim ser As series
Dim startrow As Integer
Set wb = ActiveWorkbook
idry = 1
icond = 1
For Each CH In wb.Charts
If CH.name = "Stress-Strain curve cond" Then
Set CHcond = CH
ElseIf CH.name = "Stress-Strain curve dry" Then
Set CHdry = CH
End If
Next CH
If Not CHdry Is Nothing Then
For Each ser In CHdry.SeriesCollection
ser.Delete
Next ser
End If
If Not CHcond Is Nothing Then
For Each ser In CHcond.SeriesCollection
ser.Delete
Next ser
End If
For Each ws In wb.Worksheets
If ws.name <> "Start" And ws.name <> "Auswertung" And ws.name <> "Zusammenfassung" Then
i = 1
For k = 1 To 15
If ws.Cells(k, 5 * i - 4) = "Material" Or ws.Cells(k, 5 * i - 4) = "Werkstoff" Then
Material = ws.Cells(k, 5 * i - 3).Value
ElseIf ws.Cells(k, 5 * i - 4) = "Temperatur" Then
Temperatur = ws.Cells(k, 5 * i - 3).Value
ElseIf ws.Cells(k, 5 * i - 4) = "Zustand" Then
state = ws.Cells(k, 5 * i - 3).Value
End If
Next k
While Not IsEmpty(ws.Cells(i * 5 - 4).Value)
name = Material & "_" & i & ", " & state & ", " & Temperatur
Set namerng = ws.Cells(1, 5 * i - 1).End(xlDown).Offset(-1, -1)
namerng.Value = name
startrow = ws.Cells(1, 5 * i - 1).End(xlDown).Row
Set xrng = Range(ws.Cells(startrow, 5 * i - 2), ws.Cells(startrow, 5 * i - 2).End(xlDown))
Set yrng = Range(ws.Cells(startrow, 5 * i - 1), ws.Cells(startrow, 5 * i - 1).End(xlDown))
If Not (CHdry Is Nothing) And state = "dry" Then
CHdry.SeriesCollection.NewSeries
CHdry.SeriesCollection(idry).XValues = xrng
CHdry.SeriesCollection(idry).Values = yrng
CHdry.SeriesCollection(idry).name = ??????????
CHdry.SeriesCollection(idry).Border.ColorIndex = 42 + ws.Index Mod 5
idry = idry + 1
End If
If Not (CHcond Is Nothing) And state = "conditioned" Then
CHcond.SeriesCollection.NewSeries
CHcond.SeriesCollection(icond).XValues = xrng
CHcond.SeriesCollection(icond).Values = yrng
CHcond.SeriesCollection(icond).Border.ColorIndex = 42 + ws.Index Mod 5
CHcond.SeriesCollection(icond).name = ???????
icond = icond + 1
End If
i = i + 1
Wend
End If
Next ws
End Sub
????? 标志着这个问题。我想将我的系列命名为单元格“namerng”的值,所以如果我稍后更改此单元格,则图表中的名称将更新。这可以通过选择单元格作为名称范围在excel中手动完成。如果我使用:
CHcond.SeriesCollection(icond).name = namerng.value
结果将是正确的,但在更改namerng的值后不会更改。 那么如何使用VBA将一个单元格的值作为系列名称引用?
答案 0 :(得分:0)
我明白了:
CHcond.SeriesCollection(icond).name = "='" & ws.name & "'!" & namerng.Address
不是很优雅,但它有效。