[英]Macro to change the formula in a column isn't working (SOLVED)
我有一個帶有三個選項卡的工作簿,用於項目預算模板。 在第一個選項卡(第一個選項卡稱為SOW)中有一個下拉菜單,該菜單根據從下拉菜單中選擇的內容來調用不同的宏。 所述宏之一和我需要修復的宏會根據從下拉框中選擇的模板類型來更改第二個選項卡(此選項卡稱為“服務”)的G列中的單位費率公式。
這是公司A所需的公式: =SUMIF(K368:411,C29, L368:L411)
這是我對公司B所需的公式: =IF(C429="Managing Director",295,IF(C429="Director of Production",239.5,IF(C429="Deputy Director of Production",225,IF(C429="Executive Producer",215,IF(C429="Senior Producer",185,IF(C429="Producer",149.5,IF(C429="Associate Producer",129.5,IF(C429="Senior Project Coordinator",119.5,IF(C429="Project Coordinator",95,IF(C429="Production Assistant",75,IF(C429="Head of Technical Services",239.5,IF(C429="Technical Director",195,IF(C429="Senior Production Manager",185,IF(C429="Production Manager",149.5,IF(C429="Executive Creative Director",335,IF(C429="Group Creative Director",315,IF(C429="Creative Director",285,IF(C429="Associate Creative Director",185,IF(C429="Design Director",185,IF(C429="Senior 3D Director",179.5,IF(C429="3D Designer",115,IF(C429="Senior 2D Designer",179.5,IF(C429="2D Designer / Production Designer",115,IF(C429="Animator",179.5,IF(C429="Strategy Director",315,IF(C429="Senior Strategist",185,IF(C429="Strategist",165,IF(C429="Group Account Director",265,IF(C429="Account Director",235,IF(C429="Senior Account Manager",195,IF(C429="Account Manager",145,IF(C429="Account Executive",115,IF(C429="Content Designer / Presentation Graphics",175,IF(C429="Lighting Designer",195,IF(C429="Sound Designer",195,IF(C429="Video Designer",195,IF(C429="Showcaller",195,IF(C429="Assistant Stage Manager",175,IF(C429="Makeup Artist",175,IF(C429="Prompt Operator",175,0))))))))))))))))))))))))))))))))))))))))
=IF(C429="Managing Director",295,IF(C429="Director of Production",239.5,IF(C429="Deputy Director of Production",225,IF(C429="Executive Producer",215,IF(C429="Senior Producer",185,IF(C429="Producer",149.5,IF(C429="Associate Producer",129.5,IF(C429="Senior Project Coordinator",119.5,IF(C429="Project Coordinator",95,IF(C429="Production Assistant",75,IF(C429="Head of Technical Services",239.5,IF(C429="Technical Director",195,IF(C429="Senior Production Manager",185,IF(C429="Production Manager",149.5,IF(C429="Executive Creative Director",335,IF(C429="Group Creative Director",315,IF(C429="Creative Director",285,IF(C429="Associate Creative Director",185,IF(C429="Design Director",185,IF(C429="Senior 3D Director",179.5,IF(C429="3D Designer",115,IF(C429="Senior 2D Designer",179.5,IF(C429="2D Designer / Production Designer",115,IF(C429="Animator",179.5,IF(C429="Strategy Director",315,IF(C429="Senior Strategist",185,IF(C429="Strategist",165,IF(C429="Group Account Director",265,IF(C429="Account Director",235,IF(C429="Senior Account Manager",195,IF(C429="Account Manager",145,IF(C429="Account Executive",115,IF(C429="Content Designer / Presentation Graphics",175,IF(C429="Lighting Designer",195,IF(C429="Sound Designer",195,IF(C429="Video Designer",195,IF(C429="Showcaller",195,IF(C429="Assistant Stage Manager",175,IF(C429="Makeup Artist",175,IF(C429="Prompt Operator",175,0))))))))))))))))))))))))))))))))))))))))
當我測試這些宏以分別來回更改單位費率時,它們似乎可以正常工作。 但是,當它們包含在下拉宏中時,由於某種原因,它們會使事情變得混亂。 在“服務”選項卡的G列的每一行(第二個選項卡的G列)中始終輸入一種角色。 該宏不應在G列中輸入任何值,而應僅更改此列中用於計算單位費率的公式。 對於SUMIF公式,第二個選項卡底部的表格隱藏了角色和費率。 對於嵌套的IF公式,我還將其隱藏在第二個選項卡底部的一個單元格中(宏復制了該單元格,然后在第二個選項卡的G列中選擇了所有必要的行,並粘貼了嵌套的if公式。因為G列似乎也發生了變化,因此在刪除宏錯誤輸入的角色后,我必須為該列中的每個單元格重新輸入所有邊框。此外,如果情況有所不同,則大約有16個合並的單元格行在所說的G列中,這就是為什么在G列中選擇了多個范圍的原因。我能夠通過記錄一個宏來獲取下面的代碼,所以我對到底出了什么問題感到困惑。也許我只需要在宏中添加更多內容即可更改了單位費率,但想在此處檢查以防萬一我遺漏了一些東西。
```UnitRatesForRolesInServicesTab()
Sheets("SERVICES").Select
Range("G29").Select
ActiveCell.FormulaR1C1 = _
"=SUMIF(R[339]C[4]:R[382]C[4], RC[-4],R[339]C[5]:R[382]C[5])"
Range("G29").Select
Selection.Copy
Range( _
"G30:G48,G50:G69,G71:G90,G92:G111,G113:G132,G134:G153,G155:G174,G176:G195,G197:G216,G218:G237,G239:G258,G260:G279,G281:G300,G302:G321,G323:G342,G344:G363" _
).Select
Range("G344").Activate
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
Sheets("SOW").Select
End Sub```
以下是撤消上述操作的宏:
```Sub UndoUnitRatesForRolesInServicesTab()
'
' UndoUnitRatesForRolesInServicesTab Macro
'
'
Sheets("SERVICES").Select
Range("G423").Select
Selection.Copy
Range("G29").Select
ActiveSheet.Paste
Range( _
"G30:G48,G50:G69,G71:G90,G92:G111,G113:G132,G134:G153,G155:G174,G176:G195,G197:G216,G218:G237,G239:G258,G260:G279,G281:G300,G302:G321,G323:G342,G344:G363" _
).Select
Range("G344").Activate
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.NumberFormat = "0;-0;-;@"
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndSha```
這不是您問題的直接答案,但我希望它可以簡化您的宏和數據(然后也許也可以簡化您的問題)。
憑借如此冗長且深層嵌套的IF
語句,我建議您更改確定人物角色的比率的方式。 使用VLOOKUP
公式是實現所需目標的快速方法。
因此,創建另一個工作表。 我的示例調用是SupportingInfo
。 您可以將其設置為隱藏的工作表,也可以不設置為隱藏的工作表。 輸入您的角色和費率,然后將數據轉換為表並將其命名為RolesAndRates
:
現在,在您的主要工作表上,您列出了所有角色並需要費率,這是一個簡單的查找語句:
還要注意,您可以添加或刪除角色或更改任何費率,並且由於您的VLOOKUP
公式,它將自動反映出來。
雖然這不是您問題的直接答案,但我認為這將使您如何輕松構建工作表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.