简体   繁体   English

VBA“=活动单元格”来自另一个工作表

[英]VBA "=active cells" from another work sheet

I am trying to use VBA to update a cell in a new worksheet to equal the active cells from a schedule on another sheet.我正在尝试使用 VBA 来更新新工作表中的单元格,以等于另一个工作表上的计划中的活动单元格。

I have tried many variants of the below code, but at present, I have only managed to copy the cell values across.我已经尝试了以下代码的许多变体,但目前,我只能设法复制单元格值。 I am seeking to update the cells in the new sheet to equal the original cells as they would if you typed for example " =SheetX!A1 " directly into the cell.我试图将新工作表中的单元格更新为与原始单元格相等,就像您在单元格中直接输入例如“=SheetX!A1”一样。

Worksheets("Template").Visible = True
Sheets("Template").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = "NewName"

ThisWorkbook.Worksheets("Tech_Sub_Reg").Activate


Dim R As Range
Dim S As Range
Dim I As Range
Dim B As Range

Set R = ActiveCell
Set S = ActiveCell.Offset(0, 1)
Set I = ActiveCell.Offset(0, 2)
Set B = ActiveCell.Offset(0, 3)

Sheets("NewName").Range("A125").Formula = R
Sheets("NewName").Range("B125").Formula = S
Sheets("NewName").Range("C125").Formula = I
Sheets("NewName").Range("D125").Formula = B

Change eg改变例如

Sheets("NewName").Range("A125").Formula = R

to

Sheets("NewName").Range("A125").Formula = "=" & R.Address(External:=True)

If you also want to remove the workbook-name from the formula, replace what comes after "=" & in the line above with如果您还想从公式中删除工作簿名称,请将上一行中"=" &之后的内容替换为

Replace(R.Address(External:=True), "[" & R.Parent.Parent.Name & "]", "", 1, -1, vbTextCompare)

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

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