简体   繁体   中英

Excel VBA Range Variable Throws Error 1004 - I'm in

I need your help please. I am new to using ranges as variables, so there maybe something obvious I'm missing but I can't seem to find a solution after a lot of googling.

I am formatting four sheets of data (headings, pretty fill colour, nice borders). They are all pretty much the same, but they have a varying number of columns. To save repetitious code I've written one procedure to do the formatting and another to change the variables and call the formatting code.

sample of the calling code:

' Set Customer detail variables.

varGlobalID = Sheets(varWST1Dockets).Cells(2, 13).Value
varCustomerName = Sheets(varWST1Dockets).Cells(2, 14).Value

' Format Suspended

' Set Variables

    varReportHeading = "Suspended Dockets Investigation"

    Set rngDataHeadings = Range("B11", "T11")

    Range("B1048576").End(xlUp).Select
    Set rngDataTable = Range(Selection, "T11")

    Range("B1048576").End(xlUp).Select
    Set rngData = Range(Selection, "T12")

' Run Format Reports Procedure

    Sheets(varWSSuspended).Select
    Call FormatReports

sample of formatting code

' Format Data Headings

rngDataHeadings.Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = -4300032
    .PatternTintAndShade = 0
End With
With Selection.Font
    .ColorIndex = 2
    .TintAndShade = 0
    .Bold = True
End With
With Selection
    .HorizontalAlignment = xlLeft
    .VerticalAlignment = xlCenter
    .WrapText = True
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With

' Apply Borders

rngDataTable.Select

With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 2
    .TintAndShade = 0
    .Weight = xlMedium
End With

The code seems to work on the first run of the variables but not the second. Do I need to unload them before resetting? Or am I doing something else stupidly obviously wrong?

Thanks in advance.

Set rngDataHeadings = Range("B11", "T11") references B11:T11 of the ActiveSheet. Selecting another worksheet and try rngDataHeadings.Select will throw an exception Runtime Error '1004' Select method of Range class failed

It's best to avoid Select and Active. You should watch Selecting Cells (Range, Cells, Activecell, End, Offset)

If you have standard tables this will work.

Sub FormatTable(wsWorksheet As Worksheet, HeaderAddress As String)
    Dim rDataBody As Range
    Dim rHeader As Range

    With wsWorksheet
        Set rHeader = .Range(HeaderAddress, .Range(HeaderAddress).End(xlToRight))
        Set rDataBody = Range(HeaderAddress).CurrentRegion
        Set rDataBody = rDataBody.Offset(1).Resize(rDataBody.Rows.Count - 1)
    End With
    With rHeader.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = -4300032
        .PatternTintAndShade = 0
    End With
    With rHeader.Font
        .ColorIndex = 2
        .TintAndShade = 0
        .Bold = True
    End With
    With rHeader
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With

    ' Apply Borders
    With rDataBody.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 2
        .TintAndShade = 0
        .Weight = xlMedium
    End With

End Sub

Call it like this

FormatTable Worksheets("Sheet1"), "B11"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM