Private Sub Opens Hyperlink Twice instead of Once - excel

I have a pretty simple macro, I thought would be cheeky to add a macro to a Resume where upon opening the document it also pulls up my LinkedIn. However, the macro opens the same link on two tabs, is there something I'm missing?
Private Sub Document_Open()
ActiveDocument.FollowHyperlink ("[My link would be here]")
End Sub
The Excel Version of this Macro:
Sub Workbookbook_Open()
ActiveWorkbook.FollowHyperlink ("[My link would be here]")
End Sub

From the docs:
ActiveDocument.FollowHyperlink _
Address:="https://www.Microsoft.com", _
NewWindow:=True, AddHistory:=True
only opens one tab.

Related

How to reference an Excel UserForm from Access VBA

CASE:
I have created an Access database.
Using Access-VBA I open an Excel workbook to do stuff.
This workbook (xlWb) does complex calculations and also loads an Excel Userform (UserForm2) on open.
So far everything OK. I open xlWb, UserForm2 loads, I do stuff.
GOAL:
After this, I need to "refresh" Userform2.
By the term "refresh" I mean either call a custom sub of this UserForm, or unload and reload UserForm2.
QUESTION:
How can I can reference UserForm2 from my Access-VBA code?
WHAT I 'VE TRIED:
By searching I 've only found how to reference a UserForm from
another workbook.
The first suggestion was to use VBA.UserForms to get a loaded UserForm.
So I 've tried the following references: VBA.UserForms("UserForm2"), VBA.UserForms.Item("UserForm2"), VBA.UserForms(0), VBA.UserForms(1), all of which threw error: "subscript out of range", which implies that what I 'm writing is not a member of the collection.
Another suggestion was to create a function that loads and unloads the object.
So I wrote inside an xlWb's module named Apps this:
Public Sub Refresh_UserForm()
Unload Userform2
Userform2.Show
End Sub
and in the access sub this:
Application.Run "'" & xlWb.Name & "'!Apps.Refresh_UserForm"
This throws a
Run-time error 2517 cannot find the procedure
'calc_8.4.xls'!Apps.Refresh_UserForm'
The same error is also generated when I tried a 3rd similar suggestion to create a function that returns an instance of the object.
Every suggestion is very welcome thank you.
So this works for me:
Note: for demonstration purposes I created a xlsm file called "Map1.xlsm" with a userform called "UserForm1". Also, you'll need the "Microsoft Excel 16.0 Object Library" reference turned on.
First, as mentioned above I created an Excel file with the below macro in Module1:
1). To open the UserForm for the first time (and refresh):
Sub Refresh_UserForm()
Unload UserForm1
UserForm1.Show False
End Sub
Within Access-VBA I created the following two macros (based on this):
1). To open and use the Excel UserForm
Option Compare Database
Dim xlApp As Excel.Application, xlWB As Excel.Workbook
Sub mySub()
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Open("C:\...Path...\Map1.xlsm") 'Specify Path
xlApp.Visible = True
xlApp.Run "Map1.xlsm!Refresh_UserForm"
End Sub
2). To Refresh the Excel UserForm
Sub Refrsh()
Set xlApp = GetObject(, "Excel.Application")
xlApp.Run "Map1.xlsm!Refresh_UserForm"
End Sub
Hopefully this helps
Please try the following:
In userform2, add a "Refresh" button and the following procedure:
Private Sub CommandButton_Refresh_Click()
Me.Repaint
End Sub
If you have a UserForm_Initialize sub and UserForm_Terminate sub , you can include the procedures to simulate the unload-reload action.
Private Sub CommandButton_Refresh_Click()
Call UserForm_Terminate
Call UserForm_Initialize
Me.Repaint
End Sub
If you need to unload the form and call it back later manually, then you will have to call it back from the actual loaded Excel window (e.g. with a form Button on the worksheet), but cannot be from Access or the xlWb hook.

How can I call a macro from another worksheet without opening it?

I have two worksheets; in the first one called "Foglio1.xls" I have the function "printValue", in the second one I want to call this function.
To do that I used this code:
Private Sub Workbook_Open()
Application.Run "'C:\Users\fra31\source\repos\Key Visor\Key Visor\Foglio1.xls'!printValue"
End Sub
It works well, but I don't want to open a new window, because the file "Foglio1.xls" is already open.
can someone help me?
If the workbook is already open this should work.
Private Sub Workbook_Open()
Application.Run "'Foglio1.xls'!printValue"
End Sub

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

calling VBA forms from a button in Excel

I have a macro that parses through a large file in Excel. I created a form that allows me to search for and provide a summary of specific messages. I can put
FormName.Show
at the end of the macro and it works. The form displays as the parsing macro completes. But I don't always want the form to appear and sometimes I want to call it again after I've saved the spreadsheet. So I wrote another function that can create a button that can open the form. In the .OnAction statement I have OnAction = "FormName.Show"
Sub Create_Button()
ActiveSheet.Buttons.Add(437.25, 72, 125.25, 47.25).Select
Selection.OnAction = "FormName.Show"
Selection.Characters.Text = "Search Messages"
End Sub
This doesn't work, it created the button but when I click on the button I get
"Cannot run the macro "xxxx.xlam'!FormName.Show' The macro may not be available in this workbook or all macros may be disabled.
Why does it work in the main macro but not in the button OnAction?
Thanks
Per MSDN, Shape.OnAction requires a macro name, not VBA code. You have tried to pass it VBA code. Instead try this:
Selection.OnAction = "showForm"
....
End Sub
Public Sub showForm()
FormName.Show
End Sub
.OnAction will not only run Macros not it will not evaluate code. Paste thiss in a standard module.
Sub ShowUserform()
FormName.Show
End Sub
Sub Create_Button()
With ActiveSheet.Buttons.Add(437.25, 72, 125.25, 47.25)
.OnAction = "ShowUserform"
.Characters.Text = "Search Messages"
End With
End Sub

How do I delete all buttons on a worksheet?

I've got a bit of code that creates a Save button on a worksheet (held in the wsReport variable), but it doesn't remove previous buttons. Over time, they tend to build up. Is there any way to do something like this?
wsReport.Buttons.All.Delete
(Not right, obviously, but gives an idea of what I'm looking for.)
See code below :)
Sub RemoveButtons()
Dim i As Integer
If ActiveSheet.ProtectContents = True Then
MsgBox "The Current Workbook or the Worksheets which it contains are protected." & vbLf & " Please resolve these issues and try again."
End If
On Error Resume Next
ActiveSheet.Buttons.Delete
End Sub
Source:
http://www.mrexcel.com/forum/excel-questions/609668-delete-all-buttons-sheet-visual-basic-applications.html
or could you use code below:
(Buttons in VBA are in the Shapes collection).
Sub DelButtons()
Dim btn As Shape
For Each btn In ActiveSheet.Shapes
If btn.AutoShapeType = msoShapeStyleMixed Then btn.Delete
Next
End Sub
source:
Deleting a collections of VBA buttons
See the code below:
Sub DeleteAllShapes()
ActiveSheet.Shapes.SelectAll
Selection.Delete
End Sub
If somebody found this question, but needs to delete only one button, this helped me:
ActiveSheet.Shapes("my_button_name").Delete
There's an easier method that doesn't use VB:
Click the Developer tab
Click Design Mode
Click on any button in the worksheet
Press Ctrl + A to select all buttons in the worksheet
Press Delete

Resources