Avatar of Seamus2626
Seamus2626
Flag for Ireland asked on

Enter Formula on button click

Hi,

I have some buttons named e.g Button 24, Button 23

I want some code that will enter into cell U23 = G2

So if button 24 is clicked U23 = G2
     if button 23 is clicked U23 = H2
     if button 22 is clicked U23  = I2

Thanks!
Microsoft ExcelVisual Basic Classic

Avatar of undefined
Last Comment
Seamus2626

8/22/2022 - Mon
Rory Archibald

What kind of buttons - Form or ActiveX? Also do you mean you want the actual text "G2", or the value in cell G2?
Seamus2626

ASKER
Its the form buttons and I want the value from G2

Thanks Rory!
Rob Henson

Does it have to be buttons?

How about a drop down selection that allows you the user to choose options 1 to 3 or values 22 to 24 or whatever is required. This will then feed a value into a specific cell, lets assume B2.

You can then have the formula in U23 as:

=IF(B2="Option 1",I2,IF(B2="Option 2",H2,IF(B2="Option 3",G2,"No Option Chosen")))

Thanks
Rob H
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Rory Archibald

You mean Form buttons on a sheet, rather than buttons on a userform, yes? (just to be sure)
Seamus2626

ASKER
Form buttons on a sheet
Seamus2626

ASKER
Thanks Rob, but due to multiple other things happening within a macro, that wouldn't be possible, the formula would have to activate when the button is clicked. Thanks
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rob Henson

I take it you mean that "Button 23" or whichever is clicked would also have other actions and not just populating cell U23 with the required formula link.

Do your Buttons all have separate macros? If they all do pretty much the same thing apart from different values into that cell, it might be possible to combine the various macros into one and then use the "Case Select" statement to determine the variations between different buttons.

As always, a sample file would help.

Thanks
Rob
Rory Archibald

Assuming the same macro is called by all the buttons, you could just add this to it:

    Dim sCaller               As String
    Dim lColumn               As Long
    sCaller = Application.Caller
    lColumn = 31 - Val(Trim(Replace(sCaller, "Button", "", , , vbTextCompare)))
    Range("U23").FormulaR1C1 = "=R2C" & lColumn

Open in new window

Seamus2626

ASKER
Thanks Rory, im getting Type Mismatch on

sCaller = Application.Caller

This is my full sub

Seamus

Sub Step_Thru_Managed()
    Dim slItem As SlicerItem
    Dim i As Long
    Dim sCaller As String
    Dim lColumn As Long
    
    Application.ScreenUpdating = False
    
    Sheets("PCM Sales Manager Dashboard").Select
    

    sCaller = Application.Caller
    lColumn = 31 - Val(Trim(Replace(sCaller, "Button", "", , , vbTextCompare)))
    Range("U23").FormulaR1C1 = "=R2C" & lColumn
    
  With ActiveWorkbook.SlicerCaches("Slicer_PCM_Global_Sales_Manager")
        '--deselect all items except the first
        .SlicerItems(1).Selected = True
        For Each slItem In .VisibleSlicerItems
            If slItem.Name <> .SlicerItems(1).Name Then _
                slItem.Selected = False
                
        Next slItem
      Call Print_PDF_Managed
        '--step through each item and run custom function
        For i = 2 To .SlicerItems.Count
            .SlicerItems(i).Selected = True
            .SlicerItems(i - 1).Selected = False
            Call Print_PDF_Managed
        Next i
    End With
    Application.ScreenUpdating = True
End Sub

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
Rory Archibald

Is that routine assigned as a macro to the buttons?
Seamus2626

ASKER
So button 23 has Step_Thru_Managed() assigned to it

So yes, one of the buttons, kicks that routine off

Thanks
Rory Archibald

Then you shouldn't get that error when you click Button 23. Can you post a workbook?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Seamus2626

ASKER
Attached

Thanks
EE.xlsm
Seamus2626

ASKER
My word, I have no idea why that is 34mb
Seamus2626

ASKER
Heres a light version

So I need on clicking the button (or starting the routine) the code says

U23 =G2

So it could be placed in the onclick part?

Each button has a separate routine, so on clicking that button, you would say specifically - U23 = G2

On clicking the next button, its specifically U23 = I2 etc etc
Book3.xlsx
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Seamus2626

ASKER
Sorry Rob, missed your next comment, ya there is a sample file but its bloody big!
Seamus2626

ASKER
Were you guys able to open the file?

Thanks
Rory Archibald

I can't see any buttons in your first file and there clearly isn't any code in the second file as it's an .xlsx. Care to try again?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Seamus2626

ASKER
if you can open the file EE.xlsm, the buttons are on the top of the sheet M2 - W6
Seamus2626

ASKER
ID: 40682946

The buttons are on that file Rory when I open, and the code behind it
Rory Archibald

Excel tries to recover the content, and there are no buttons when it does. I'll try another version.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Rory Archibald

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Seamus2626

ASKER
Im a donkey, I was stepping through it in the routine as opposed to clicking the button.

Thanks Rory!
Seamus2626

ASKER
Rory, I just checked if I click button number 2, it moves along to =H2

But because that's booked, it must move to G3 as you can see the way the spreadsheet is set up

So all the buttons ive listed below and what there corresponding value needs  to be

I can repost as a new q if this is a different q

thanks
Seamus

PDF Country (Managed) = G2
PDF Country (Booked) = G3
PDF Global SM (Managed) = H2
PDF Regional SM = I2
PDF in-country! (booked) = J2
PDF Sector (booked) = K3
PDF Sector (Managed) = K2