简体   繁体   English

如何使用另一个工作表中单元格内的单元格地址更改具有 Excel vba 的单元格的值?

[英]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:我们有两个工作表:

  1. Reference参考
  2. Operator操作员

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:我试过的:

  1. Capturing the reference in d2 with a variable:使用变量捕获 d2 中的引用:
    Dim NewRNG as Range
    NewRNG = Range(Range("d2").value).value

No luck here.这里没有运气。

  1. Finding the reference cell in Operator and using the found cell's address在 Operator 中查找参考单元格并使用找到的单元格的地址
    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.

相关问题 如果单元格值包含在另一个工作表中,则为Excel vba - Excel vba if cell value is included in another worksheet Excel Vba,如何用另一个单元格更改单元格值? - Excel Vba, How to change cell value with another cell? 如何使用 vba 根据 Excel 中另一张纸上的列表更改单元格的值? - How to change a cell's value using vba based off a list on another sheet in Excel? Excel VBA - 使用单元格中的值作为地址来定位另一个单元格 - Excel VBA - Use value in cell as address to target another cell 如何使用 VBA 从 MS Access 更改 Excel 工作表中单元格的值? - How to I change a value of a cell in an Excel worksheet from MS Access using VBA? 使用Excel VBA删除地址位于另一个单元格中的单元格 - Delete cell in which address is in another cell using excel vba 如何使用参考单元格的值对另一个工作表中的excel数据求和 - How to sum excel data in another worksheet, using the value of a reference cell 如何使用VBA在Excel中逐个单元格地将信息从一个工作表剪切并粘贴到另一个工作表 - How to cut and paste information cell by cell from one worksheet to another in Excel using VBA Excel VBA-通过另一个单元格更改单元格值? - Excel VBA - Change cell value via another cell? Excel VBA:工作表将单元格值更改为其他工作表 - Excel VBA: Worksheet Change cell value to different sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM