Make my button say hello - vba

I'm trying to get started with VBA for Excel and have added the "Developer" tab to the ribbon. I have also in Module1 added a small sub:
Sub Hello()
MsgBox ("Hello world!")
End Sub
Then I've created a button in spreadsheet 1, right click and assigned Hello as it's macro.
By now I'm thinking this should work, but when I click on it nothing happens.
Further more, if I open the VBA editor window again Module1 have been edited, not also containing my actions?
Sub Hello()
MsgBox ("Hello world!")
End Sub
ActiveSheet.Shapes.Range(Array("Button 2")).Select
Selection.OnAction = "Hello"
Range("G15").Select
ActiveSheet.Shapes.Range(Array("Button 2")).Select
Application.Goto Reference:="Hello"
Whats this mambo jambo? And why does the button not work? :(

And try the messagebox without the parantheses, only the quotes. So: msgbox "Blabla"
Delete the code after the sub routine: it was accidentally generated by the macro recorder (without a purpose).

Private Sub CommandButton1_Click()
Call hello
End Sub
Delete everything below End Sub ....this might have got recorded somewhere along the lines
Right click the button and go fo "View code" and adjust the automated code to look like the above. You'll need to be in "Design Mode" to get the menu when selecting the button:

Related

How can I make my excel-vba macro click a button?

Here is the situation:
I have several buttons in different worksheets (but in same workbook) and the only thing those buttons do is print the worksheet. So imagine 6 different worksheets that all have a print button that quickly selects the relevant information and sends it to the printer.
Here is what I want to accomplish:
I now want to have a single button that will effectively click on all of the six buttons at the same time. So instead of clicking on the six buttons to send every indivudual worksheet to the printer, I would click one button and all of the worksheets would be sent to the printer
I would thus need help with creating a macro that essentially clicks on other macro-buttons which is what I am struggling with.
Thank you for your help.
My situation in image
Call each subroutine from your main subroutine. Something like this as an example:
Private Sub CommandButton1_Click()
MsgBox "Button 1"
End Sub
Private Sub CommandButton2_Click()
MsgBox "Button 2"
End Sub
Private Sub CommandButton3_Click()
MsgBox "Button 3"
End Sub
Private Sub Main()
CommandButton1_Click
CommandButton2_Click
CommandButton3_Click
End Sub

VBA Button to Clear a Range of Cells

As the title states, I am trying to create a button to clear a range of cells. Prior to clearing the cells, I have a dialog box pop up to confirm the selection. Here is my code:
Private Sub CommandButton1_Click()
If MsgBox("THIS WILL CLEAR EVERYTHING IN THE CART! Are you sure?", vbYesNo) = vbNo Then Exit Sub
Range("A28:AA47").ClearContents
End Sub
The code works when I'm in Design Mode and press "Play" but the button won't work on my sheet when I'm out of Design Mode.
Is the code completely ending itself when I select "No", to the point where it will not run again even if I hit the button again?
Your code looks a little incomplete. Try this instead:
EDIT: Cleaned it up a little more. No need for a vbNo, as if you don't select yes, the subroutine will simply end.
Private Sub CommandButton1_Click()
If MsgBox("THIS WILL CLEAR EVERYTHING IN THE CART! Are you sure?", vbYesNo) = vbYes Then
Range("A28:AA47").ClearContents
End If
End Sub

How to detect if user tried to click on save/save as option or pressed ctrl-S in Microsoft word using VBA code?

I want to detect when user press ctrl-S or click on Save option of Microsoft word using VBA excel macro code.
I found related links about Save changes while closing and Detecting if a document is getting close but I not able to find some example code for detecting saving of word document.
Any help would greatly appreciated.
Thanks
Unfortunately (or fortunately) Word does not work like excel and there the keybinding follows different logic. Thus, in Word, you should try something like this, to bind Ctrl + S. The idea is that on openning of the file you tell the application to bind Ctrl + S to the SaveMe Sub.
Option Explicit
Private Sub Document_Open()
With Application
.CustomizationContext = ThisDocument
.KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyS), KeyCategory:=wdKeyCategoryCommand, Command:="SaveMe"
End With
End Sub
Public Sub SaveMe()
MsgBox "User Saved"
End Sub
Put the code in ThisDocument:
Until now, it works this way only for Ctrl+S. If you go for the Save through the menu, it will not follow this logic. This is what you should do then:
For the general solution, follow these instructions:
I. Create a clsWord and put the following code inside:
Option Explicit
Public WithEvents appWord As Word.Application
Private Sub appWord_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Call SaveMe
End Sub
II. Change the code in ThisDocument to this:
Option Explicit
Dim myWord As New clsWord
Private Sub Document_Open()
With Application
.CustomizationContext = ThisDocument
.KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyS), _
KeyCategory:=wdKeyCategoryCommand, Command:="SaveMe"
End With
Set myWord.appWord = Word.Application
End Sub
III. In a module:
Option Explicit
Public Sub SaveMe()
MsgBox "User Saved"
End Sub
Parts of the ideas are taken from the MSDN here - https://msdn.microsoft.com/en-us/library/office/ff838299.aspx But the code there was not complete :)

Passing control to button in vba

I have a sub routine (named WBR) in "WBR45" sheet, and I have created a button in "Main" sheet. I have assigned macro also to that button.
And i have added the below code in the module1 where my sub WBR is present:
Sub Button2_Click()
WBR
End Sub
My Sub which i want to run through the button (in module1):
Sub WBR()
Dim Count1Criteria As Variant
Dim Count3Criteria As Variant
Dim test As Variant
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
End Sub
(PS: the code is too huge so i have just given the beginning)
But I when I click on the button, it doesnt run or show any result.
I suspect your code is not connected to the code you think is under Sub Button2_Click, try the code below to "debug" (when pressing your button)
Sub Button2_Click()
MsgBox "Test here" ' <-- to test if you are getting here
Sheets("WBR45").WBR
End Sub
You must insert the code on the WBR subroutine example:
Sub WBR()
'do something
End Sub
The macro on the button should look something like this
Sub Button2_Click()
WBR
End Sub
It looks like your macro isn't assigned to the button, the code you post looks correct. Right click the button > Assign macro. Then select the correct macro.
If you want to debug, place a red dot on the grey bar directly left to the code, this will insert a pause when the macro is executed.
(The code in the TS actually doesn't do anything, so it might be that the code does get executed but you don't notice anything)

How to open a spreadsheet in Excel?

How can I make that, when pressing the command button, it opens the first spreadsheet of Excel?
I am coding in Visual Basic Forms in Excel 2016. This is my code:
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then MsgBox (" Has decidido Actualizar el Inventario")
Workbook("Hoja1").ShowAllData
End Sub
The code can be simplified into this:
Sub firstSheet()
Sheets(1).Select
End Sub
Then go to the Developer tab, add a button, and assign it to this macro.