我正在尝试复制工作表,然后在单独的摘要工作表中创建公式,该公式链接到此新创建的副本中的单元格。 我并没有取得多少成功,这可能与我如何识别和命名新工作表或如何构造公式(或两者)有关。
Sub CopyCosting()
'
Dim currentNPD As String
Dim currentCOST As String
Dim currentCALC As String
Dim NewNPD As String
Dim NewCOST As String
Dim NewCALC As String
ActiveSheet.Select
currentNPD = ActiveSheet.Name
ActiveSheet.Next.Select
currentCOST = ActiveSheet.Name
ActiveSheet.Next.Select
currentCALC = ActiveSheet.Name
Sheets(Array(currentNPD, currentCOST, currentCALC)).Copy After:= _
Sheets("SUMMARY")
Worksheets("SUMMARY").Select
ActiveSheet.Next.Select
NewNPD = ActiveSheet.Name
ActiveSheet.Next.Select
NewCOST = ActiveSheet.Name
ActiveSheet.Next.Select
NewCALC = ActiveSheet.Name
Worksheets("SUMMARY").Select
NextFree = Range("B9:B" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree).EntireRow.Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B" & NextFree).Select
ActiveCell.FormulaR1C1 = _
"=NewCOST!R[-39]C[-1]&"" (""&NewCOST!R[-35]C[3]&""x""&NewCOST!R[-37]C[3]&""g)"""
ActiveCell.Offset(0, 1).Activate
ActiveCell.FormulaR1C1 = "=IF(NewCOST!R[-40]C[5]>49,""AYR"",""Seasonal"")"
End Sub
excel公式#REFS
并相对于第一个串联公式,我要求宏创建它,如下所示:
=[NewCOST]NewCOST!A2&" ("&[NewCOST]NewCOST!E6&"x"&[NewCOST]NewCOST!E4&"g)"
我希望公式在R[-39]C[-1] = Cheese Cubes
和R[-35]C[3] = 4 and R[-37]C[3] = 200
时以以下格式返回文本
Cheese Cubes (4x200g)
我希望这一切都说得通!任何帮助和建议,将不胜感激。
答案 0 :(得分:0)
首先,最好使用工作簿和工作表功能而不是选择:
Dim wb as Workbook, ws as worksheet, nws as worksheet
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("wsname")
set nws = wb.Worksheets("newws")'When you create new worksheet
然后,在定义范围时不再需要使用select。使用:
ws.Range("...")
我还将尝试使用下面的for循环插入每个单元格公式,我还将在excel中写出公式并在vba中复制/粘贴。用“ r”更改数字:
For each r in Range("B" & NextFree)
'Formula here
Next r