Sub lastRowCode() lastRow = Cells(Rows.Count, <specified column>).End(xlUp).Row MsgBox lastRow End Sub
Sub lastColumnCode() lastColumn = Cells(<specified row>, Column.Count).End(xlToLeft).Column MsgBox lastColumn End Sub
Sub nextRowCode() nextRow = Cells(Row.Count, <specified column>).End(xlUp).Row + 1 MsgBox nextRow End Sub
Sub Macro1() Range("A9").Select ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B2:B9"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A9"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange Range("A1:B9") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With ActiveWorkbook.Save End Subthe macro below sorts dynamically
Sub dynamicSorting() Range("A1").Select lastRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear ' use the last row variable to dynamically set the range ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B2:B" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A9"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet1").Sort ' use the last row variable to dynamically set the range .SetRange Range("A1:B" & lastRow) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With ActiveWorkbook.Save End Sub
Sub absolute() Range("B4").Select ActiveCell.FormulaR1C1 = "hello there" Range("C5").Select End Subrelative reference starts from the selected cell
Sub relative() ActiveCell.Offset(3, 1).Select ActiveCell.FormulaR1C1 = "hello there" ActiveCell.Offset(1, 1).Select End Sub
Sub withoutWith() Range("C6").Value = 12 Range("C6").Font.Bold = True Range("C6").Font.Italic = True End Sub
Sub withWith() With Range("C6") .Value = 12 .Font.Bold = True .Font.Italic = True End With End Sub
Operator | Action |
---|---|
= | equals |
<> | not equal to |
< | less than |
> | greater than |
<= | less than or equal to |
>= | greater than or equal to |
Sub ifStatement() If Range("C6") = 12 Then MsgBox "C6 is equal to 12" End If End Sub
Sub ifStatement() If Range("C6") <> 12 Then MsgBox "C6 is not equal to 12" End If End Sub
Sub ifStatement() If Not Range("C6") = 12 Then MsgBox "C6 is not equal to 12" End If End Sub
Sub ifElseStatement() If Range("C6") = 12 Then MsgBox "C6 is equal to 12" Else MsgBox "C6 is not equal to 12" End If End Sub
Sub ifElseStatement() If Range("C6") = 12 Then MsgBox "C6 is equal to 12" ElseIf Range("C6") > 12 Then MsgBox "C6 is greater than 12" ElseIf Range("C6") < 12 Then MsgBox "C6 is less than 12" End If End Sub
Sub ifElseStatement() If Range("C6") = 12 Then MsgBox "C6 is equal to 12" ElseIf Range("C6") > 12 And IsNumeric(Range("C6")) Then MsgBox "C6 is greater than 12" ElseIf Range("C6") < 12 And IsNumeric(Range("C6")) Then MsgBox "C6 is less than 12" Else MsgBox "Please enter a number into C6." End If End Sub
Sub singleLineIf() If Range("C6") = 12 Then MsgBox "C6 is equal to 12" End Sub
Sub gotoExample() Goto myLabel ' code to be skipped ' colon in next line ids a label myLabel: End Sub
Sub mySwitch() Select Case Range("C6") Case 12 MsgBox "12" Case Is < 2 And IsNumeric(Range("C6")) MsgBox "less that 2" Case Else MsgBox "else" End Select End Sub
Sub myMsgBox() start: answer = MsgBox("Do you like excel VBA?", vbYesNo) If answer = vbYes Then MsgBox "yes" ElseIf answer = vbNo Then MsgBox "no" GoTo start End If End Sub
Sub offsetSub() Selection.Offset(3, 1) = Selection Cells(1, 1).Offset(4, 1) = "offset by 4,1" Range("E4").Offset(-3, -3) = "offset by -3,-3" End Sub
Function KGrams(lbs) KGrams = lbs * 0.453592 End Functionto calculate the kilograms for a cell call the function using the desired pounds cell as an arg as shown in the sixth row
A | B | |
---|---|---|
1 | Pounds | =KGrams() |
2 | 1 | 0.453592 |
3 | 13 | 5.986696 |
4 | 40 | 18.14368 |
5 | 20.25 | 9.185238 |
6 | 23 | =KGrams(A6) |
Function KGrams(lbs, Optional dplaces) If IsMissing(dplaces) Then KGrams = lbs * 0.453592 Else KGrams = Round(lbs * 0.453592, dplaces) End If End Functioncell and number of decimal places as args as shown in the sixth row
A | B | |
---|---|---|
1 | Pounds | =KGrams() |
2 | 1 | 0.45 |
3 | 13 | 5.99 |
4 | 40 | 18.14 |
5 | 20.25 | 9.19 |
6 | 23 | =KGrams(A6, 2) |
Sub Exercise6A() 'get last row lastRow = Cells(Rows.Count, 1).Row 'clear last report Range("A2:b" & lastRow).ClearContents Range("a2") = "1" Range("b2") = "Name1" Range("a3") = "2" Range("b3") = "Name2" Range("a4") = "3" Range("b4") = "Name3" End Subresolve problem using conditional
... If lastRow > 1 Then 'clear last report Range("A2:b" & lastRow).ClearContents End If ...
Sub exercise6b() selectedRow = Selection.Row lastRow = Cells(Rows.Count, 4).End(xlUp).Row If selectedRow <= lastRow And selectedRow > 1 Then answer = MsgBox("Add $100 to current row sales?", vbYesNo) If answer = vbYes Then sales = Cells(selectedRow, 4).Value Cells(selectedRow, 4).Value = sales + 100 End If End If End Sub