Excel VBA runtime error 1004 only with names that begin with 'c'

旧巷老猫 提交于 2019-12-04 14:07:12

MSDN indicates you can't use the letters "C" or "R" (upper/lower) as names. I think there is a bug relating to when this letter is the first letter in the Name is either R or C (or r or c) which I have replicated your error.

Try using the Name's address in a string concatenated to your formula, like so:

Sub ChtSeries()
Dim objChartWGain As Chart
Dim objSeries As Series
Dim nmAddress As String
Dim n As Name

Set objChartWGain = Charts("W Gain")

'Replace with your Name definition:'
Set n = ActiveWorkbook.Names.Add("Chart_Series_W_Gain_AAPL", Sheets("Sheet2").Range("A2:A4"))

'Turn the Name's address in to a usable string:'
nmAddress = Replace(n.RefersTo, "=", vbNullString)

Set objSeries = objChartWGain.SeriesCollection(1)
objSeries.Formula = "=SERIES(""AAPL"",," & nmAddress & ",1)"

End Sub

Info from MSDN here:

http://office.microsoft.com/en-us/excel-help/define-and-use-names-in-formulas-HA102749565.aspx#_Learn_about_syntax

You cannot use the uppercase and lowercase characters "C", "c", "R", or "r" as a defined name, because they are all used as a shorthand for selecting a row or column for the currently selected cell when you enter them in a Name or Go To text box.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!