Show Custom View Using VBA Varaible

XLMonst

New Member
Joined
Feb 22, 2018
Messages
2
Hi,

Problem, I wanted to consolidate my VBA so every button used the same macro.

The below doesn't seem to work when I use the variable ButtonText to call the custom view, but works fine if I type it manually, eg. "Total" which is one of the views I can to show.

Code:
'Define variables
    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim ButtonText As String
    
    ButtonText = ws.Shapes(Application.Caller).TextFrame.Characters.Text
    
'Apply custom view - to pull print layout etc.
   ThisWorkbook.CustomViews(ButtonText).Show

I also tried the below which didn't work.

Code:
   ThisWorkbook.CustomViews.Item(ViewName:=ButtonText).Show

My work around was the below which works as I want it but it's more lines. Not the end of the world just like having short macros :).

Code:
    For Each CustomView In ThisWorkbook.CustomViews
        If CustomView.Name = ButtonText Then
            CustomView.Show
            Exit For
        End If
    Next CustomView

So, why don't the first 2 examples work?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
NVM!!! MORON MODE! the button text had a space in front of it :eek:, so the final option simply never gave an error....
 
Upvote 0

Forum statistics

Threads
1,215,379
Messages
6,124,609
Members
449,174
Latest member
ExcelfromGermany

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top