+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    06-07-2009
    Location
    vancouver
    MS-Off Ver
    Excel 2003
    Posts
    1

    How do you use a variable in a function in VBA?

    Hi, I'm referencing a user inputted variable in the data labels for a graph.

    To the best of my knowledge, the only way to do the data labels is to type something similar to:
    Code:
    ActiveChart.SeriesCollection(1).XValues = "='Chartname'!$A$3:$A$11"
    where 'Chartname'! is the chart sheet where the x labels are located. I can't do this... because "Chartname" is a user input and a changing variable everytime.

    I don't really know what this problem is called and thus haven't been able to search for it very well... but what is the syntax for putting a user inputted variable (via text box) into something like x labels or quotes " " ?

    I've tried everything, "=""'Chartname'""! etc all fail miserably...

    Thanks for any help.
    Last edited by Leith Ross; 06-07-2009 at 06:00 PM.

  2. #2
    Forum Guru mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,984

    Re: How do you use a variable in a function in VBA?

    Variables do not go inside " marks else they & treated as string and not a variable

    you will need something like this

    Code:
    Dim sUserVariable As String
    
    sUserVariable = "Chartname"
    
    ActiveChart.SeriesCollection(1).XValues = "='" & sUserVariable & "'!$A$3:$A$11"
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0