Anda di halaman 1dari 5

' SOME ADDITIONAL CODE FROM THE BOOK

' Can be pasted into an Excel code module


' *****************************************
' Chapter 10
Sub DeleteCustomMenuItem()
Dim cbc As CommandBarControl
Set cbc = Application.CommandBars( _
"Worksheet menu bar"). _
FindControl(Type:=msoControlPopup, _
Tag:="SRXUtilsCustomMenu")
If Not cbc Is Nothing Then cbc.Delete
End Sub
' **********************
Sub CreateCustomMenuItem()
Dim cbcpop As CommandBarControl
Dim cbpop As CommandBarControl
' Check for custom menu. If it exists then exit.
Set cbcpop = Application.CommandBars( _
"Worksheet menu bar"). _
FindControl(Type:=msoControlPopup, _
Tag:="SRXUtilsCustomMenu")
If Not cbcpop Is Nothing Then Exit Sub
' Create a popup control on a menu bar
Set cbpop = Application.CommandBars( _
"Worksheet menu bar"). _
Controls.Add(Type:=msoControlPopup, _
Temporary:=True)
cbpop.Caption = "Cu&stom"
' Set tag property to find it later for deletion
cbpop.Tag = "SRXUtilsCustomMenu"
' Add menu item to popup menu
With cbpop.Controls.Add(Type:=msoControlButton, _
Temporary:=True)
.Caption = "&ActivateSheet"
.OnAction = "ActivateSheet"
End With
End Sub
' *****************************************
' Chapter 12
Private Sub Workbook_Open()
Dim
Dim
Dim
Dim

lngPos As Long
objHelpMenu As CommandBar
objHelpMenuItem As CommandBarControl
objExcelAbout As CommandBarControl

'Get reference to Help menu


Set objHelpMenu = Application.CommandBars("Help")
' Determine position of "About Microsoft Excel"
Set objExcelAbout = objHelpMenu.Controls("About Microsoft Excel")

If Not objExcelAbout Is Nothing Then


lngPos = objExcelAbout.Index
Else
lngPos = objHelpMenu.Controls.Count
End If
' Add "About SRXUtils" menu item
Set objHelpMenuItem = objHelpMenu.Controls.Add(msoControlButton, 1, , lngPos, Tr
ue)
objHelpMenuItem.Caption = "About &SRXUtils"
objHelpMenuItem.BeginGroup = True
objHelpMenuItem.OnAction = "ShowAboutMacros"
End Sub
' **********************
Sub CreateToolbar()
Dim cbar As CommandBar, cbctl As CommandBarControl
' Delete if it exists
For Each cbar In Application.CommandBars
If cbar.Name = "Toolbar Example" Then cbar.Delete
Next
' Create a floating toolbar
Set cbar = Application.CommandBars.Add(Name:="Toolbar Example", _
Position:=msoBarFloating)
cbar.Visible = True
' Add a custom button control to execute a macro
Set cbctl = cbar.Controls.Add(Type:=msoControlButton)
cbctl.Visible = True
cbctl.Style = msoButtonCaption
cbctl.Caption = "CustomButton"
' Run the following macro
cbctl.OnAction = "ExampleMacro"
' Add built-in Open... control
Set cbctl = cbar.Controls.Add(Id:=23)
' Icon for button
cbctl.FaceId = 23
cbctl.Visible = True
' Add built-in spell checking button
Set cbctl = cbar.Controls.Add(Id:=2)
cbctl.FaceId = 2
cbctl.Visible = True
' Add a list box
Set cbctl = cbar.Controls.Add(Type:=msoControlDropdown)
' Add a tag so macro can find it
cbctl.Tag = "ComposerList"
cbctl.Visible = True
cbctl.Caption = "ListCaption"
' Set list properties of the list box

With cbctl
.AddItem "Chopin", 1
.AddItem "Mozart", 2
.AddItem "Bach", 3
.DropDownLines = 0
.DropDownWidth = 75
' select nothing to start
.ListIndex = 0
End With
' Set macro to execute when an item
' is selected
cbctl.OnAction = "ExampleListMacro"
End Sub
' **********************
Sub CreatePopup()
Dim cbpop As CommandBarControl
Dim cbctl As CommandBarControl
Dim cbsub As CommandBarControl
' Create a popup control on the main menu bar
Set cbpop = Application.CommandBars("Worksheet Menu Bar"). _
Controls.Add(Type:=msoControlPopup)
cbpop.Caption = "&Custom"
cbpop.Visible = True
' Add a menu item
Set cbctl = cbpop.Controls.Add(Type:=msoControlButton)
cbctl.Visible = True
' Next is required for caption
cbctl.Style = msoButtonCaption
cbctl.Caption = "MenuItem&1"
' Action to perform
cbctl.OnAction = "ExampleMacro1"
' Add a popup for a submenu
Set cbsub = cbpop.Controls.Add(Type:=msoControlPopup)
cbsub.Visible = True
cbsub.Caption = "&SubMenuItem1"
' Add a menu item to the submenu
Set cbctl = cbsub.Controls.Add(Type:=msoControlButton)
cbctl.Visible = True
' Next is required for caption
cbctl.Style = msoButtonCaption
cbctl.Caption = "SubMenuItem&2"
' Action to perform
cbctl.OnAction = "ExampleMacro2"
End Sub
' **********************
Public Sub ListControlIDs()
Dim fr As Integer
Dim cbar As Office.CommandBar
Dim ctl As CommandBarControl
Dim i As Integer
Const maxid = 4000
fr = FreeFile
Open "d:\temp\ids.txt" For Output As #fr
' Create temporary toolbar

Set cbar = Application.CommandBars.Add("temporary", msoBarTop, _


False, True)
For i = 1 To maxid
On Error Resume Next ' skip if cannot add
cbar.Controls.Add Id:=i
Next i
On Error GoTo 0
For Each ctl In cbar.Controls
Print #fr, ctl.Caption & " (" & ctl.Id & ")"
Next
cbar.Delete
Close #fr
End Sub
' *****************************************
' Chapter 19
Public Function GetUsedRange(ws As Worksheet) As Range
' Assumes that Excel's UsedRange gives a superset
' of the real used range.
Dim
Dim
Dim
Dim
Dim
Dim
Dim

s As String, x As Integer
rng As Range
r1Fixed As Integer, c1Fixed As Integer
r2Fixed As Integer, c2Fixed As Integer
i As Integer
r1 As Integer, c1 As Integer
r2 As Integer, c2 As Integer

Set GetUsedRange = Nothing


' Start with Excel's used range
Set rng = ws.UsedRange
' Get bounding cells for Excel's used range
' That is, Cells(r1,c1) to Cells(r2,c2)
r1 = rng.Row
r2 = rng.Rows.Count + r1 - 1
c1 = rng.Column
c2 = rng.Columns.Count + c1 - 1
' Save existing values
r1Fixed = r1
c1Fixed = c1
r2Fixed = r2
c2Fixed = c2
' Check rows from top down for all blanks.
' If found, shrink rows.
For i = 1 To r2Fixed - r1Fixed + 1
If Application.CountA(rng.Rows(i)) = 0 Then
' empty row -- reduce
r1 = r1 + 1
Else
' nonempty row, get out
Exit For
End If
Next

' Repeat for columns from left to right


For i = 1 To c2Fixed - c1Fixed + 1
If Application.CountA(rng.Columns(i)) = 0 Then
c1 = c1 + 1
Else
Exit For
End If
Next
' Reset the range
Set rng = _
ws.Range(ws.Cells(r1, c1), ws.Cells(r2, c2))
' Start
r1Fixed
c1Fixed
r2Fixed
c2Fixed

again
= r1
= c1
= r2
= c2

' Do rows from bottom up


For i = r2Fixed - r1Fixed + 1 To 1 Step -1
If Application.CountA(rng.Rows(i)) = 0 Then
r2 = r2 - 1
Else
Exit For
End If
Next
' Repeat for columns from right to left
For i = c2Fixed - c1Fixed + 1 To 1 Step -1
If Application.CountA(rng.Columns(i)) = 0 Then
c2 = c2 - 1
Else
Exit For
End If
Next
Set GetUsedRange = _
ws.Range(ws.Cells(r1, c1), ws.Cells(r2, c2))
End Function

Anda mungkin juga menyukai