Friday, March 15, 2019

VBA Code to How to Sort Data by Single Column

Open Excel Sheet

Press Alt + F11 Key 


Go to the Insert Menu and Click on new Module


in New Module copy and paste the above VBA code and save.


Now Press Alt + F8  and new windows will open which is shown in below figure,


in that windows you will ask to enter a key to create shortcut for Paste Special


Enter a letter in the box. 


Sub SortDataHeader()
Range("DataRange").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
End Sub

VBA Code to Highlight Blank Cells With VBA

Open Excel Sheet

Press Alt + F11 Key 


Go to the Insert Menu and Click on new Module


in New Module copy and paste the above VBA code and save.


Now Press Alt + F8  and new windows will open which is shown in below figure,


in that windows you will ask to enter a key to create shortcut for Paste Special


Enter a letter in the box. 



Sub HighlightBlankCells()
Dim Dataset as Range
Set Dataset = Selection
Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed

End Sub

VBA Macro to Highlight All Cells With Comments

Open Excel Sheet

Press Alt + F11 Key 


Go to the Insert Menu and Click on new Module


in New Module copy and paste the above VBA code and save.


Now Press Alt + F8  and new windows will open which is shown in below figure,


in that windows you will ask to enter a key to create shortcut for Paste Special


Enter a letter in the box. 



Sub HighlightCellsWithComments()
ActiveSheet.Cells.SpecialCells(xlCellTypeComments).Interior.Color = vbBlue

End Sub

VBA Code to Refresh All Pivot Tables in the Workbook

Open Excel Sheet

Press Alt + F11 Key 


Go to the Insert Menu and Click on new Module


in New Module copy and paste the above VBA code and save.


Now Press Alt + F8  and new windows will open which is shown in below figure,


in that windows you will ask to enter a key to create shortcut for Paste Special


Enter a letter in the box. 





Sub RefreshAllPivotTables()
Dim PT As PivotTable
For Each PT In ActiveSheet.PivotTables
PT.RefreshTable
Next PT
End Sub

VBA Code to Change the Letter Case of Selected Cells to Upper Case

Open Excel Sheet

Press Alt + F11 Key 


Go to the Insert Menu and Click on new Module


in New Module copy and paste the above VBA code and save.


Now Press Alt + F8  and new windows will open which is shown in below figure,


in that windows you will ask to enter a key to create shortcut for Paste Special


Enter a letter in the box. 



Sub ChangeCase()
Dim Rng As Range
For Each Rng In Selection.Cells
If Rng.HasFormula = False Then
Rng.Value = UCase(Rng.Value)
End If
Next Rng

End Sub

VBA Code to hide rows with previous dates in excel | Macro to hide rows based on dates in excel



VBA Code to hide rows with previous dates in excel | Macro to hide rows based on dates in excel


Open Excel Sheet

Press Alt + F11 Key 


Go to the Insert Menu and Click on new Module


in New Module copy and paste the above VBA code and save.


Now Press Alt + F8  and new windows will open which is shown in below figure,


in that windows you will ask to enter a key to create shortcut for Paste Special


Enter a letter in the box. 
Sub Hide_Dates()

Dim MyRange As Range, c As Range

Set MyRange = Range("H2:H4436")

MyRange.EntireRow.Hidden = False

For Each c In MyRange

    If IsDate(c.Value) And c.Value < Date Then

        c.EntireRow.Hidden = True

    End If

Next

End Sub

VBA Code to hide rows with previous dates in excel | Macro to hide rows based on dates in excel

VBA Code to hide rows with previous dates in excel | Macro to hide rows based on dates in excel

VBA Code to hide rows with previous dates in excel | Macro to hide rows based on dates in excel




Before applying Macro, follow the steps to apply Macro on Excel.


Open Excel Sheet

Press Alt + F11 Key 


Go to the Insert Menu and Click on new Module


in New Module copy and paste the above VBA code and save.


Now Press Alt + F8  and new windows will open which is shown in below figure,


in that windows you will ask to enter a key to create shortcut for Paste Special


Enter a letter in the box. 
Sub Hide_Dates()

Dim MyRange As Range, c As Range

Set MyRange = Range("H2:H4436")

MyRange.EntireRow.Hidden = False

For Each c In MyRange

    If IsDate(c.Value) And c.Value < Date Then

        c.EntireRow.Hidden = True

    End If

Next

End Sub