+ Reply to Thread
Results 1 to 6 of 6

ComboBox.Value reference to worksheet name and reference pivot table from another workbook

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

    ComboBox.Value reference to worksheet name and reference pivot table from another workbook

    Hello everyone, I have been looking everywhere trying to find a solution to my vba macro and have yet to find something that will work. So what I am trying to do is run userform containing two comboboxes (which I have put together and seems to be working fine), that after completion of the userform will begin another macro containing code attempting to reference a pivot table on a separate workbook (the workbook name is variable based on the userform filled out before (i.e. January2014.xlsx), which I am having difficulty coding correctly to make the reference work) to draw data from it. I am fairly new to coding so this complex idea of jumping around workbooks using pivot tables and using variables based on the userform to reference is very confusing. Below is the macro referenced after the user form is completed. The embedded DATE function is my attempt at making the pivot table reference change with the combobox choice (date function for april works with DATE(2014,4,1)).

    Any help would be greatly appreciated, as I have been stuck on this for a while. Thanks!


    ActiveCell.FormulaR1C1 = _
    "=GETPIVOTDATA(""Sum of A"",'[UserForm1.MonthComboBox.Value & UserForm1.YearComboBox.Value]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"",'[UserForm1.MonthComboBox.Value & UserForm1.YearComboBox.Value]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 Ap"",'[UserForm1.MonthComboBox.Value & UserForm1.YearComboBox.Value]by Channel'!R4C1,""Month"",DATE(UserForm1.YearComboBox.Value,Monthnm(UserForm1.MonthComboBox.Value),1))"
    ActiveCell.Offset(1, 0).Range("A1").Select
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox.Value reference to worksheet name and reference pivot table from another work

    Like I said, you need to incorporate the values from the comboboxes in the formula using concatenation.

    For example.
    Please Login or Register  to view this content.
    PS Can you use code tags when posting code?
    If posting code please use code tags, see here.

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

    Re: ComboBox.Value reference to worksheet name and reference pivot table from another work

    Sorry about that, I will do that in the future. All but one thing worked... the Monthnm function did not come out as desired? Maybe there is a different function out there I need to use? Need to convert a word form of month (i.e. March) to the number of month (i.e. 3). Do you know how I could make that work?

    Thanks!

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox.Value reference to worksheet name and reference pivot table from another work

    Do you actually have a function called Monthnm?

    IF you don't you could us Excel's Month function.
    Please Login or Register  to view this content.
    Or assuming the months are listed in chronlogical order then you can use it's ListIndex to get the month number.
    Please Login or Register  to view this content.

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

    Re: ComboBox.Value reference to worksheet name and reference pivot table from another work

    Thank you so much for your help! The first formula did not work for me (not sure why), and so I just decided on the ListIndex one. When I used the ListIndex, it always came back minus 1 of what I wanted so I just included +1 after and it works fine. So the ListIndex must count the first one in the list as zero meaning January = 0. Again I really appreciate all your help!

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox.Value reference to worksheet name and reference pivot table from another work

    Oops, I meant to add the +1, honest.

    Yes you are right, the ListIndex of a listbox is zero-based which means the first item's index is 0, the second 1 and so on.

+ 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. Reference Pivot table in another worksheet
    By dssrun209 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-21-2011, 12:19 PM
  2. Can I reference an entire worksheet in Pivot Table?
    By SEMMatt in forum Excel General
    Replies: 2
    Last Post: 11-09-2011, 12:37 PM
  3. Pivot Table reference 2nd worksheet
    By plasticus in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 03-22-2011, 10:20 AM
  4. Combobox values reference to another workbook
    By Aussie_Striker in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-15-2007, 02:13 AM
  5. [SOLVED] How do I use a pivot table value to reference a worksheet
    By neoschenker in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 01-20-2005, 11:06 AM

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