简体   繁体   English

宏无法更改列中的公式(已解决)

[英]Macro to change the formula in a column isn't working (SOLVED)

I have a workbook with three tabs for project budget template. 我有一个带有三个选项卡的工作簿,用于项目预算模板。 There is a drop down menu in the first tab (first tab is called SOW) which calls different macros depending on what is selected from the drop down menu. 在第一个选项卡(第一个选项卡称为SOW)中有一个下拉菜单,该菜单根据从下拉菜单中选择的内容来调用不同的宏。 One of said macros, and the macro which I need to fix, changes the formula for the unit rate in Column G of the second tab (this tab is called Services) depending on which type of template is selected from the drop down box. 所述宏之一和我需要修复的宏会根据从下拉框中选择的模板类型来更改第二个选项卡(此选项卡称为“服务”)的G列中的单位费率公式。

This is the formula I need to Company A: =SUMIF(K368:411,C29, L368:L411) 这是公司A所需的公式: =SUMIF(K368:411,C29, L368:L411)

This is the formula I need for Company 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)))))))))))))))))))))))))))))))))))))))) 这是我对公司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))))))))))))))))))))))))))))))))))))))))

When I test out these macros to change the unit rate back and forth individually, they seem to work without a problem. 当我测试这些宏以分别来回更改单位费率时,它们似乎可以正常工作。 However, when they are included in the drop down macro, they mess things up for some reason. 但是,当它们包含在下拉宏中时,由于某种原因,它们会使事情变得混乱。 There is always one type of role entered into each row of Column G in the Services tab (column G of the second tab). 在“服务”选项卡的G列的每一行(第二个选项卡的G列)中始终输入一种角色。 This macro shouldn't enter any values into column G, it should only change the formula in this column which calculates the unit rate. 该宏不应在G列中输入任何值,而应仅更改此列中用于计算单位费率的公式。 For the SUMIF formula, there is a table with the roles and rates hidden at the bottom of the second tab. 对于SUMIF公式,第二个选项卡底部的表格隐藏了角色和费率。 For the nested IF formula, I also hid this in one cell at the bottom of the second tab (the macro copies this cell, then selects all necessary rows in Column G in the second tab, and pastes in the nested if formula. The format for column G also seems to change. I have to re-enter all borders for each cell in this column after deleting the role which was entered incorrectly by the macro. Also, in case it makes a different, there are about 16 merged cell rows within said column G which is why there are multiple ranges selected in column G. I was able to get the code below by recording a macro so I'm confused as to what is going wrong. Perhaps I just need to add more to the macros which change the unit rate but wanted to check here in case I am missing something. 对于嵌套的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```

The following is the macro to undo the above: 以下是撤消上述操作的宏:

```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```

This isn't a direct answer to your question, but I hope it simplifies your macros and data (and then maybe simplifies your problem too). 这不是您问题的直接答案,但我希望它可以简化您的宏和数据(然后也许也可以简化您的问题)。

With such a wickedly long and deeply nested IF statement, I'm suggesting a change to how you're determining the rate for a person's role. 凭借如此冗长且深层嵌套的IF语句,我建议您更改确定人物角色的比率的方式。 Using the VLOOKUP formula is a quick way to achieve what you want. 使用VLOOKUP公式是实现所需目标的快速方法。

So create another worksheet. 因此,创建另一个工作表。 My example calls is SupportingInfo . 我的示例调用是SupportingInfo You can make this a hidden worksheet or not. 您可以将其设置为隐藏的工作表,也可以不设置为隐藏的工作表。 Enter your roles and rates, then convert that data into a table and name it RolesAndRates : 输入您的角色和费率,然后将数据转换为表并将其命名为RolesAndRates

在此处输入图片说明

Now on your main worksheet where you're listing all your roles and you need the rates, it's a simple lookup statement: 现在,在您的主要工作表上,您列出了所有角色并需要费率,这是一个简单的查找语句:

在此处输入图片说明

Notice also that you can add or delete roles or change any of the rates and it will automatically be reflected because of your VLOOKUP formula. 还要注意,您可以添加或删除角色或更改任何费率,并且由于您的VLOOKUP公式,它将自动反映出来。

While this isn't a direct answer to your question, I think it will make how you've constructed your worksheet a bit easier. 虽然这不是您问题的直接答案,但我认为这将使您如何轻松构建工作表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM