vba ActiveCell.FormulaR1C1 中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15885730/
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-11 20:29:03  来源:igfitidea点击:

Variable in ActiveCell.FormulaR1C1

variablesexcel-vbaexcel-formulavbaexcel

提问by Burton Guster

I want my formula to yield the value in my column to the left a period and the variable Name, which refers to the sheet name. For example, if the column to the left was 10 and the worksheet name was 12. My value would be 10.12

我希望我的公式在我左边的列中产生一个句点和变量 Name,它指的是工作表名称。例如,如果左边的列是 10,工作表名称是 12。我的值是 10.12

I've tried the following, but I can't get Name to show up as the variable rather than "Name"

我尝试了以下操作,但无法让 Name 显示为变量而不是“Name”

Dim ws As Worksheet
Dim Name As Integer

For Each ws In ActiveWorkbook.Worksheets
    ws.Activate
    Name = ws.Name

    Range("C2").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]&""."" & Name "
Next ws

Thanks for any help.

谢谢你的帮助。

回答by sous2817

This seems to get you closer:

这似乎让你更接近:

Dim Name As String
Dim Output As String

Name = ActiveSheet.Name


Output = ActiveCell.Offset(0, -1).Value & "." & Name


ActiveCell.FormulaR1C1 = Output

Here is your code without all the selecting:

这是没有所有选择的代码:

Sub foo()
Dim ws As Worksheet
Dim Name As String
Dim Output As String

For Each ws In ActiveWorkbook.Worksheets
    ws.Activate
    Name = ws.Name
    Output = ws.Range("C2").Offset(0, -1).Value & "." & Name
    ws.Range("C2").FormulaR1C1 = Output
Next ws
End Sub