来自字符串的 VBA 范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10403794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:04:26  来源:igfitidea点击:

VBA Range from String

excelvbaexcel-vba

提问by Christian Vielma

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, thisand this, but nothing.

我已经测试了我在互联网上找到的所有解决方案,例如thisthisthis,但什么都没有。

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

它总是给我错误:1004“应用程序定义的错误”或“对象变量或未设置”。

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

这是一件非常愚蠢的事情,但我已经被困了一段时间:s

Can anyone help me?

谁能帮我?

Really thanks in advance.

真的提前致谢。

Best regards,

此致,

回答by assylias

You need to use Set to assign objects:

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

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