我对VBA并不熟悉所以这个解决方案可能非常简单,我只是不知道如何去做。我有10个工作表(成本比较1到10)。我需要用公式更新它们并重新格式化它们。目前我有以下代码:
Sub Comparison1()
' Comparison1 Macro to change formulas to compare scenario # to as-is
Sheets("Cost Comparison-1").Select
Range("H8").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(IF(('Costs As-Is'!RC-'Scenario-1'!RC)<>0,'Costs As-Is'!RC-'Scenario-1'!RC,""""),"""")"
Range("H8").Select
Selection.AutoFill Destination:=Range("H8:R8"), Type:=xlFillDefault
Range("H8:R8").Select
Selection.AutoFill Destination:=Range("H8:R1006"), Type:=xlFillDefault
Range("H8:R1006").Select
Sheets("Costs As-Is").Select
Cells.Select
Selection.Copy
Sheets("Cost Comparison-1").Select
Cells.Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveSheet.Outline.ShowLevels RowLevels:=1
End Sub
上面的代码仅适用于单张纸。我为每张表格为此宏制作了10个单独的实例。然后做了一个宏来打电话&#34;这些宏中的每一个都是分开的。我想要的是拥有一个结合了所有这一切的宏。并且,如果可能的话,如果工作表不存在,则不会像我当前那样给出错误消息(&#34;下标超出范围)。
提前致谢
答案 0 :(得分:0)
遍历工作簿中的所有工作表。
Sub WorksheetLoop2()
' Declare Current as a worksheet object variable.
Dim Current As Worksheet
' Loop through all of the worksheets in the active workbook.
For Each Current In Worksheets
' Insert your code here.
' This line displays the worksheet name in a message box.
MsgBox Current.Name
Next
End Sub
此外,您可以轻松更改每张表格中的某种格式。
Sub change_color()
For Each sht In ActiveWorkbook.Sheets
Set rng = sht.UsedRange
For Each cell In rng
If cell.Font.ColorIndex = 3 Then
cell.Font.ColorIndex = 0
End If
Next cell
Next sht
End Sub