Logicwurks Home Page

Links To Excel Code Examples

Range/Wkb/Wks Variables
Add Grand Totals Using Ranges
Using Range Offset Property
Using Range Find Method
Union Of Ranges
Parse Range Strings
Delete Duplicate Rows
Delete Rows And Columns
Worksheet Variables
TypeName And TypeOf
Loop Through Worksheets
Loop Through Open Workbooks
Form Button Magic
Command Button Magic
Add Worksheets Dynamically
Find Last Row Or Column
Copy And Paste Special
Copy To Specific Cell Types
Range Copy With Filter
Open An Excel File
Open An Excel File w/Params
Open An Excel File On Web
Save A Workbook
Clone A Workbook
Test If WEB URL Exists
Parse Using Split Command
Color Management
Convert Cell Color To RGB
Sort Methods 2003 - 2010
Sort Alpha/Numeric In ASCII
Search Using Match Function
Search Using Vlookup Function
Using Find Instead of Vlookup
Remove String Non-Printables
Auto_Open And Auto_Close
Initialize Form At Open
Edit Numerics In UserForm
Load Combo And List Boxes
Floating Sheet Combo Boxes
Advanced User Form Coding
Excel Events
Worksheet Change Events
Binary Search Of Array
Typecast Constants
Excel Error Handling
Handling Optional Parameters
Data Validation Drop Downs
Read A Text File w/Handle
Write A Text File w/Handle
Read A Text Fiile w/Script
Text File Processing Examples
Test For Exists Or Open
Splash Screen
Dynamically Load Formulas
Date Examples
Date Find Same Days
Convert Month To Number
Initialize Arrays
Load Arrays Using Evaluate
Redim An Array
Reassign Button Action
Timer Functions
Legacy Calendar Control
Excel 2010 Date Picker
Date Picker Alternative
Generate Multiple Worksheets
Read Access Data Into Excel
Send Outlook Email w/Attach
Copy AutoFilters To Sheets
Export A Text File
Get Windows User Name
VBA Format Statement
Manipulate Files via VBA
Dynamically Load Images
Loop Through Worksheet Objects
Loop Through Form Objects
Loop Through Files with DIR
Active-X Checkboxes
Add Forms Checkboxes Dynam
Paste Pictures Into Excel
Copy Pictures Sheet To Sheet
Copy Pictures Sheet To Sheet
Create Forms Buttons With VBA
Extract Filename From Path
Convert R1C1 Format to A1
Special Cells Property
Insert Cell Comments

Links To Access Code Examples

Create Recordset With AddNew
Multi-Select List Boxes
Update Field(s) In A Recordset
Import A Tab Delimited File
Export Excel FileDialog
Create Excel Within Access
Open Excel Within Access
Open Excel OBJ From Access
Format Excel From Access
Import Tab Delim w/WinAPI
Initialize Global Variables
Using TempVars For Globals
Access Error Handling
Loop Through Form Controls
Insert A Calendar Control
Create A Filtered Recordset
Populate Combo Boxes
Bookmarks And Forms
Combo Box Multiple Sources
Passing Form Objects
Create VBA SQL Statements
Create Dynamic Queries
Display File Images On A Form
Manipulate Files via VBA
Manipulate Files via Scripting
Number Subform Records
Reference Subform Objects
Parse Delimited Fields
Parameterized Queries (VBA)
Manipulating QueryDefs In VBA
FindFirst On Combined Keys
Dlookup Command
Dlookup In Form Datasheet
Execute SQL Delete Records
Commit Form To Table
Report With No Data
Reference Form Objects
DSNLess Connections To MySQL
Print Active Form Record
Count Records in Linked Tables
Delete Empty Tables
Open Linked SQL Tables


Form Button Magic On Worksheets

There are two types of buttons: Forms Control Buttons which can be linked to a Macro, and Active-X (OLE Objects) Buttons. This article concerns only Forms Control buttons. There are many times when it is valuable to dynamically insert a button onto a worksheet, and then delete, hide or unhide it. The button inserted is assigned to a macro and when clicked by the user, the button will initiate the assigned Macro. The following code shows how to create and delete buttons, and how to reference them as Shape and Button Objects.

Program Code

Option Explicit
Public Sub TestButton()
' *******************************************************************
' There are 2 types of buttons:  Forms Control and Active-X
' This demonstration is only for Forms Control Buttons
' *******************************************************************
Dim wkbThisWorkbook As Workbook
Dim wksButtonSheet As Worksheet
' *******************************************************************
' Button Objects for Forms Control Buttons (Shape Type 8)
' *******************************************************************
Dim btnButton As Button
Dim btnButton1 As Button
Dim btnButton2 As Button

' *******************************************************************
' Shape Objects
' *******************************************************************
Dim shpButton As Shape

' *******************************************************************
' Range Objects
' *******************************************************************
Dim rngButton2 As Range

' *******************************************************************
' Initialize Workbook, Worksheet and Range Variables
' *******************************************************************
Set wkbThisWorkbook = ActiveWorkbook
Set wksButtonSheet = wkbThisWorkbook.Sheets("Sheet1")
Set rngButton2 = wksButtonSheet.Cells(20, "C")

' ********************************************************************
' Delete All Forms Control Buttons
' ********************************************************************
MsgBox ("Buttons Deleted")

' ********************************************************************
' Create Adequate Space For A Button Based on Cell Size
' ********************************************************************
wksButtonSheet.Rows("20:20").RowHeight = 44.5
wksButtonSheet.Columns("C:C").ColumnWidth = 25.5
' ******************************************************************** ' Create A Forms Control Button (Button1) Using the Button Object ' Place it in an exact screen location ' ******************************************************************** Set btnButton1 = wksButtonSheet.Buttons.Add(100, 100, 100, 100) With btnButton1 .OnAction = "TestButton" .Caption = "*Button 1*" .Name = "btnRunMacro1" .Font.Name = "Arial" .Font.Size = 15 .Font.Color = RGB(255, 0, 0) .Font.Bold = True .Visible = False 'Choose one of these two .Visible = True 'Choose one of these two .Placement = xlFreeFloating 'Choose one of these three .Placement = xlMove 'Choose one of these three .Placement = xlMoveAndSize 'Choose one of these three End With MsgBox ("Button1 Created") ' ******************************************************************** ' Create A Forms Control Button (Button2) Using the Button Object ' Fill an entire Cell with the button ' Note: The Caption sets the Alternative Text Property For the Shape ' ******************************************************************** Set btnButton2 = wksButtonSheet.Buttons.Add(rngButton2.Left, rngButton2.Top, _ rngButton2.Width, rngButton2.Height) With btnButton2 .OnAction = "TestButton" .Caption = "*Button 2*" .Name = "btnRunMacro2" .Font.Name = "Arial" .Font.Size = 15 .Font.Color = RGB(255, 0, 0) .Font.Bold = True .Visible = False 'Choose one of these two .Visible = True 'Choose one of these two .Placement = xlFreeFloating 'Choose one of these three .Placement = xlMove 'Choose one of these three .Placement = xlMoveAndSize 'Choose one of these three End With MsgBox ("Button2 Created") ' ******************************************************************** ' Change Button's Dimensions By Referencing The Shape Object ' ******************************************************************** With wksButtonSheet.Shapes("btnRunMacro1") .Height = 23.25 .Width = 200.1 .Left = 586.5 .Top = 28.5 End With ' ******************************************************************** ' Manipulate Existing Buttons Looping Through Shape Objects ' And Only Selecting Type 8 (Forms Control Button) Objects ' ******************************************************************** For Each shpButton In wksButtonSheet.Shapes If shpButton.Type = 8 Then Debug.Print shpButton.Name Debug.Print shpButton.AlternativeText Debug.Print shpButton.Type Application.ScreenUpdating = False shpButton.Visible = False Application.ScreenUpdating = True MsgBox ("Button " & shpButton.AlternativeText & " Should Be Hidden") Application.ScreenUpdating = False shpButton.Visible = True Application.ScreenUpdating = True MsgBox ("Button " & shpButton.AlternativeText & " Should Be Visible") shpButton.AlternativeText = "NewButton" End If Next ' ******************************************************************** ' Manipulate Existing Button Using Specific Button Object ' ******************************************************************** With wksButtonSheet.Buttons("btnRunMacro1") .Height = 28 .Width = 80 .Left = 600 .Top = 30 .OnAction = "TestButton" .Caption = "Run New" End With ' ******************************************************************** ' Manipulate Existing Button Looping Through Button Objects ' ******************************************************************** For Each btnButton In wksButtonSheet.Buttons Debug.Print btnButton.Name Debug.Print btnButton.Caption Debug.Print btnButton.Font.Name Debug.Print btnButton.Font.Color Next btnButton End Sub