How to open a spreadsheet in Excel? - vba

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.

Related

VBA userform now showing in quick access toolbar in excel 2016

I have a userform which I cannot select in the quick access toolbar to add to the ribbon.
All of the other macros I have are visible and can be selected to be added to the quick access toolbar.
I have the name of the userform and command buttons in the userform code matching the name listed within their reposective properties windows.
I have been able to add userforms to previous versions of excel so I am not sure if anything has changed for excel 2016.
Sub userform1_show()
UserForm1.Show
End Sub
Private Sub CommandButton1_Click()
Load.Linux_unix_scan_tab_only
Linux_unix_scan_tab_only.Linux_unix_scan_tab_only
End
End Sub
Private Sub CommandButton2_Click()
Load.unix_SSHkey_tabs
unix_SSHkey_tabs.Linux_unix_scan_and_SSH_Key_trust_tabs
End Sub
Private Sub userform_click()
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 :)

how to prompt sheet selection in excel vba

When a workbook has many sheets such as 100, you can activate the sheet quickly by right-clicking the arrows on the bottom left and then a prompts shows up by lets you choose which sheet you want to select. However, I want to be able to reach this prompt without clicking and using just the keyboard. So I want to create a simple macro where I can quickly pull up this prompt by assigning to something like ctrl+g. However, I do not know how to do this in vba.
Build your replica Activate form: Insert a userform into your project. Set its caption to "Activate". Add a label to it that says "Activate:". Add a Listbox and two buttons. Label one button Ok and the other Cancel.
In the UserForm_Initialize sub of the userform, Loop through the names of your sheets and add them to the listbox....something like:
Private Sub UserForm_Initialize()
Dim oSheet As Worksheet
For Each oSheet In Worksheets
ListBox1.AddItem oSheet.Name
Next
End Sub
Private Sub CommandButton1_Click()
Sheets(ListBox1.Text).Activate
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Now in the ThisWorkbook section of the VBA Project, add a sub to show your userform:
Private Sub ShowActivateForm()
UserForm1.Show
End Sub
Go back to Excel and click the Macros button on the developer tab. You should see a macro named ShowActivateForm. Select it and click Options to assign it your desired hotkey.

Calling a macro from a button in edit mode while in PowerPoint

I'm trying to write a vba macro that can be called in edit-mode in PowerPoint 2007-2010.
I can easily add a Command Button to a presentation. However, this button can only be clicked to trigger the vba macro while in slideshow mode.
However, what I would like to do is have this button trigger the associated vba macro while in edit mode. Clicking on it in edit mode allows me to change its size etc, but it doesn't call the macro.
In Excel on the other hand, I get exactly the expected behaviour when I insert a button -> clicking on it calls the vba action.
So how can I create a button (or other element that acts the same way) that calls a vba macro during edit view in PowerPoint. The only way I can think of is using a ribbon action, however this is unpractical in this case, because the macro will modify a shape that is associated with the button and there might be several of these shapes per slide that should each have their own button.
The only way I can think of is using a ribbon action, however this is unpractical in this case, because the macro will modify a shape that is associated with the button and there might be several of these shapes per slide that should each have their own button.
Depending on what you're trying to do, a ribbon button that launches a macro might be quite practical. The macro could operate on the current selection (and test the current selection to ensure that it's something appropriate).
With ActiveWindow.Selection.ShapeRange
' operate on the currently selected shapes
End with
just answer it a some where else also
it is possible to do so all you need is download this file
http://www.officeoneonline.com/eventgen/EventGen20.zip
install it
create a class module
paste this code
Option Explicit
Public WithEvents PPTEvent As Application
Private Sub Class_Initialize()
End Sub
Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
If Sel.Type = ppSelectionShapes Then
If Sel.ShapeRange.HasTextFrame Then
If Sel.ShapeRange.TextFrame.HasText Then
If Trim(Sel.ShapeRange.TextFrame.TextRange.Text) = "Text inside your shape" Then
Sel.Unselect
yoursub
End If
End If
End If
End If
End Sub
insert a new module
paste this code
Dim cPPTObject As New Class1
Dim TrapFlag As Boolean
Sub TrapEvents()
If TrapFlag = True Then
MsgBox "Already Working"
Exit Sub
End If
Set cPPTObject.PPTEvent = Application
TrapFlag = True
End Sub
Sub ReleaseTrap()
If TrapFlag = True Then
Set cPPTObject.PPTEvent = Nothing
Set cPPTObject = Nothing
TrapFlag = False
End If
End Sub
Sub yoursub()
MsgBox "Your Sub is working"
End Sub
Now run TrapEvents and whenver you will click shape with that text in it your sub will run
Credits to the person who wrote this http://www.officeoneonline.com/eventgen/eventgen.html

Make my button say hello

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: