Consulting

Results 1 to 4 of 4

Thread: Set a variable based on another variable

  1. #1
    VBAX Regular
    Joined
    Mar 2013
    Posts
    80
    Location

    Set a variable based on another variable

    Hello
    My Excel workbook contains a sheet per month (doing HR stuff based on hire date). In order to have only one macro for all workbook I need my calculation to be based on variable. I understand the theory but I am a beginner and don't know a better way to avoid all the "if" you can see below :
    Sheet name Representing month 1st day month value
    1 01/01/2013 41275
    2 01/02/2013 41306
    3 01/03/2013 41334
    4 01/04/2013 41365
    5 01/05/2013 41395
    6 01/06/2013 41426
    7 01/07/2013 41456

    Dim Mysheet as integer
    Dim Myvalue as integer

    MySheet=ActiveSheet.Name

    If mysheet=1 then my value=41275
    if mysheet=2 then my value=41306
    etc..

    THANKS!

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Hi. If you're always working with "ActiveSheet" and ActiveSheet name is a number from 1 to 12...
    Dim MyValue As Long      MyValue = DateSerial(Year(Date), CInt(ActiveSheet.Name), 1)
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Regular
    Joined
    Mar 2013
    Posts
    80
    Location
    Quote Originally Posted by mancubus View Post
    Hi. If you're always working with "ActiveSheet" and ActiveSheet name is a number from 1 to 12...
    Dim MyValue As Long      MyValue = DateSerial(Year(Date), CInt(ActiveSheet.Name), 1)
    Thanks! I love when you can reduce to one single line and learn something new at the same time!.. I'll try that right now..

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you're most welcome.i'm glad it helped.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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