[英]How to change the value of a cell with Excel vba using a cell address that's inside a cell in another worksheet?
We have two worksheets:我们有两个工作表:
Suppose the cell d2 in the Reference sheet contains the value $a$25 .假设参考表中的单元格 d2 包含值 $a$25 。
How to change the value of the cell $a$25 in the Operator sheet using Excel VBA (referencing the address from d2 in the Reference sheet)?如何使用 Excel VBA 更改运算符表中单元格 $a$25 的值(引用参考表中 d2 的地址)?
What I have tried:我试过的:
Dim NewRNG as Range
NewRNG = Range(Range("d2").value).value
No luck here.这里没有运气。
Sub Find_Cell()
Dim RNG As Range
Dim NewRNG as Range
With Sheets("Operator").Range("A:A")
Set RNG = .Find(What:=Sheets("Reference").Range("d2"), _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
NewRNG = RNG.Address
NewRNG.select
End Sub
**in this case, i can't set rng.address to a variable **在这种情况下,我不能将 rng.address 设置为变量
Like this:像这样:
Dim addr As String
addr = Sheets("Reference").Range("D2").Value 'get the cell address
Sheets("Operator").Range(addr).Value = "Test" 'pass the address to Range()
To define the NewRNG variable you have to use "Set"!要定义 NewRNG 变量,您必须使用“Set”!
Option Explicit
Sub setRange()
Dim NewRNG As Range
Set NewRNG = Range(Range("B2").Value)
Range("B5").Value = NewRNG.Address
Range("B6").Value = NewRNG.Value
Sheets("Operator").Range(Range("B2").Value) = "Test"
End Sub
Sub setRange2() ' variant including a check and error handling
Dim NewRNG As Range
On Error Resume Next
Set NewRNG = Range(Range("B2").Value)
If Err() Then
MsgBox ("You need to set a Range definition (e.g. ""B3"") in field B2!")
Exit Sub
End If
On Error GoTo 0
Range("B5").Value = NewRNG.Address
Range("B6").Value = NewRNG.Value
Sheets("Operator").Range(Range("B2").Value) = "Test"
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.