Vba Codes

Download Free Classic Office Menu-2003 Addins for Office-2007 from

http://shahshaileshs.web.officelive/menuaddins.aspx

Download Free Exshail Classic Menu for Excel-2007 from

http://shahshaileshs.web.officelive/Exshail_Classic_Menu.aspx

Sample VBA Codes

  1. UDF to put Sheet name in a Cell.
    1. Formula to get sheet name in cell.
      1. Formula to get sheet name in cell.
      2. Function GetShtName()
      3. With Application
      4. .Volatile (True)
      5. GetShtName = .Caller.Parent.Name
      6. End With
      7. End Function
      8. In any cell (e.g. A1) type below formula (Copy formula to other sheets)
      9. =GetShtName()
      10. Go Top
  2. To Show Sheet Activate Dialogbox
      1. When there is a lots of sheets & you want easy navigation use this methods.
      2. 1. Find inbuilt control for sheet activate & execute.
      3. Sub moresheets()
      4. CommandBars.FindControl(Type:= _
      5. msoControlButton, ID:=957).Execute
      6. End Sub
      7. 2. Run this macro to add control to standard toolbar &
      8. afterword you have to click this button to activate dialog.
      9. Sub AddControl()
      10. 'add button to commandbar for sheet activate dialogbox.
      11. Set cnt = CommandBars("Standard").Controls.Add _
      12. (Type:=msoControlButton, before:=1, ID:=957)
      13. End Sub
      14. 3. Using sendkeys: More then 16 sheet then show activate
      15. dialogbox else popup sheet name.
      16. Sub SheetActivate()
      17. 'assigne shortcut Key
      18. If Application.CommandBars("workbook tabs").Controls
      19. (16).Caption Like "More Sheets*" Then
      20. Application.SendKeys "{end}~"
      21. Application.CommandBars("workbook tabs").ShowPopup
      22. End Sub
      23. Go Top
  3. Position Custom Toolbar to Next of another Toolbar:
      1. 'Move Custom/InBuilt Toolbar on the screen Next to desired Toolbar.
      2. Sub test()
      3. 'Positionnext(source,destination)
      4. PositionNext "mytoolbarname", "standard"
      5. End Sub
      6. Function PositionNext(Source As String, Destination As String)
      7. 'position any commandbars to be next of.
      8. Dim DesTop As Long
      9. Dim DesLeft As Long
      10. Dim DesWidth As Long
      11. Dim DesRow As Long
      12. Dim DesPos As Long
      13. With Application.CommandBars(Destination)
      14. DesTop = .Top
      15. DesLeft = .Left
      16. DesWidth = .Width
      17. DesRow = .RowIndex
      18. DesPos = .Position
      19. End With
      20. With Application.CommandBars(Source)
      21. .Position = DesPos
      22. .RowIndex = DesRow
      23. .Top = DesTop
      24. .Left = DesLeft + DesWidth
      25. End With
      26. End Function
      27. Go Top
  4. Change Text Case in the Cells:
      1. Change Text to UPPERCASE or Title Case or lowercase using intellisense logic.
      2. Sub SelectCase() 'Assign Shortcut key
      3. Dim C As Range, rng As Range, cc As Integer
      4. If Selection.Count = 1 Then
      5. Set rng = Activecell
      6. Else
      7. Set rng = Selection.SpecialCells(xlCellTypeConstants).Cells
      8. End If
      9. Set C = rng(1)
      10. Select Case True
      11. Case C = LCase(C)
      12. cc = 1
      13. Case C = UCase(C)
      14. cc = 2
      15. Case Else
      16. cc = 3
      17. End Select
      18. Application.EnableCancelKey = xlErrorHandler
      19. On Error GoTo xit
      20. Application.EnableEvents = 0
      21. For Each C In rng
      22. With C
      23. .Formula = Choose(cc, UCase(.Formula), Application.Proper(.Formula), LCase(.Formula))
      24. End With
      25. Next C
      26. xit:
      27. Application.EnableEvents = 1
      28. End Sub
      29. Go Top
  5. List of Colour Index on the new Sheet
      1. Get ColorIndex of Colors from Active Workbook.
      2. Sub ColorList()
      3. 'Modified by Tom Ogilvy
      4. Dim colsht As Worksheet
      5. Dim xlcol As Integer
      6. On Error Resume Next
      7. Application.DisplayAlerts = False
      8. Worksheets("Col Index List").Delete
      9. Application.DisplayAlerts = True
      10. On Error GoTo 0
      11. Set colsht = Worksheets.Add
      12. Set rng = colsht.Range("A1:G8")
      13. With colsht
      14. On Error Resume Next
      15. .Name = "Col Index List"
      16. For xlcol = 1 To 56
      17. With rng(xlcol)
      18. .Interior.ColorIndex = xlcol
      19. .Value = xlcol
      20. Select Case xlcol
      21. Case 2, 6, 8, 19, 20, 28, 34, 35, 36, 40
      22. .Font.ColorIndex = 1
      23. Case Else
      24. .Font.ColorIndex = 2
      25. End Select
      26. End With
      27. Next
      28. End With
      29. End Sub
      30. Go Top
  6. Sort Sheet Tab
      1. When there are a lots of sheets & you want to sort Sheet Tabs.
      2. Change Worksheets to Sheets to sort all type of sheets.
      3. Sub QuickSortSheets(Optional SortOrder)
      4. Dim i As Long
      5. Dim j As Long
      6. Dim SheetsCount As Long
      7. Dim FirstSheet As String
      8. Dim NextSheet As String
      9. Dim LValue As String
      10. Dim HValue As String
      11. Dim VTemp As String
      12. Application.ScreenUpdating = 0
      13. SheetsCount = Worksheets.Count
      14. For i = 1 To SheetsCount \ 2
      15. FirstSheet = Worksheets(i).Name
      16. LValue = FirstSheet
      17. HValue = FirstSheet
      18. For j = i To SheetsCount - 1
      19. NextSheet = Worksheets(j + 1).Name
      20. If LValue > NextSheet Then LValue = NextSheet
      21. If HValue < NextSheet Then HValue = NextSheet
      22. Next
      23. If IsMissing(SortOrder) Then
      24. Else
      25. VTemp = LValue
      26. LValue = HValue
      27. HValue = VTemp
      28. End If
      29. If LValue <> FirstSheet Then Worksheets(LValue).Move before:=Worksheets(i)
      30. If HValue <> Worksheets(SheetsCount).Name Then Worksheets(HValue).Move after:=Worksheets(SheetsCount)
      31. SheetsCount = SheetsCount - 1
      32. Next
      33. Application.ScreenUpdating = 1
      34. End Sub
      35. Go Top
  7. Create Userform at Run-time:
      1. Copy below code to any standard module & run macro called "Runme"
      2. Option Explicit
      3. Dim myform
      4. Const bShow As Boolean = False '(True=show excel)
      5. Sub RunMe()
      6. 'Create userform at run-time for Print Preview.
      7. On Error GoTo PgmEnd
      8. Application.VBE.MainWindow.Visible = False
      9. Application.Visible = bShow
      10. 'designing UserForm1
      11. MyForm_Create
      12. MyForm_Controls_Create
      13. MyForm_Codes
      14. MyForm_Show
      15. PgmEnd:
      16. If Err.Number <> 0 Then
      17. MsgBox "Error No.:" & Err.Number & vbNewLine & _
      18. "Description : " & Err.Description, , "Print Preview'"
      19. End If
      20. MyForm_Remove 'To Keep userform1 then comment this line
      21. Application.Visible = 1
      22. End Sub
      23. Function MyForm_Create()
      24. MyForm_Remove ' If Userform1 exist then remove it.
      25. With ThisWorkbook.VBProject.VBComponents
      26. Set myform = .Add(3).Designer ' Create & set form designer
      27. End With
      28. End Function
      29. Function MyForm_Controls_Create()
      30. Dim cmd1 As MSForms.Control
      31. Dim lbox1 As MSForms.Control
      32. With myform.Controls
      33. Set lbox1 = .Add("Forms.listbox.1")
      34. Set cmd1 = .Add("Forms.CommandButton.1")
      35. End With
      36. With cmd1
      37. .Top = 80
      38. .Left = 40
      39. .Caption = "Preview"
      40. .ControlTipText = "Click to Preview selected Sheets"
      41. End With
      42. With lbox1
      43. .Top = 5
      44. .Left = 15
      45. .Width = 115
      46. .ColumnCount = 1
      47. .ControlTipText = "Click to Select/Unselect, Double Click to Activate Sheet"
      48. End With
      49. Set lbox1 = Nothing
      50. Set cmd1 = Nothing
      51. Set myform = Nothing
      52. End Function
      53. Function MyForm_Codes()
      54. Dim MyCtlEvents As Integer
      55. With ThisWorkbook.VBProject.VBComponents("Userform1").CodeModule
      56. If bShow Then
      57. MyCtlEvents = .CreateEventProc("Click", "CommandButton1")
      58. .InsertLines MyCtlEvents + 1, "Print_Job"
      59. MyCtlEvents = .CreateEventProc("DblClick", "Listbox1")
      60. .InsertLines MyCtlEvents + 1, "Sht_Activate"
      61. MyCtlEvents = .CreateEventProc("Click", "Userform")
      62. .InsertLines MyCtlEvents + 1, "Msgbox ""By Shailesh Shah"",,""Print Preview"" "
      63. MyCtlEvents = .CreateEventProc("Initialize", "Userform")
      64. .InsertLines MyCtlEvents + 1, "Lbox1_Fill"
      65. Else
      66. .InsertLines 1, "Sub CommandButton1_Click()"
      67. .InsertLines 2, "Print_Job"
      68. .InsertLines 3, "End Sub"
      69. .InsertLines 1, "Sub Listbox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)"
      70. .InsertLines 2, "Sht_Activate"
      71. .InsertLines 3, "End Sub"
      72. .InsertLines 1, "Sub Userform_Click"
      73. .InsertLines 2, "Msgbox ""Print Preview:- By Shailesh Shah"",,""About"""
      74. .InsertLines 3, "End Sub"
      75. .InsertLines 1, "Sub Userform_Initialize"
      76. .InsertLines 2, "Lbox1_fill"
      77. .InsertLines 3, "End Sub"
      78. End If
      79. End With
      80. End Function
      81. Function MyForm_Show()
      82. With UserForm1
      83. .Caption = "Print Preview"
      84. .Height = 125
      85. .Width = 150
      86. .Show
      87. End With
      88. End Function
      89. Function MyForm_Remove()
      90. On Error Resume Next
      91. With ThisWorkbook.VBProject
      92. .VBComponents.Remove .VBComponents("Userform1")
      93. End With
      94. Err.Clear
      95. End Function
      96. Function LBox1_Fill()
      97. Dim sht As Variant
      98. With UserForm1.ListBox1
      99. For Each sht In Sheets
      100. On Error Resume Next
      101. If sht.Visible And Application.CountA(sht.Cells) > 0 Then
      102. .AddItem sht.Name
      103. .MultiSelect = 1
      104. If sht.Name = ActiveSheet.Name Then
      105. .Selected(.ListCount - 1) = True
      106. .ListIndex = .ListCount - 1
      107. End If
      108. End If
      109. Err.Clear
      110. Next sht
      111. If .ListCount = 0 Then
      112. UserForm1.CommandButton1.Visible = 0
      113. .AddItem "No Sheets found to Preview."
      114. Else
      115. .TopIndex = .ListIndex
      116. End If
      117. End With
      118. End Function
      119. Function Print_Job()
      120. Dim i As Integer
      121. With UserForm1
      122. .Hide
      123. Application.Visible = 1
      124. For i = 0 To .ListBox1.ListCount - 1
      125. If .ListBox1.Selected(i) Then
      126. Application.ScreenUpdating = 0
      127. Sheets(.ListBox1.List(i)).PrintPreview
      128. End If
      129. Next i
      130. Application.ScreenUpdating = bShow
      131. Application.Visible = bShow
      132. .Show
      133. End With
      134. End Function
      135. Function Sht_Activate()
      136. On Error Resume Next
      137. With UserForm1.ListBox1
      138. Sheets(.List(.ListIndex)).Activate
      139. End With
      140. Err.Clear
      141. Unload UserForm1
      142. MyForm_Remove 'To Keep userform1 then comment this line
      143. Application.Visible = 1
      144. End Function
      145. Go Top
  8. UDF for Sentence Case for cells:
      1. Usage = "=wdsentcase(A1)"
      2. Where A1 is the reference cell
      3. Function WdSentCase(mytext As String)
      4. Dim WdDocTemp As Object
      5. Set WdDocTemp = CreateObject("word.document")
      6. With WdDocTemp.Application.Selection
      7. .Text = mytext
      8. .Range.Case = 4
      9. WdSentCase = .Range
      10. .Parent.Parent.Close 0
      11. End With
      12. Set WdDocTemp = Nothing
      13. End Function
      14. To work this UDF MS word should also be installed.
      15. to use with macro
      16. Sub SentCase() 'Assign Shortcut key
      17. Dim C As Range, rng As Range
      18. If Selection.Count = 1 Then
      19. Set rng = ActiveCell
      20. Else
      21. Set rng = Selection.SpecialCells(xlCellTypeConstants).Cells
      22. End If
      23. Application.Calculation = xlCalculationManual
      24. Application.EnableEvents = 0
      25. For Each C In rng
      26. With C
      27. .Formula = WdSentCase(.Formula)
      28. End With
      29. Next C
      30. Application.Calculation = xlCalculationAutomatic
      31. Application.EnableEvents = 1
      32. End Sub
      33. Go Top

All rights reserved by Exshail Software