VBA Name a Sheet with ComboBox - vba

I have a commandButton which opens a UserForm to add a new Worksheet.
In this Userform is a ComboBox, where i can choose the machine type.
Now i want to create a new Worksheet with the Name of the machine type which was selected in the ComboBox.
This is my Code:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Sheets("Sheet1").Copy Before:=Sheets(10)
ws.Name = ComboBox1
[UserForm1].Hide
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = Array("Machine Type 1", "Machine Type 2")
End Sub
Is there a way to create a new sheet, which is a copy from Sheet1 and name it like the machine type from the ComboBox1?
Thanks for your help.

Try the code below, see notes in the code's comments:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Sheets("Sheet1").Copy Before:=Sheets(10)
Set ws = ActiveSheet ' <-- you need to set the worksheet object to the latest copied sheet
ws.Name = ComboBox1.Value '<-- now you can modify the name using the worksheet object
Me.Hide '<-- hide the user-form
End Sub

use
Private Sub CommandButton1_Click()
Sheets("Sheet1").Copy Before:=Sheets(Sheets.Count)
ActiveSheet.Name = ComboBox1.Value
Me.Hide
End Sub

Related

Excel vba when adding or deleting sheet in workbook, show/hide button in main sheet

I have a button with a simple macro that deletes certain sheets.
I'd like to show this button only when those sheets are actually there (I can use worksheets.count because I have 2 "permanent" sheets; if > 2 then I know I have a new sheet and I want to show the button to delete it if I want to).
I think I have to use "Workbook.SheetChange event" because "Worksheet.Change event" doesn't seem to work for me in this case.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim foglio_parametri As Worksheet
Set foglio_parametri = ThisWorkbook.Worksheets("PARAMETRI") 'my main sheet where I want to show/hide the button
Application.ScreenUpdating = True
If Application.Worksheets.Count > 2 Then
foglio_parametri.CommandButton2.Visible = True
Else
foglio_parametri.CommandButton2.Visible = False
End If
End Sub
Thank you very much for your time.
I will not use your names as they are in a foreign language I do not understand .
Let's assume the button you are talking about is in a sheet with the name sheet3 which also has the codename sheet3. The button itself has the name CommandButton1. Let's further assume the certain sheets you are talking about have the names sheet4 and sheet5 then I would add the following code to the workbook module
Option Explicit
Private Sub Workbook_Open()
Sheet3.HidecmdBtn
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "Sheet3" Then
Sheet3.HidecmdBtn
End If
End Sub
In the worksheet module of sheet3 you have the following code
Option Explicit
Private Sub CommandButton1_Click()
' Your code goes here
' In case your code deletes the sheets you have to hide the button
HidecmdBtn
End Sub
Sub HidecmdBtn()
Dim Sh As CommandButton
' My button is located on sheet 3 and has the name "CommandButton1"
Set Sh = CommandButton1
Dim sh1Name As String
Dim sh2Name As String
sh1Name = "Sheet4"
sh2Name = "Sheet5"
If SheetExists(sh1Name) Or SheetExists(sh2Name) Then
Sh.Visible = msoTrue
Else
Sh.Visible = msoFalse
End If
End Sub
In a normal module you have
Public Function SheetExists(SheetName As String, Optional wrkBook As Workbook) As Boolean
If wrkBook Is Nothing Then
Set wrkBook = ActiveWorkbook 'or ThisWorkbook - whichever appropriate
End If
Dim obj As Object
On Error GoTo HandleError
Set obj = wrkBook.Sheets(SheetName)
SheetExists = True
Exit Function
HandleError:
SheetExists = False
End Function

combobox with different worksheets, that changes worksheets in code

i've a question:
I would like to have a Combobox where all the worksheets are displayed. If you select a worksheet, then the worksheets in the code needs to change to the worksheet that you selected. I've tried but can't program this.
easy example:
dim WRKsheet as worksheet
set worksheet = Combobox1.value
sheets(WRKsheet).activate
Do any of you guys know how i can succeed in this?
Grts
Use the following code in a User_Form Module
Private Sub ComboBox1_Change()
' select the worksheet selected in the ComboBox1
Worksheets(ComboBox1.Value).Activate
End Sub
Private Sub UserForm_Activate()
Dim Sht As Worksheet
' show all sheets names in thisworkbook in ComboBox1
For Each Sht In ThisWorkbook.Sheets
ComboBox1.AddItem Sht.Name
Next Sht
End Sub
Note: (you need to call the form from another module, with UserForm1.Show)

In Excel, how can I set up a VBA ComboBox so that it still works if the worksheet is copied?

In Excel 2010, I can create an ActiveX ComboBox in a worksheet and configure it to give me a list of all worksheets, and it will activate whichever worksheet I select.
However, if I copy the worksheet containing the ComboBox, the new ComboBox is dead. I have to duplicate all the VBA code that makes it work, changing the labels accordingly.
Is there any way to set it up so that it works automatically if I copy the worksheet?
This is how I'm currently doing it:
Microsoft Excel Objects \ ThisWorkbook:
Private Sub Workbook_Open()
' Rebuild the list of sheets for the worksheet ComboBox.
Dim i As Long
For i = 1 To ThisWorkbook.Sheets.Count
Sheet1.ComboBox1.AddItem Sheets(i).Name
Next
End Sub
Microsoft Excel Objects \ Sheet1(Sheet1):
Private Sub ComboBox1_Change()
With Sheet1.ComboBox1
Sheets(.List(.ListIndex)).Activate
End With
End Sub
Do this in your Workbook Module:
Private Sub Workbook_Open()
Call PopulateBoxes(Sheet1)
End Sub
In a standard module, do this:
Sub PopulateBoxes(ws As Worksheet)
Dim sht As Worksheet
'Populate the combobox on sheet 1
Dim obj
Set obj = ws.OLEObjects.Item("ComboBox1").Object
obj.Clear
For Each sht In ThisWorkbook.Worksheets
obj.AddItem sht.Name
Next
End Sub
Then, in your Sheet1 module, make this:
Private Sub ComboBox1_Change()
With Me.ComboBox1
Sheets(.List(.ListIndex)).Activate
End With
End Sub
Private Sub WOrksheet_Activate()
Call PopulateBoxes(Me)
End Sub
Now, the code for each ComboBox should be functional even after copying sheets.

Referencing Most Recently Added Worksheet

I have a userform that fields the user's input to take certain actions within a workbook, one of the actions is inserting a new tab in the workbook and having the user input the new sheet's name within an input box. I want to be able to then reference this new sheet (but I won't know what someone else might name it) and to paste a chart object within the newly created sheet.
So far the adding sheet code is working fine, but any of my attempts to paste the chart range are not working. My current code for adding the worksheet is:
Private Sub MyChart_Click()
Dim Answer As String
Dim sht_name As Variant
On Error Resume Next
If Me.OptionButton2.Value = True Then
Unload Me
sht_name = InputBox("Please enter value")
If sht_name <> "" Then
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sht_name
Else
Exit Sub
End Sub
My chart lives in another worksheet ("Sheet2") and I am trying to just copy it into the newly created sheet whenever the user selects this OptionButton2 in the Userform... Any help is appreciated.
When you use the Worksheets.Add method, that sheet automatically is activated. To test this you can run this small portion of code:
Option Explicit
Private Sub SheetReference()
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Test"
Debug.Print ActiveSheet.Name
End Sub
And the output you would see is
Test
So in your case, you could declare a worksheet variable and then set the reference after you call the add method. Something like this:
Option Explicit
Private Sub MyChart_Click()
Dim Answer As String
Dim sht_name As Variant
Dim ws As Worksheet
On Error Resume Next
If Me.OptionButton2.Value = True Then
Unload Me
sht_name = InputBox("Please enter value")
If sht_name <> "" Then
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sht_name
Set ws = ActiveSheet
With ws
'Do whatever you need to do on the worksheet
End With
Else
Exit Sub
End If
End Sub

Excel VBA: ListBox-UserForm Variable creation issue

Checked other similar questions on other websites, but could not find a solution.
I am trying to generate variables from the contents of a listbox-userform. The contents in the list are the names of workbooks. The first piece of code shows you how I generate the contents of the list for your reference. The second piece of code is the one with the issue.
Private Sub CommandButton1_Click()
Dim wb As Workbook
For Each wb In Workbooks
With ListBox1
.AddItem (wb.Name)
End With
Next wb
lbl_Exit:
End Sub
I am getting a Object Required error on the For Each line below. This piece of code resides in the Userform.
Private Sub CommandButton3_Click()
Dim MainBook As Workbook
Dim ListBox1 As ListBox
For Each Item In ListBox1
If Item.Name Like "Aggregate" Then
Item.Select
Set MainBook = Selection
End If
Next
End Sub
Note: I read that Item is a property of a ListBox which is where I got that from.
Is this what you are trying?
Private Sub CommandButton1_Click()
Dim wb As Workbook
For Each wb In Workbooks
With ListBox1
.AddItem (wb.Name)
End With
Next wb
End Sub
Private Sub CommandButton2_Click()
Dim MainBook As Workbook, i as Long
For i = 0 To (ListBox1.ListCount -1)
If ListBox1.List(i) Like "Aggregate" Then
Set MainBook = Workbooks(ListBox1.List(i))
MainBook.Activate
Exit For
End If
Next
End Sub