+ Reply to Thread
Results 1 to 4 of 4

Write Macro Code to Change based on User Choice

  1. #1
    Registered User
    Join Date
    06-15-2014
    Posts
    9

    Write Macro Code to Change based on User Choice

    Hello everyone, I am relatively new to coding and have been learning as I go so forgive me if there is a very simple solution that I have yet to learn. So what I am trying to do is create a macro that will begin with a userform combo box giving the user an option to select a month and year. Then with this information, the rest of the macro code will change (or replace certain characters) based on this decision. For example, say the user chooses "April" "2014" in the userform combo boxes at the start of the macro, I would like the code in the rest of the macro to change to accommodate for that choice by replacing the code where there is a month to put in "April" and the places where there is a year to put in "2014." The reason I need this specifically to happen is because each month I do reporting and reference certain workbooks titled "Month Year", and if I can get that code to change based on what I choose in the userform then the macro will reference the proper workbook and find the correct information in that workbook. I am able to create the userform and the code for one month's worth of reporting but I cannot figure out how to change the code dynamically based on the decision. I'm trying to avoid making a separate macro for every possible month and year combination with this. I hope this makes sense, and I hope someone out there can give me some help on this issue (if it is even possible)

    I appreciate any help or advice anyone can offer, thanks!

  2. #2
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Write Macro Code to Change based on User Choice

    Hi Steven,

    Yes this can be done and its done through variables.

    if your code shows a message box then:

    Please Login or Register  to view this content.
    If you have a sample workbook or code share and I can help you more.

    Thanks

  3. #3
    Registered User
    Join Date
    06-15-2014
    Posts
    9

    Re: Write Macro Code to Change based on User Choice

    Thank you for your response. That has gotten me closer... Below is a sample of my code that contains an error because it is not referencing the proper excel document to find the source data. To help make sense of this the second arguement in each =GetPivotdata function should be "Copy of Month Year.xlsx" (i.e. Copy of March 2012.xlsx) and the last arguement should be the date in numbers (i.e. Date(yyyy,m,1). The userform runs but when it gets to finding the proper place to find the data it searches elsewhere (even with the source data workbook opened).

    ActiveCell.FormulaR1C1 = _
    "=GETPIVOTDATA(""Sum of A"",'[Copy of UserForm1.MonthComboBox.Value & UserForm1.YearComboBox.Value.xlsx]by Channel'!R4C1,""Month"",DATE(UserForm1.YearComboBox.Value,Monthnm(UserForm1.MonthComboBox.Value),1))"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = _
    "=GETPIVOTDATA(""Sum of B"",'[Copy of UserForm1.MonthComboBox.Value & UserForm1.YearComboBox.Value.xlsx]by Channel'!R4C1,""Month"",DATE(UserForm1.YearComboBox.Value,Monthnm(UserForm1.MonthComboBox.Value),1))"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = _
    "=GETPIVOTDATA(""Sum of C"",'[Copy of UserForm1.MonthComboBox.Value & UserForm1.YearComboBox.Value.xlsx]by Channel'!R4C1,""Month"",DATE(UserForm1.YearComboBox.Value,Monthnm(UserForm1.MonthComboBox.Value),1))"
    ActiveCell.Offset(1, 0).Range("A1").Select
    End Sub



    I appreciate any help on what my error might be, thanks!

  4. #4
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Write Macro Code to Change based on User Choice

    Can you use code tags when posting ? It makes the code easier to read. Just highlight it and hit the "#" button on the little toolbar. Or type [CODE ]Place Your Code Here[ /CODE]

    Thanks

+ 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. Can I write macro to change Code in a Userform? If so, how?
    By Prexcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-18-2014, 09:39 AM
  2. How to write VBA code that waits for the user to select a chart?
    By geophysicist in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-22-2013, 08:33 PM
  3. Message box that controls running a macro depending on user choice.
    By msouma in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2013, 08:34 AM
  4. Code/Macro to populate table based on user input.
    By galvinpaddy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-08-2011, 07:50 AM
  5. [SOLVED] Start Macro after user selects a choice from a pick list
    By Mathew in forum Excel General
    Replies: 2
    Last Post: 08-17-2006, 10:30 AM

Tags for this Thread

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