+ Reply to Thread
Results 1 to 4 of 4

VBA Control Button Help

Hybrid View

  1. #1
    Registered User
    Join Date
    10-20-2009
    Location
    Manila, Philippines
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    7

    VBA Control Button Help

    Hi,

    Here is what i am currently using as code for one of my 56 or so buttons:

    Private Sub CommandButton8_Click()
    
    Dim i As String
    i = "Mathematics"
    
        Sheets("Subjects").Select
        
        On Error GoTo ErrorHandler
        
        ActiveSheet.PivotTables("PivotTable1").PivotFields("Class").CurrentPage _
            = i
         
    ErrorHandler:
        
        MsgBox i & " does not exist. Please press OK and choose another button.", vbOKOnly
            
        Sheets("Summary").Select
        
        Exit Sub
    
    End Sub
    And here are the challenges i'm getting so far:

    #1

    The message box is only supposed to pop-up if "i" is not found on the "Subjects" worksheet. However, what i am getting is the msgbox even if "i" is found or valid. Is there something wrong with my syntax or the order it was written?

    #2

    How do i type the message box content so that it reads:

    "Mathematics" does not exist. Please press OK and choose another button.

    instead of:


    Mathematics does not exist. Please press OK and choose another button.

    Please note the "" marks.

    #3

    This is just an add-on question. For worksheets with multiple buttons, is there a way to format the code so that it works for all instead of typing it separately for each "i"?


    Thanks guys. Looking forward to your help.
    Last edited by Vittorio; 10-28-2009 at 10:20 AM. Reason: problem solved

  2. #2
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: VBA Control Button Help

    hello, Try this, I will exit the sub before the error message if everthink ran fine, if not it will show the message. Also not the """" in the message itself
    Private Sub CommandButton1_Click()
    Dim i As String
    i = "Mathematics"
    
        Sheets("Subjects").Select
        
    On Error GoTo ErrorHandler
    
        ActiveSheet.PivotTables("PivotTable1").PivotFields("Class").CurrentPage _
            = i
            
        Exit Sub
        
    ErrorHandler:
    
        MsgBox """" & i & """" & " does not exist. Please press OK and choose another button.", vbOKOnly
            
        Sheets("Summary").Select
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: VBA Control Button Help

    sheet loop
    Private Sub UserForm_Click()
    Dim AllSheets As Worksheet
    For Each AllSheets In ThisWorkbook.Sheets
        MsgBox AllSheets.Name
    
    ''''' your code
    dim i as string
     i = AllSheets.name
    '' rest of code
    
    
    Next AllSheets
    End Sub

  4. #4
    Registered User
    Join Date
    10-20-2009
    Location
    Manila, Philippines
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    7

    Re: VBA Control Button Help

    Sorry for the late reply... Got caught up with work and family...

    Your suggestion worked like a charm. Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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