简体   繁体   中英

Excel, VBA, .ClearContents with referenced range, error 1004

I'm trying to use.clearcontents on range that is referenced by with some.offset, and I'm having trouble

I know that this works

Sub clear1_1()

Workbooks("xyz").Sheets("abc").range("A2:A3").ClearContents

End Sub

but if I try this it does not

Sub clear2()

Dim region As range
Set region = range("S509:AD618")
Workbooks("xyz").Worksheets("abc").range(region).ClearContents

end sub

I do understand from other postings, that it has something to do with object defyining, but I have no idea where I do mistake, what I need to write.

Final macro is run from one workbook, and is supposed to.clearcontents in other not activated workbook.

My code looks like this

sub Macro()

..... ton of code
Dim filename as string
dim sheetname as string
dim address3, address4 as string

filename = "xyz"
sheetname = "abc"  ' both variables that are loaded in other part

address3 and address4 loaded in other part


'here is where i get the error
sheets(sheetname).Range(Range(address3).Offset(0, 12).Address & ":" & Range(address4).Offset(-1, 23)).ClearContents


end sub

I can probably bypass it with.value="" But I'm looking to learn. Thank you for any response in advance.

EDIT 1 Hi Scott, doesn't make it. Posting bigger part of my code

If mapanchorsuccess = True And map1success = True And map2success = True Then
    If Workbooks(Filename).Sheets(startws).Range(address1).Offset(10, 13).HasFormula = True Then
                With Workbooks(wbm).Sheets("Report") 'report
                    .Range("A" & reportrow).Value = runnumber
                    .Range("B" & reportrow).Value = Filename
                    .Range("C" & reportrow).Value = Workbooks(Filename).Sheets(startws).Name
                    .Hyperlinks.Add anchor:=.Range("D" & reportrow), Address:=FilePath & Filename, SubAddress:=Workbooks(Filename).Sheets(startws).Name & "!A1"
                    .Range("E" & reportrow).Value = "Error"
                    .Range("F" & reportrow).Value = "rolling probably done already in this sheet"
                    reportrow = reportrow + 1
                End With

    Else
        With Workbooks(Filename).Sheets(startws)
            .Range(Range(address1).Offset(0, 12).Address & ":" & Range(address2).Offset(0, 14).Address).Copy _
            Range(Range(address1).Address & ":" & Range(address2).Offset(0, 2).Address)
                Application.CutCopyMode = False

            .Range(Range(address1).Offset(0, 16).Address & ":" & Range(address2).Offset(0, 16).Address).Copy _
            Range(Range(address1).Offset(0, 3).Address & ":" & Range(address2).Offset(0, 23).Address)
                Application.CutCopyMode = False

                    With Workbooks(wbm).Sheets("Report") 'report
                        .Range("A" & reportrow).Value = runnumber
                        .Range("B" & reportrow).Value = Filename
                        .Range("C" & reportrow).Value = Workbooks(Filename).Sheets(startws).Name
                        .Hyperlinks.Add anchor:=.Range("D" & reportrow), Address:=FilePath & Filename, SubAddress:=Workbooks(Filename).Sheets(startws).Name & "!A1"
                        .Range("E" & reportrow).Value = "Completed"
                        .Range("F" & reportrow).Value = "region1 rolled forward"
                        reportrow = reportrow + 1
                    End With


            .Range(Range(address3).Offset(0, 12).Address & ":" & Range(address4).Offset(-1, 23).Address).Copy _
            Range(Range(address3).Address & ":" & Range(address4).Offset(-1, 11).Address)

            '///// here the error 1004 occurs
            .Range(Range(address3).Offset(0, 12).Address & ":" & Range(address4).Offset(-1, 23).address).clearcontent
        End With
    End If
End If

The workbook and sheet need to be set with the variable.

Then when using it since it is a range itself just refer to it.

Sub clear2()

Dim region As range
Set region = Workbooks("xyz").Worksheets("abc")range("S509:AD618")
region.ClearContents

end sub

As to your next code; that is a different problem. The ranges inside the () need to allocated to the correct sheet parentage or it will use the active sheet.

The easiest is with a With block:

With sheets(sheetname)
    .Range(.Range(address3).Offset(0, 12), .Range(address4).Offset(-1, 23)).ClearContents
End With

I had this same issue, but it turned out to be very simple. I had a row of cells merged together between columns E and F , so when I used this command I had to set the ClearContents from the top corner of my E column to the bottom row of my F column.

What did not work:

Range("E1:E10").Clear Contents

What did work:

Range("E1:F10").ClearContents

I can't believe such a simple thing left me so thwarted.

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