+ Reply to Thread
Results 1 to 6 of 6

Variable and constant question.

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Variable and constant question.

    Hi guys,

    Just wondering if it's possible to declare 1 variable for multiple constant or string?

    Example, instead of doing:
    strVar= "Dog"
    strVar2= "Cat"
    strVar3="Rat"
    If wsData.Cells(1,1) = strVar or wsData.Cells(1,1) = strVar2 or wsData.Cells(1,1) = strVar3 then
    'Blabla
    End if

    Is there a way to declare strVar for all the string, so the code can be as simple as:
    If wsData.Cells(1,1) = strVar then
    'Bla
    End if

    Any ideas?

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Variable and constant question.

    Can you use something like this?

    Please Login or Register  to view this content.

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Variable and constant question.

    you could make the strings an array, and check if the cell is part of the array:

    Please Login or Register  to view this content.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Variable and constant question.

    one way could be to declare strVar with concatenation of all the strings. and then Use instr to find the wsData.Cells(1,1) in it.
    like
    HTML Code: 
    please confirm instr syntax. I am a bit unsure of it. But the concept would definitely work.

    or you can split the "Vikas,Calvinle,Excelforum" to make it an array and the use match function to find the wsData.Cells(1,1).value

    HTML Code: 
    I hope this helps
    Regards,
    Vikas Gautam
    Excel-buzz.blogspot.com

    Excel is not a matter of Experience, its a matter of Application.

    Say Thanks, Click * Add Reputation

  5. #5
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Variable and constant question.

    In my example, I gave a single word, but what if it's a sentence?

    My purpose of doing this is because I want to be able to add more value to a variable without the need to find the wsData.cells and add more "or".

    For example, the reports has the wording:
    Client report
    Employer report
    Product price

    So I want to add all them to the variable.
    If in the future, the report add another parameter, example, Machine Cost.

    I will only need to add this wording into the variable without modifying the whole macros..

  6. #6
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Variable and constant question.

    You may try this:-
    --> Use a helper column in the excel sheet say Column X where you could list all the strings or sentences.
    for eg. you listed
    X1= Client report
    X2= Employer report
    X3= Product price

    and then use this column in the macro to be searched in for wsData.cells(1,1).value

    like this
    Please Login or Register  to view this content.
    Try Columns("X") instead of Columns("X").value, if the later didn't worked.

    this should work. you won't need to change macro every time as you could add or change values in the column X.
    Last edited by Vikas_Gautam; 06-26-2015 at 05:07 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Calling and Excel Constant by A Variable
    By diakonos1984 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-10-2014, 09:34 AM
  2. Call a constant/variable by creating another variable
    By pienaaj in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-12-2013, 10:36 AM
  3. Sum of constant + variable = constant
    By neurogirl900 in forum Excel General
    Replies: 3
    Last Post: 12-19-2011, 11:02 PM
  4. How to average one constant # to variable #s? A1:A2 A1:A3 A1:A4
    By Colfax in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-01-2006, 10:55 PM
  5. [SOLVED] Variable Acting Like a Constant?
    By George Boynton in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-14-2005, 12:05 PM

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.6.0 RC 1