Excel 如何使用索引而不是工作表名称引用此工作表?
请参阅下面的代码片段。我基本上只是在寻找如何在不使用硬编码名称的情况下选择上个月相同的工作表Excel 如何使用索引而不是工作表名称引用此工作表?,excel,vba,excel-formula,Excel,Vba,Excel Formula,请参阅下面的代码片段。我基本上只是在寻找如何在不使用硬编码名称的情况下选择上个月相同的工作表 ActiveCell.FormulaR1C1 = _ "='[Previous Month''s Public Numbers.xls]Previous Month'!R4C4+RC[-4]" 我试过一次,但似乎对我不起作用: ActiveCell.FormulaR1C1 = _ "='[Previous Month''s Public Numbers.x
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R4C4+RC[-4]"
我试过一次,但似乎对我不起作用:
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]'(1)!R4C4+RC[-4]"
错误:
有什么想法吗?我只是不想手动输入上个月的工作表名称,如果我可以帮助的话,我宁愿用索引引用它
根据请求,我添加了更多的代码和错误。我尽量不添加完整的代码,因为完整的代码非常长,可以做很多事情,我不想转移我问这个问题的主要原因
Sheets("Month Raw Data").Select
Range("H4").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R4C8+RC[-4]"
Range("H4").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("H5").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R5C8+RC[-4]"
Range("H5").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("J4").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=(((RC[-2]-RC[-1])/RC[-1])*100)"
Range("J5").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=(((RC[-2]-RC[-1])/RC[-1])*100)"
Range("H8").Select
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R8C8+RC[-4]"
Range("H9").Select
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R9C8+RC[-4]"
Range("H10").Select
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R10C8+RC[-4]"
Range("H11").Select
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R11C8+RC[-4]"
Range("H8").Select
Selection.Copy
Range("H4:H5").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("H14").Select
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R14C8+RC[-4]"
Range("H15").Select
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R15C8+RC[-4]"
Range("H17").Select
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R17C8+RC[-4]"
Sheets("Month % Change").Select
Range("I29").Select
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]Previous Month'!R[-26]C[8]+RC[-5]"
Range("I29").Select
Selection.AutoFill Destination:=Range("I29:I50")
Range("I29:I49").Select
Range("I29:I50").Select
Selection.Copy
您可以这样做:
dim WksName as string
WksName = Workbooks("Previous Month''s Public Numbers").sheets(1).Name '1 is the indice you want
'Now you call the sheet in all your formula this way :
ActiveCell.FormulaR1C1 = _
"='[Previous Month''s Public Numbers.xls]" & WksName & "'!R[-26]C[8]+RC[-5]"
希望我能很好地理解您。使用工作表变量,例如
ws
,并在中连接.Name
?如果您想使用索引,假设上个月始终是最后第二张工作表,您可以通过Worksheets.Count-1
获得索引号,因此上个月的工作表名称是工作簿中唯一的工作表。这就是为什么我认为用索引(1)替换它可能有效的原因。我收到一个对象错误。想和大家分享吗?不过我得到了一个对象错误…在这里多写一点代码会很有帮助