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)
Related
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
I am having an issue copying and pasting a drop-down data validation menu from one sheet into all selected sheets. The drop-down menu seems to paste randomly instead of pasting into sheet "B22" of the selected sheets.
Sub TEST()
Dim sht As Worksheet
Sheets("Sheet2").Range("B22").Copy
'Sheets selection should be done before running macro
Selection.Range("B22").PasteSpecial xlPasteValidation
Application.CutCopyMode = False
End Sub
Any suggestions on how to tackle this? I am having some difficulty finding the error in my code.
In case you want to work with the workbook and avoid using copy/paste..
Option Explicit
Sub Test()
Dim excel_sheet As Worksheet
Dim sht As Worksheet
Dim drop_down_value As Range
Set sht = ThisWorkbook.Sheets("Sheet2")
Set drop_down_value = sht.Range("B22")
'Sheets selection should be done before running macro
For Each excel_sheet In ThisWorkbook.Windows(1).SelectedSheets
excel_sheet.Range("B22").Value = drop_down_value.Value
Next
End Sub
Try the following, you need to loop through all selected Sheets instead of using Selection.Range:
Sub TEST()
Dim sht As Worksheet
Sheets("Sheet2").Range("B22").Copy
'Sheets selection should be done before running macro
For Each sht In ActiveWindow.SelectedSheets
sht.Range("B22").PasteSpecial xlPasteValidation
Application.CutCopyMode = True
Next
End Sub
I have two simple pieces of code in a Userform and what I want to do, I believe is quite straight forward however I'm stuck!
Private Sub UserForm_Initialize()
Dim wkb As Workbook
With Me.CB_Excel_File
For Each wkb In Application.Workbooks
.AddItem wkb.Name
Next wkb
End With
End Sub
Private Sub CB_Excel_File_Change()
Dim wks As Worksheet
With Me.CB_Worksheet
For Each wks In ***Me.CB_Excel_File.Value.Worksheets***
.AddItem wks.Name
Next wks
End With
End Sub
Its the piece with *** that I'm stuck on as I want to list all of the worksheets in the workbook that is selected by the user from the Userform_Initialize code.
Thanks in advance!
You need to pass the selected workbook name into the Workbooks collection as a variable, as so:
Private Sub CB_Excel_File_Change()
Dim wks As Worksheet
With Me.CB_Worksheet
For Each wks In Workbooks(CB_Excel_File.Value).Worksheets
.AddItem wks.Name
Next wks
End With
End Sub
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.
I found a way to add all of the worksheets in the workbook into the list of the ComboBox on the UserForm, which is done with use of the following code
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In Worksheets
cmbSheet.AddItem ws.Name
Next ws
End Sub
My problem is that I only need to add some specific worksheets and not all of them. For example my user will select specific sheet and by clicking "Continue" button should end up on the selected worksheet to continue his/her task. My workbook holds several worksheets, some of which are used to output data (Reports) and some worksheets contain so called templates, which I want my user (only those) to be able to select from the ComboBox I have talked above.
Can you guys help me please?
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In Worksheets
if ws.Name like "*Template" then cmbSheet.AddItem ws.Name
Next ws
End Sub
perhaps you could use a naming convention for the worksheets?
if the reports you want the user to select all have the word temmplate in their name you could do something like this:
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In Worksheets
if instr(lcase(ws.name),"template")<>0 then
cmbSheet.AddItem ws.Name
end if
Next ws
End Sub