+ Reply to Thread
Results 1 to 4 of 4

Thread: VBA -autofill using LastRow error when only 1 record present

  1. #1
    Registered User
    Join Date
    03-25-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    Excel 2010
    Posts
    12

    VBA -autofill using LastRow error when only 1 record present

    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

  2. #2
    Extremely Helpful member
    Join Date
    12-23-2006
    Location
    Belgium
    MS-Off Ver
    XL2003
    Posts
    6,127

    Re: VBA -autofill using LastRow error when only 1 record present

    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

  3. #3
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: VBA -autofill using LastRow error when only 1 record present

    ...
    On error resume next   
    Range("AL2").AutoFill Destination:=Range("al2:al" & LastRow)
    On error goto 0
    ...
    End Sub
    You can force VB to ignore error and continue code execution further. Alternatively you can check LastRow value and take appropriate action:

    ...
    If LastRow > 2 then Range("AL2").AutoFill Destination:=Range("al2:al" & LastRow)
    ...
    End Sub
    Also nice practice to avoid Select in your code which will make your code much more effificent. Example:

    Range("AL2").Select
    ActiveCell.FormulaR1C1 = "=RC[2]/RC[1]"
    Instead it can be replaced with:
    [al2].FormulaR1C1 = "=RC[2]/RC[1]"

  4. #4
    Registered User
    Join Date
    03-25-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA -autofill using LastRow error when only 1 record present

    Thank you! That worked perfectly!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0