简体   繁体   中英

VBA Range from String

This is kind of silly, but I've been stuck for a while in this simple statement:

    Dim range1 as Range
    Dim mysheet as String
    Dim myrange as String

    mysheet = "Sheet1"
    range = "A1:A10"

range1 = Worksheets(mysheet).Range(myrange)

I've testing all the solutions that I've found on the internet as for example this , this and this , but nothing.

All the time it gives me errors: 1004 "Error defined by the application" or "object variable or with not set".

I have tried the following:

range1 = ThisWorkbook.Worksheets(mysheet).Range(myrange)

range1 = ActiveWorkbook.Worksheets(mysheet).Range(myrange)

range1 = Sheets(mysheet).Range(myrange) (and the combinations above)

range1 = Worksheets(mysheet).Range(Cells(1,1), Cells(1,10)) (and the combinations with This/Active workbook)

and

with This/ActiveWorkbook
range1 = .Worksheets(mysheet).Range(myrange)
end with

None have worked.

This is a REALLY silly thing, but I've been stuck for a while now :s

Can anyone help me?

Really thanks in advance.

Best regards,

您需要使用Set来分配对象:

Set range1 = Worksheets(mysheet).Range(myrange)

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