Sample Macros
-
Upload
anonymous-cikyr0t -
Category
Documents
-
view
215 -
download
0
Transcript of Sample Macros
-
8/11/2019 Sample Macros
1/6
Unlocking few cells.................................................................................................................. 1Locking all cells in the sheet .................................................................................................. 1Unlocking few cells.................................................................................................................. 1Protecting the sheet ................................................................................................................ 1Un-protecting the sheet .......................................................................................................... 2Inserting a row above row 4.................................................................................................. 2
Formatting row 4 same as row 3.......................................................................................... 2Sorting out Data with headers wrt heading in A1 ............................................................... 2Sorting on two fields ascending or descending............................................................... 2Copying & Pasting row ........................................................................................................... 2Clearing contents of few cells............................................................................................... 3Retreiving column number or row number of the selected cell........................................ 3Selecting entire row and deleting row .................................................................................. 3Checking if the cell has formula ............................................................................................ 3Selecting another sheet......................................................................................................... 3Running another macro from within the macro.................................................................. 3Adding formula ......................................................................................................................... 3Adding current date and time ................................................................................................ 4
Hiding Columns / Un-hiding columns ................................................................................... 4Sample macro to hide selected rows................................................................................... 4Selecting all cells and paste special values ........................................................................ 4Checking the error values in cells......................................................................................... 5Subroutine for deleting rows based on error values.......................................................... 5Inserting columns .................................................................................................................... 5Deleting column ....................................................................................................................... 6Formatting column as 0.0...................................................................................................... 6Changing width of a column and applying centre format................................................. 6
Unlocking few cells
Range("A3,B3,C3,E3,F3").SelectSelection.Locked = FalseSelection.FormulaHidden = False
Locking all cells in the sheet
Cells.SelectSelection.Locked = TrueSelection.FormulaHidden = True
[Thus the formulas will not be visible]
Unlocking few cells
Cells(rno, 1).Locked = False
Protecting the sheet
-
8/11/2019 Sample Macros
2/6
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Un-protecting the sheet
ActiveSheet.Unprotect
Inserting a row above row 4
Rows("4:4").SelectSelection.Insert Shift:=xlDown
Formatting row 4 same as row 3
Rows("3:3").SelectSelection.CopyRows("4:4").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _SkipBlanks:=False, Transpose:=FalseApplication.CutCopyMode = False
Sorting out Data with headers wrt heading in A1
Columns("A:AZ").SelectSelection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _DataOption1:=xlSortNormal
Sorting on two fields ascending or descending
Range("A:G").select.
Selection.Sort Key1:=Range("D2"), Order1:=xlDescending, Key2:= _Range("E2"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, _MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _DataOption2:=xlSortNormal
Copying & Pasting row
Rows("2:2").SelectSelection.CopyRows("3:3").SelectActiveSheet.PasteApplication.CutCopyMode = False
Example 2
-
8/11/2019 Sample Macros
3/6
Rows("16:30").SelectSelection.CopyCells(rno, 1).SelectActiveSheet.Paste
Clearing contents of few cells
Range("A3,B3,C3,E3,F3").SelectSelection.ClearContents
Cells(rno, 1).ClearContents
Retreiving column number or row number of the selected cell
cno = ActiveCell.Cells.Columnrno = ActiveCell.Cells.Row
Selecting entire row and deleting row
Cells(rno, cno).EntireRow.SelectSelection.Delete Shift:=xlUp
Checking if the cell has formula
If Cells(rno, 4).HasFormula = True Then
Selecting another sheet
Sheets("Associates").Select
Running another macro from within the macro
Application.Run "'2 Digits L3.xls'!AddCols1"
Application.Run "AddCols1"
Adding formula
Cells(rnoA, cnoA + 1).SelectActiveCell.FormulaR1C1 = "=RC[-1] + " & Weeks
Cells(rno, 12).SelectActiveCell.FormulaR1C1 = "=IF(RC[-5]="""","""",IF(RC[-7]=""JR"","""",IF(RC[-7]=""EG"","""",RC[-1] + " & 84 & ")))"
-
8/11/2019 Sample Macros
4/6
ActiveCell.FormulaR1C1 = "=R[-1]C+R[-1]C[-1]"
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],Details!C[-9]:C[-7],2,FALSE)"
Adding current date and time
Cells(14, 9) = DateCells(15, 9) = Time
Hiding Columns / Un-hiding columns
Columns("A:EU").SelectSelection.EntireColumn.Hidden = False
Range( _
"B:K,M:N,Q:Z,AB:AC,AF:AO,AQ:AR,AU:BD,BF:BG,BJ:BS,BU:BV,BY:CH,CJ:CK,CN:CW,CY:CZ,DC:DL,DN:DO,DR:EA,EC:ED,EG:EP,ER:ES" _
).SelectSelection.EntireColumn.Hidden = True
Sample macro to hide selected rows
' Hiding selected rows if Amount not deposited
If Cells(rno + 9, 3) = 1 Then
rnoU = 2
Do While rnoU < rno
If Cells(rnoU, 23) = 0 ThenCells(rnoU, 23).EntireRow.SelectSelection.EntireRow.Hidden = FalseElseCells(rnoU, 23).EntireRow.SelectSelection.EntireRow.Hidden = TrueEnd If
rnoU = rnoU + 1
Loop
End If
Selecting all cells and paste special values
-
8/11/2019 Sample Macros
5/6
Cells.SelectSelection.CopySelection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=FalseApplication.CutCopyMode = False
Checking the error values in cells
Create a cell that has an error
Cells(rno, 14).SelectActiveCell.FormulaR1C1 = "=RC[-12] - 1"
IF IsError(Cells(rno, 36)) = True Then
Else
Cells(rno, 24).SelectActiveSheet.Paste
End If
Subroutine for deleting rows based on error values
Do While rno
-
8/11/2019 Sample Macros
6/6
Deleting column
Columns("AK:AK").SelectSelection.Delete Shift:=xlToLeft
Formatting column as 0.0
Columns("C:C").SelectSelection.NumberFormat = "0.0"
Changing width of a column and applying centre format
Columns("A:A").SelectSelection.ColumnWidth = 5.29Columns("A:A").SelectWith Selection
.HorizontalAlignment = xlCenterEnd With
*Better way to give column width
Columns("W:W").ColumnWidth = 40.14