Results 1 to 4 of 4

Set active Worksheet from a ComboBox value.

Threaded View

  1. #1
    Registered User
    Join Date
    01-18-2016
    Location
    Manchester, England
    MS-Off Ver
    Office 10
    Posts
    2

    Set active Worksheet from a ComboBox value.

    Hi this is my first post, I've not coded for some time until recently so my vba Knowledge may be a little rusty and not upto date with the newest versions of Excel.

    I have a userform with dependant comboboxes, textbox, date etc that works great, but the sheet names are hardcoded. (see below code)

    Private Sub cmbSubmit_Click()
    
    Dim thissheet As Worksheet
    
    If cmbType.Value = "Fruit" Then
       Set thissheet = ThisWorkbook.Sheets("Fruit Exp") ' ("Fruit Exp") sheet To be write to if cmbType.value = Fruit
    
    ElseIf cmbType.Value = "Vegetable" Then
       Set thissheet = ThisWorkbook.Sheets("Vegetable Exp")
    
    ElseIf cmbType.Value = "Fish" Then
       Set thissheet = ThisWorkbook.Sheets("Fish Exp")
    
    ElseIf cmbType.Value = "Other" Then
       Set thissheet = ThisWorkbook.Sheets("Other Exp")
    
    Else
        MsgBox "Please choose an Expense Type!", vbOKOnly + vbExclamation
      
    End If
    
    ' More code that writes to the sheet.
    
    End Sub

    I wish to change the hard coding ("Fruit Exp") that sets the active sheet to the same name as selected in the cmbType .

    Should I try case function?


    I hope I've explained the problem clearly enough.

    Thank you in advance for any help you can offer.

    Cheers

    MaxSheets

    Update.

    I've Changed this code line and similar, {ThisWorkbook.Worksheets("Data").Range("A1")}
    this eliminates one of the hard coded name values.

    If cmbType.Value = ThisWorkbook.Worksheets("Data").Range("A1") Then
       Set thissheet = ThisWorkbook.Sheets("Fruit")
    Last edited by MaxSheets; 01-18-2016 at 06:18 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 4
    Last Post: 07-03-2015, 04:27 AM
  2. On ActiveX ComboBox Change, switch focus to the active worksheet
    By ericbartha in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-29-2015, 10:54 AM
  3. ComboBox Selection Caused Cells to be copied to active worksheet
    By octalman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-07-2015, 02:12 PM
  4. Replies: 1
    Last Post: 07-07-2014, 09:19 AM
  5. Selecting an active worksheet AND an active table that keep changing names!
    By brucemc777 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-12-2014, 10:22 PM
  6. combobox cannot read active worksheet
    By craig2793 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-11-2013, 02:59 AM
  7. Macro to edit active chart on active worksheet
    By shlurpee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-07-2011, 04:58 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