Useful Excel VBA Codes
Useful Excel VBA Codes
Useful Excel VBA Codes
UPPER CASE
LOWER CASE
PROPER CASE
Option Explicit
Application.EnableEvents = False
‘Sub Procedure to Center and Apply a Supplied Font Size to the Selected Range
Sub Format_Centered_And_Sized(Optional iFontSize As Integer = 10)
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
Selection.Font.Size = iFontSize
End Sub
}}
Sub CopyFilteredCells()
'Updateby20150203
Dim rng1 As Range
Dim rng2 As Range
Dim InputRng As Range
Dim OutRng As Range
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Copy Range :", xTitleId,
InputRng.Address, Type: = 8)
Set OutRng = Application.InputBox("Paste Range:", xTitleId, Type: = 8)
For Each rng1 In InputRng
rng1.Copy
For Each rng2 In OutRng
If rng2.EntireRow.RowHeight > 0 Then
rng2.PasteSpecial
Set OutRng = rng2.Offset(1).Resize(OutRng.Rows.Count)
Exit For
End If
Next
Next
Application.CutCopyMode = False
End Sub
VBA CODE TO SORT TABLE ACCORDINGLY SELECTED COLUMN RANGE
Sub sbSortDataInExcelInDescendingOrder()
.SetRange Range(strDataRange)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
UPPER CASE VBA CODE (Fully Working with copy & paste also)
''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''
Application.EnableEvents = False
Target = UCase(Target)
Application.EnableEvents = True
End If
On Error GoTo 0
End Sub
FILL IN THE BLANK CELLS IN SELECTED RANGE
Sub FillEmptyBlankCellWithValue()
Dim cell As Range
Dim InputValue As String
On Error Resume Next
InputValue = InputBox("Enter value that will fill empty cells in
selection", _
"Fill Empty Cells")
For Each cell In Selection
If IsEmpty(cell) Then
cell.Value = InputValue
End If
Next
End Sub
End Sub
Option Explicit
Sub EnableInsertDeleteRowsCols()
Dim ctrl As CommandBarControl
''
'Disable "Row" Delete.
For Each ctrl In Application.CommandBars.FindControls(ID:=293)
ctrl.Enabled = True
Next ctrl
'Disable "Column" Delete.
For Each ctrl In Application.CommandBars.FindControls(ID:=294)
ctrl.Enabled = True
Next ctrl
''
'Disable "Row" and "Column" Insert.
For Each ctrl In Application.CommandBars.FindControls(ID:=3183)
ctrl.Enabled = True
Next ctrl
''
'Disable "Cell" Delete.
For Each ctrl In Application.CommandBars.FindControls(ID:=292)
ctrl.Enabled = True
Next ctrl
'Disable "Cell" Insert.
For Each ctrl In Application.CommandBars.FindControls(ID:=3181)
ctrl.Enabled = True
Next ctrl
End Sub