Excel VBA: Workbook_Open - excel

I'm using Workbook_Open to call a userform when the application is opened, this is working fine. However I would like it to only run the first time it is opened.
I tried this and it work if I run the sub from the editor but not when I open the file.
Sub Workbook_Open()
If Worksheets("DataSheet").Range("A1").Value = "" Then
QuickStartForum.Show
End If
End Sub
Note: A1 contains a value that will be populated after the user form has run
It appears that the problem is that it opens the user form before the data is loaded into the worksheet.
Is this there a way around this or do I need to take a different approach ?

I think it is because you have this code in a Module. You need to put the code within 'ThisWorkBook'.
I tried this following code and had no issues when it was in the 'ThisWorkBook' it failed to run within 'Module1'
Private Sub Workbook_Open()
If Worksheets("DataSheet").Range("A1").Value = "" Then
QuickStartForum.Show
Worksheets("DataSheet").Range("A1").Value = "filled" ' <-- this fills the cell with data for testing, so that when you reopen the file it should not re-open the userform
Else
MsgBox ("not shown because the A1 cell has data")
End If
End Sub

Related

Command_button in VBA inside vs Shape with Assigned Macro

I am trying to write a script for extracting data from web inside a userform. I am using Get Data From Web option in Excel (2019). The data gets updated when I refresh it manually or even when I do it with a macro assigned Autoshape. However, I have pasted the same code from the macro for a CommandButton that is inside my Userform. My intention is to refresh the data when I click the CommandButton. But when I click it, the background query keeps on running and doesn't terminate.
This is the code for a macro (for REFRESH DATA autoshape)
Sub refresh()
ActiveWorkbook.Connections("Query - stocklist").refresh
End Sub
This is the code inside my Userform (for REFRESH and GET commandbuttons). The GET button does a kind of Vlookup and works fine.
Private Sub cmd_refresh_Click()
ActiveWorkbook.Connections("Query - stocklist").refresh
End Sub
Private Sub cmd_get_Click()
txt_script.Text = UCase(txt_script.Text)
For Each cell In Range("Table_stock[Symbol]")
'For Each cell In Range("A1:I220")
If cell = txt_script.Value Then
txt_ltp.Value = cell.Offset(0, 4)
End If
Next cell
End Sub
ANY SUGGESTIONS?
it is more clear in the attached picture of excel template showing user form and macro button

Reopen workbook at last active sheet

How can I reopen my workbook at the last active sheet when I click on a hyperlink that runs ForceReopen? What I have fails because LstSht is not set. (Note that I do not want to save changes when I run ForceReopen.)
' Workbook module
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Set LstSht = Sh
End Sub
' Standard module
Public LstSht As Worksheet
Sub ForceReopen()
Application.OnTime Now + TimeValue("00:00:01"), "GoToLast"
ThisWorkbook.Close False
End Sub
Sub GoToLast()
LstSht.Activate
End Sub
You need to store the name of the last active sheet somwhere. Since you don't want to save the file on close, it can't be in the file itself.
One possibility is to create a small text file that contains just the sheet name to activate on file open. The workbook open event can then read the text file and activate the specified sheet.
The workbook activate event should update the text file. Provide error handlers to allow for the text file not existing, or the specified sheet not existing. Depending on how robust you want to make it, you might need to handle the sheet name changing too.
Location of the text file is a design choice: maybe the same folder as the Excel file, or some fixed config folder.
Another possibility would be to use the registry rather than a text file.
Remember something during automatic reopen within Application.Caption
You may store the ActiveSheet.Name within Application.Caption during a forced reopening of a workbook, even if all other global variables are lost.
' Within ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.Caption = "Last Active: " & ActiveSheet.Name
' Stop ' temporary to see the caption, but restart will not work!
End Sub
' Both following subs within a normal module:
Public Sub ForceReopen()
Application.OnTime Now + TimeValue("00:00:01"), "GoToLast"
ThisWorkbook.Close False
End Sub
Private Sub GoToLast()
Dim iLastSheet As Integer ' Position of text "Last Active" in Caption
iLastSheet = InStr(Application.Caption, "Last Active:")
' Stop ' temporary to see "Last Active: ..." in the caption
If iLastSheet > 0 Then
On Error Resume Next
ThisWorkbook.Sheets(Mid(Application.Caption, iLastSheet + 13)).Activate
On Error GoTo 0
Application.Caption = ""
End If
End Sub
If you save the workbook staying in Sheet1 and start ForceReopen staying in Sheet3 (by button, by hyperlink, by manual execution, whatever) then it reopens at Sheet3.
To check the functionality, you may add the first Stop to see, if Application.Caption is set correctly (workbook will not open afterwards, so you have to delete this Stop after testing):
You may add the second Stop to check, if Application.Caption is set as intended after it reopened automatically:
There is a logical flaw in your plan. Why should you activate the last viewed sheet and then close the workbook without saving that change?
Set a public variable by the name of LstSht. (I would prefer it to be the sheet name rather than the sheet object but that may be a matter of taste.
Set the variable on the Activate event of each sheet. Note that the Activate event may not occur when you first open the workbook. Therefore the variable must also be set in the Workbook_Open event.
In the GoToLast procedure provide for the eventuality that the variable doesn't hold a value - just in case. Use On Error Resume Next. Also make sure that the sheet isn't activated if it is already active. You may make this procedure a function and let it return True if the sheet was changed (for whatever purpose).
The effect of the whole thing, as you have laid it out, would be that, in the middle of whatever you are doing, the last active sheet is suddenly activated and then the workbook closed without saving your work. Charming idea!

How to make Application.key Shortcuts WorkBook Specific?

I added code to a series of similar files for different projects.
I defined a (Control + R) "^ + R" shortcut to let users see the current record in a userform.
I added application.key code on workbook activating and deactivating event, so the shortcut can be used when several files of this type are open.
My problem is even if the form opens and reads the data from the respective workbook, the userform is not called from the respective file!
Should I localize the procedure as well?
Here are my codes:
The Code in ThisWorkbook
Private Sub Workbook_Activate()
With Application
.OnKey "^R", "ReadCurrentRecord"
End With
End Sub
Private Sub Workbook_Deactivate()
Application.OnKey "^R"
End Sub
The Code in a general Module
Sub ReadCurrentRecord()
If ActiveCell.Worksheet.Name = "OSW" Then
If OSWRng Is Nothing Then
Set OSW = ThisWorkbook.Sheets("OSW")
Set OSWRng = OSW.Range("a5:bz2000")
End If
FrmWODetails.Tag = OSW.Cells(ActiveCell.Row, 14)
FrmWODetails.UserForm_Activate
FrmWODetails.Show vbModeless
End If
End Sub
All the Names are same in the files.
thisworkbook and activecell may not be what you expect them to be.
if this is an addin, this workbook will refer to the addin while active cell wont. i don't even know if activate a cell can be used in an add-in' sheet... I'm thinking not, I'll test that then i get to s computer for my own knowledge.
it's almost always best to fully qualify the object, sometimes from the application level but normally the workbook will suffice
personally, I prefer codenames over names and babes over indexes but they have a few drawbacks that keep them from being my#1 choice

VBA does not active

I suggest that my workbook contains a VBA code below. But It does not run when I opened my workbook too.
Sub Appl_StatusBar()
Application.StatusBar = "SupportABC"
End Sub
If you put your code in the Workbook Open Event it will do what you need. To do this click the top dropdown where it says "(General)" and hit "Workbook". In the right dropdown select "Open" and save your code there". See below
Private Sub Workbook_Open()
Application.StatusBar = "SupportABC"
End Sub

Excel Userform Combobox Properties Rowsource box issues?

I have a userform in Excel that works as a calculator.
In this userform I have two ComboBoxs (1 & 2)
In VBA editor, with ComboBox1 selected, In Properties, under Rowsourse I have: Sheet1!a4:a5
In Sheet1, A4 = Auckland and A5 = Christchurch
This is fine and when I run the userform there is a drop down arrow with the two options (Auckland or Christchurch).
However my problem is that when you open this workbook I have a VBA command to hide it from the users sight, leaving them only the userform to work with which is what is desired.
The issue is that if you have another workbook open then open this calculator workbook (which automatically hides itself). Then the combobox list is populated by Sheet1!a4:a5 on the other workbook that was already open, not the workbook that actually contains "Auckland" & "Christchurch" from which the userform is from.
I have tried making the Rowsource for the comboboxes more specific by putting the following in the rowsource box in properties: [book1.xlsm]sheet1!a4:a5 but this comes up with a "Invalid Property Value" error message.
I have also tried making a:
Private Sub Userform1_Initialize()
ComboBox1.Additem "Auckland"
ComboBox1.Additem "Christchurch"
End Sub
And also tried this:
Private Sub Userform1_Initialize()
ComboBox1.RowSource = Workbooks("book1.xlsm").Sheets("Sheet1").Range("a4:a5").Value
End Sub
However with both codes when it opens and runs now the comboboxes are empty and there is no list.
I think the easist solution would be to somehow put the full path (including workbook name) into the rowsource box under properties. But I must be missing something as its coming up with that error for me?
All help would be greatly appreciated.
Thanks
You are missing ' in your full path row source.
It should be like this:
Me.ComboBox1.RowSource = "'[book1.xlsm]Sheet1'!$A$4:$A$5"
I have similar question that can be found HERE.
Set the row source property of the combobox as: SheetName!$Col$Row:$Col$Row, e.g.: Location!$A$1:$A$3.
You may try adding this code on userform:
Private Sub UserForm_Initialize()
ComboBox1.list = Array("Auckland","Christchurch")
End Sub
Then set Combobox propert "MatchEntry" to "1".

Resources