2013-04-08 51 views
1

我希望我的公式能夠在我的列的左邊一個句點和變量Name中引用表格名稱。例如,如果左邊的列是10,工作表名稱是12.我的值將是10.12ActiveCell.FormulaR1C1中的變量

我試過以下,但我不能讓名稱顯示爲變量而不是「名稱「

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 

感謝您的任何幫助。

回答

2

這似乎讓你更接近:

Dim Name As String 
Dim Output As String 

Name = ActiveSheet.Name 


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


ActiveCell.FormulaR1C1 = Output 

這裏是你的代碼,而所有的選擇:

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