I am currently trying to improve some processes and am looking to automate some formula entry on a workbook exported from an Access database.
I was able to get the LastRow function to help me autofill but am having issues when the macro encounters a sheet which only contains 1 row. Code is below and I am attempting to use this in two places: AL and AS. The macro needs to cycle through a workbook with 200+ sheets and needs to be able to handle the smallest dataset, 1. Thank you in advance.
Sub FormatWorksheet() Dim LastRow As Long LastRow = Cells(Rows.Count, "aj").End(xlUp).Row ' ' FormatWorksheet Macro Columns("AQ:AR").Select Selection.Cut Columns("AL:AL").Select Selection.Insert Shift:=xlToRight Columns("AL:AL").Select Selection.Insert Shift:=xlToRight Range("AL1").Select ActiveCell.FormulaR1C1 = "Discount" Range("AL2").Select ActiveCell.FormulaR1C1 = "=RC[2]/RC[1]" Columns("AL:AL").Select Selection.NumberFormat = "0.00%" Range("AL2").AutoFill Destination:=Range("al2:al" & LastRow) Range("AG1").Activate Application.CutCopyMode = False Selection.Sort Key1:=Range("AC2"), Order1:=xlAscending, Key2:=Range("Q2") _ , Order2:=xlAscending, Key3:=Range("AH2"), Order3:=xlAscending, Header _ :=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _ xlSortTextAsNumbers Cells.Select Selection.ColumnWidth = 9.43 Cells.EntireColumn.AutoFit Range("As2").Select ActiveCell.FormulaR1C1 = "=RC[-1]/RC[-9]" Range("As2").AutoFill Destination:=Range("as2:as" & LastRow) Columns("As:As").Select Selection.FormatConditions.Delete Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _ Formula1:="0" With Selection.FormatConditions(1).Font .Bold = True .Italic = False .ColorIndex = 3 End With End Sub
Last edited by bk77; 11-29-2010 at 05:49 PM. Reason: fixed for rule 3
Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
Quoting entire posts clutters the forum and makes threads hard to read !
If you are pleased with a member's answer then use the Star icon to rate it
Click here to see forum rules
You can force VB to ignore error and continue code execution further. Alternatively you can check LastRow value and take appropriate action:... On error resume next Range("AL2").AutoFill Destination:=Range("al2:al" & LastRow) On error goto 0 ... End Sub
Also nice practice to avoid Select in your code which will make your code much more effificent. Example:... If LastRow > 2 then Range("AL2").AutoFill Destination:=Range("al2:al" & LastRow) ... End Sub
Instead it can be replaced with:Range("AL2").Select ActiveCell.FormulaR1C1 = "=RC[2]/RC[1]"[al2].FormulaR1C1 = "=RC[2]/RC[1]"
Thank you! That worked perfectly!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks