简体   繁体   English

使用像元值作为范围VBA

[英]Using cell value as range VBA

I am trying to have a range (call it "A5:L10") be picked up from a cell. 我正在尝试从一个单元格中拾取一个范围(称为“ A5:L10”)。 In other words, my code looks something similar to that below: 换句话说,我的代码看起来类似于下面的代码:

  Dim summ_rng1 As String
  summ_rng1 = Sheet11.Cells(17, 3).Value

  Workbooks(wb).Sheets(summ).Range(summ_rng1).......

Where summ_rng1 = "A5:L10" 其中summ_rng1 =“ A5:L10”

I have done this same thing for the workbooks and sheets in my code and it works fine, but when I try to replace the range reference with the variable summ_rng1 it does not work. 我在代码中对工作簿和工作表做了同样的事情,并且工作正常,但是当我尝试用变量summ_rng1替换范围引用时,它不起作用。

Any idea on how to get the code to run with the range value as a variable, like that above? 关于如何使代码以范围值作为变量运行的任何想法,像上面那样吗? Thanks for the help! 谢谢您的帮助!

Your code is working for me. 您的代码对我有用。 I think it is related to your wb object, which may contain the workbook itself, rather than the name of the workbook. 我认为它与您的wb对象有关,该对象可能包含工作簿本身,而不是工作簿的名称。 Try this : 尝试这个 :

Sub testSub()
Dim myRange As String
Dim wb As Workbook
Set wb = ThisWorkbook

myRange = wb.Sheets(1).Cells(1, 1)

wb.Sheets(1).Range(myRange).Select
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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