hoping someone can help. I'm very new to VB code.
I'm building a set of Userforms where the data input is linked to various different worksheets. What i want is for my Userform Continue command buttons to only proceed to a certain Userform if the corresponding worksheet is active.
Eg My "Buildings" Userform must only display if Sheet "Buildings" is active on the workbook.
If Sheet "Buildings" is not active then the code must look for the next active sheet and go to the Userform that is linked to that sheet.
Eg If Sheet "Contents" is active on the workbook then the code must show my "Contents" Userform I created.
My first Userform asks the user to select which worksheets must be active by clicking on Toggle Buttons that activate sheets in the workbook.
From there the user clicks on a "Continue" button and then I need my code to look at which sheets are active.
I have this so far but I know its incorrect...
Private Sub CommandButton1_Click()
Dim answer As Integer
answer = MsgBox("Have you selected all your sections?", vbYesNo +
vbQuestion, "Selection Query")
If answer = vbYes And Sheets("Buildings") = Active Then
Me.Hide
Buildings_UF.Show
Else
Exit Sub
End If
End Sub
Thanking in advance
Patrick
UserForm.Show will stop the sub execution while the userform is opened, so the following code should suffice (untested code):
If answer = vbYes Then
Me.Hide
If Worksheets("Buildings").Visible then Buildings_UF.Show
If Worksheets("OtherSheet").Visible then OtherSheet_UF.Show
...
End If
Related
I have one doubt!
I have a code for consolidation of 22 sheets in a workbook.
Now I want to prepare separate buttons for each sheet so that if the user wants they can consolidate only sheets required by them and not all 22 sheets.
I only know the way by creating 22 separate module having part of the codes related to each sheet. Is there any other concise way to do it which do not make me prepare 22 separate modules?
Code example:
sheets("AT").select
"whatever code that was required"
Sheets("DE").select
"whatever code that was required"
and so on....
Do let me know if question is not clear.
You only need a single module. When you click a button on a sheet it will be on the activesheet. Therefore you will only need to act on the activesheet and not every sheet in the workbook.
If I was doing the job I would create a userform with a List of worksheet names that the user can select and then the module will step through each selected worksheet name in the list and do whatever actions you need. A checkbox for whole workbook action would also be useful.
Make a new Userform with a commandbutton and also a list box called "myListBox" and ensure that the MultiSelect property is set to multi and not single then add the following code. This will step through each sheet in the workbook and adds the name to the listbox. Once you select a number of names and click the command button it will print the selected names to the Immediate window
Private Sub UserForm_Initialize()
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
Me.myListBox.AddItem wks.Name
Next
End Sub
Private Sub CommandButton1_Click()
For i = 0 To myListBox.ListCount - 1
If myListBox.Selected(i) Then
Debug.Print myListBox.List(i)
End If
Next i
End Sub
Scenario:
User is running a macro-enabled Excel Workbook. User clicks a link on a UserForm that launches another macro-enabled Excel Workbook that also has user forms. The second Workbook is then closed using the following code:
Private Sub btnExit_Click()
'Check if other Excel files are open before quitting the application
If Workbooks.Count = 1 Then
Application.Quit
Else
ThisWorkbook.Close
End If
End Sub
and...
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim intResponse As Integer
If MsgBox("Are you sure you would like to exit the calculator?", vbYesNo + vbQuestion) = vbNo Then
Cancel = True
End If
ThisWorkbook.Saved = True
End Sub
Upon closing of the second Workbook (ThisWorkbook.Close), the original Workbook remains open, however, the vbModeless UserForm that was previously visible has been killed. It might also be pertinent to mention that the second Workbook's forms are Modal.
If the second Workbook is closed using the Excel exit button in the upper right corner, this problem does not occur.
Does anyone know why this is happening? Is it because code execution for Excel.Application is ceased when the Workbook is closed programmatically? Could it be due to the difference in form modes? Is there a work-around other than putting a button on the original Workbook in a Sheet to re-launch the UserForm?
Many thanks for any assistance with this issue!!
CiViCChiC79
This is what is happening:
Assuming that both workbooks are in the same Excel Application, Application.Quit would close them both.
ThisWorkbook.Close will close the workbook, in which the code is present.
Thus, concerning your question - you are not closing the second workbook with ThisWorkbook.Close, but you are closing the workbook, which has the btnExit button, on which you have clicked. I guess that both the workbooks are quite similar and you are mistaken which one you are closing?
Changing the second Workbook's form containing the Exit button to vbModeless fixed the problem!!!! Yay!!!!!
I'm trying to build a macro that will duplicate a worksheet from one workbook into a worksheet in another workbook. Is there a way I can use VBA code to allow me to manually select which worksheet I shall be duplicating?
Right now the macro works, as long as I have the full worksheet name typed into the actual VBA code. Ideally, I'd like the macro to allow me to select the worksheet through a dialog box. I know you can just copy/paste the sheet or its contents, but the guys I'm working for don't want to do that, due to the size.
You can use Worksheets collection to populate the ListBox in a user form. This should get you started:
Code in the user form (!):
Private Sub UserForm_Initialize()
Dim v As Worksheet
For Each v In Worksheets
UserForm1.lstWorksheets.AddItem v.Name
Next
End Sub
Private Sub cmdSelectWorksheet_Click()
MsgBox "You selected " & lstWorksheets.Value
End Sub
I need help with showing a specific Excel spreadsheet when the user clicks on a button called "Show in Excel".
When the user clicks i want the specific Excel worksheet to pop infront and show itself.
I have been looking for at long time now, and i need help :/
Sincerly,
Peyko.
This is what i have so far:
Private Sub CmdBtnExcel_Click()
Sheets("Sheet1").Show
End Sub
Private Sub CmdBtnExcel_Click()
Sheets("Sheet1").visible = true
Sheets("Sheet1").activate
End Sub
This will bring it 'infront'
I have a total of five sheets in a workbook. My task is to create a combo list button in the first sheet that will be able to point to the other four. If a user chooses one of the sheet names then the button will automatically activate the chosen sheet. It is unlikely that sheets will be deleted, though likely that sheets will be added.
I'm not even sure how to get the sheet names to show up on the combo list.
In order to make the combobox change the active sheet, I believe you'll need to use VBA (as I don't know how to do it using validation lists).
To do it, you'll have to:
1st - Add a combobox into your first sheet and properly name it (I called it cmbSheet). I suggest to use an ActiveX Combobox (in Excel 2007, under Developer tab).
2nd - Open VBA and add the below code into your workbook code. This code will populate the combobox with the sheet names every time the workbook is opened.
Private Sub Workbook_Open()
Dim oSheet As Excel.Worksheet
Dim oCmbBox As MSForms.ComboBox
Set oCmbBox = ActiveWorkbook.Sheets(1).cmbSheet
oCmbBox.Clear
For Each oSheet In ActiveWorkbook.Sheets
oCmbBox.AddItem oSheet.Name
Next oSheet
End Sub
3rd - Now, go to the code of your first sheet (where the combobox has been added) and add the code that will activate the sheet chosen in the combobox. The code is
Private Sub cmbSheet_Change()
ActiveWorkbook.Sheets(cmbSheet.Value).Activate
End Sub
Now, when the combobox value changes, the respective sheet is activated.
Let us know if something ins't clear and we'll help you out.