I'm trying to create a UserForm where all open Excel Workbooks are displayed in the Combo Box, but for some reason, despite having several workbooks clearly open, no results display in the dropdown of the Combo Box.
This is the code of the UserForm
Option Explicit
Private Sub CommandButton1_Click()
MyFile = Me.SourceSlate.Value
Unload Me
End Sub
Private Sub CommandButton2_Click()
Hide
Stopped = True
Unload Me
End Sub
Private Sub SourceSlate_UserForm_Initialize()
Dim wkb As Workbook
Me.Label.Caption = "Select the Source slate from the list of open Excel Workbooks below"
With Me.SourceSlate
For Each wkb In Application.Workbooks
.AddItem wkb.Name
Next wkb
End With
End Sub
and this is the code for the module to run the UserForm
Option Explicit
Public MyFile As String
Public Stopped As Boolean
Sub SourceSlate_UserForm()
Stopped = False
SourceSlate.Show
If Stopped Then Exit Sub
End Sub
Any help is greatly appreciated!!!
Related
I will always have Workbook SQ_Macro_v1 as my main DB.
Two named Workbooks old_wk and new_wk will have different names, as I will choose them among the currently active WB on my computer.
I am going for a VBA ComboBox listing it all to a choice of mine, but in the end I am not able to store the name of my chosen WB.
Sub Macro1()
Dim main_wk, old_wk, new_wk As Workbook
Set main_wk = Workbooks("SQ_Macro_v1.xlsm")
Set old_wk = Workbooks(old_chosen) 'also tried UserForm1.ComboBox1.Value
Set new_wk = Workbooks(FileName_New) '
main_wk.Sheets("Main_DB").Range("C4").Value = old_wk.Worksheets("Sheet 1 Synthese").Range("C35").Value
As I run the UserForm code below, the old_chosen variable I set as empty in the main Sub. It seems that as I close the UserForm after it runs, nothing remains stored. Any clues to keep that variable saved after I close the UserForm?
Option Explicit
Public Sub UserForm_Activate()
Dim vWorkbook As Workbook
ComboBox1.Clear
For Each vWorkbook In Workbooks
ComboBox1.AddItem vWorkbook.Name
Next
End Sub
Public Sub CommandButton1_Click()
If ComboBox1.ListIndex <> -1 Then
Call YourMacro(ComboBox1)
End If
End Sub
Public Sub YourMacro(vWorkbookName As String)
Dim old_chosen As String
old_chosen = Me.ComboBox1.Value
MsgBox "You choose: " & Workbooks(vWorkbookName).Name
End Sub
The MsgBox pops up but no value is stored afterward:
Public Sub YourMacro(vWorkbookName As String)
Dim old_chosen As String
old_chosen = Me.ComboBox1.Value
MsgBox "You choose: " & Workbooks(vWorkbookName).Name
End Sub
You have declared the variable at procedure level and hence it is not visible after the userform is closed.
To make this variable available to all procedures in the project, precede it with the Public statement. Insert a module and paste this there
Public old_chosen As String
Having said that, I would recommend moving Macro1 inside the userform and handle the code from there after declaring the variable at module level
Almost every time I use my user form, it shrinks, and after a few times it gets too small to see and I have to go back into the forms on my project and drag it until the size is large again. Is this a result of my code, or is there something that I can do to fix this?
Option Explicit
' called on click of "OK" button
Private Sub CommandButton1_Click()
MyFile = Me.ComboBox1.Value
Unload Me
End Sub
' called on click of "Cancel" button
Private Sub CommandButton2_Click()
Stopped = True
Unload Me
End Sub
' loads the combo box with the names of all available workbooks
Private Sub UserForm_Initialize()
Dim wkb As Workbook
With Me.ComboBox1
For Each wkb In Application.Workbooks
If wkb.Name <> ActiveWorkbook.Name Then
.AddItem wkb.Name
End If
Next wkb
End With
End Sub
I can't think of a reason for this to happen, especially without any code anywhere tweaking the Height and Width of the form, and with the form Unload-ing itself every time it's shown, even if you're Show-ing the form's default instance it should still be initialized with the design-time values.
You could try forcing a size explicitly in that initialize handler:
Private Sub UserForm_Initialize()
Me.Height = 480
Me.Width = 600
InitializeComponents
End Sub
Private Sub InitializeComponents()
PopulateAvailableInactiveWorkbooks
'...
End Sub
Private Sub PopulateAvailableInactiveWorkbooks()
Dim wkb As Workbook
With Me.ComboBox1
For Each wkb In Application.Workbooks
If wkb.Name <> ActiveWorkbook.Name Then
.AddItem wkb.Name
End If
Next wkb
End With
End Sub
If you have this issue, you may like to check;
https://www.mrexcel.com/board/threads/userforms-shrink-on-successive-openings-of-an-excel-file.1078705/
For me, adding the following to the userform code at least kept the display right.
Private Sub UserForm_Initialize()
With frm40_Overview1
Height = 600
Width = 800
End With
Application.ScreenUpdating = True
End Sub
Scenario
I have a userform whereby excel workbook will be hidden while opening using the following method of Application.Visible = False. These are the codes
My userform
show excel button is Commandbutton1
hide excel button is Commandbutton2
This workbook
Codes
Private Sub Workbook_Open()
Call hideExcel
UserForm1.Show
End Sub
Userform1
Codes
Private Sub CommandButton1_Click()
If Workbooks.Count > 1 Then
Windows(ThisWorkbook.Name).Visible = True
Else
Application.Visible = True
End If
End Sub
Private Sub CommandButton2_Click()
Call hideExcel
End Sub
Sub UserForm_Initialize()
Call hideExcel
End Sub
Private Sub UserForm_Terminate()
If Workbooks.Count > 1 Then
Windows(ThisWorkbook.Name).Visible = True
Else
Application.Visible = True
End If
End Sub
Sub userform_click()
Call hideExcel
End Sub
Module
Codes
Sub hideExcel()
If Workbooks.Count > 1 Then
Windows(ThisWorkbook.Name).Visible = False
Else
Application.Visible = False
End If
End Sub
Problem
The problem I am facing is
Open my macro and userform activated. Lets call this file A
Then open another workbook. Lets call this file B
Tried to close file B while workbook A is hidden. But there is a prompt to close file A also and eventually all excel will be closing including my macro file which is A.
Does anyone know what is the problem here?
I don't understand where the problem is? If you are closing last visible (not hidden) workbook, Excel also tries to close all other open workbooks (even if they're hidden). And I think it's normal Excel behavior. You can only avoid to see a prompt, e.g. by setting up Workbook.Saved property to True or by setting up Application.DisplayAlerts property to False or just by saving workbook before closing.
If you don't want to close hidden workbook you just have to make it visible before closing the second workbook.
I'am trying to prevent users from pasting other things than values in the template I'm developing. I use a macro to always paste values in the worksheet (see below). When users switch to another workbook this macro should be disabled.
The problem is that I get error 91 when activating another workbook.
'the macro in a module
Sub AlwaysPasteValues()
Selection.PasteSpecial Paste:=xlPasteValues
End Sub
'the code in this workbook
Public wb As Workbook
Private Sub Workbook_Activate()
Application.MacroOptions Macro:="AlwaysPasteValues", Description:="AlwaysPasteValues", ShortcutKey:="v"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set wb = Nothing
End Sub
Private Sub Workbook_Deactivate()
With wb
.MacroOptions Macro:="AlwaysPasteValues", Description:="AlwaysPasteValues", ShortcutKey:=""
End With
End Sub
Private Sub Workbook_Open()
Set wb = ThisWorkbook
End Sub
You are changing .MacroOptions on wb which does not exist as a property. MacroOptions is for the Application. Use the same code as the Activate and you should be good.
Private Sub Workbook_Deactivate()
Application.MacroOptions Macro:="AlwaysPasteValues", Description:="AlwaysPasteValues", ShortcutKey:=""
End Sub
I have some VBA code in an Excel workbook.
I have a cell which I would like to use to close the workbook without saving (instead of using the usual (X) close button on the top right corner of excel.
If I close the workbook using the (X) button, it works great the following code.
However if I press the "CLOSE" button cell which is on the worksheet, it gives a 1004 error.
Can anyone help?
ON WORKSHEET WHERE BUTTON IS
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Cells(ActiveCell.Row, ActiveCell.Column) = "CLOSE" Then
CloseNoSave
End If
End Sub
IN THISWORKBOOK
Private Sub Workbook_BeforeClose(Cancel As Boolean)
CloseNoSave
End Sub
IN MODULE
Sub CloseNoSave()
ThisWorkbook.Close savechanges:=False
End Sub
Why don't you just use a single piece of code on your Selection Event
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Value = "CLOSE" Then
ThisWorkbook.Saved = True
ThisWorkbook.Close
End If
End Sub