简体   繁体   中英

VBA Application-defined or object-defined error in macro using concatenated strings with user defined function

I'm trying to use a user-defined-function (setChartAxis() from here: https://exceloffthegrid.com/chart-axis-min-mix/ ) to dynamically update the axes of many plots. It's part of a large macro but I have reproduced the relevant parts in the simple code below and cannot get it to work. I want something that works like the SUM function so that the chart axes will update as I work. I can get the SUM function to work fine and I feel like the syntax in the other line is basically identical but every time I run it I get the following error:

Run-time error '1004':

Application-defined or object-defined error

I feel like it should be an easy fix but I can't find the solution in any other question.

Sub Test()

    Range("A2").Value = "=SUM(B" & 1 & ":B" & 2 & ")"

    Range("A3").Value = "=setChartAxis('Sheet1','Chart 1','Max','Category','Primary'," & A2 & ")"

End Sub

To insert a quote ( " ) into a cell from a string, the quote needs to be doubled ( "" ) to prevent Excel thinking it's marking the end of that string.

You're going to want something like this:

Range("A3").Formula = "=setChartAxis(""Sheet1"",""Chart 1"",""Max"",""Category"",""Primary""," & "A2" & ")"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM