excel - 奇数/偶数行的交替公式
问题描述
我试图在偶数行上写一个公式,在同一列的奇数行上写一个不同的公式。搜索这个网站我发现了以下代码:
Sub OddRowAlert()
With Range("B16:B100")
.Formula = "=IF((MOD(ROW(B16),2)),""Odd"",""Even"")"
.Formula = .Value
End With
End Sub
这适用于文本或简单的公式。但是,当我用我的公式替换“奇数”或“偶数”时,我会收到一个
运行时错误“1004”:应用程序定义或对象定义错误。
这是我正在尝试的代码:
Sub Daily_Analysis()
'
' Daily_Analysis Macro
'
Lastrow = Range("B" & Rows.Count).End(xlUp).Row
Range("L7:L" & Lastrow).Formula = "=Text(A7,""dddd"")"
With Range("M7:M" & Lastrow)
.Formula = "=IF((MOD(ROW(M7),2)),""=IFS(L7=""Saturday"","""",L7=""Sunday"","""",L7<>L6,""Number of times over 75%"",L7=L6,"""")"",""=IFS(L7=""Saturday"","""",L7=""Sunday"","""",L6<>L5,COUNTIFS(D7:D103,"">75%"",K7:K103,""Working"")+COUNTIFS(G7:G103,"">75%"",K7:K103,""Working""),L6=L5,"""")"")"
.Formula = .Value
End With
With Range("N7:N" & Lastrow)
.Formula = "=IF((MOD(ROW(N7),2)),""=IFS(L7=""Saturday"","""",L7=""Sunday"","""",L7<>L6,""Percentage of the day"",L7=L6,"""")"",""=IFS(L7=""Saturday"","""",L7=""Sunday"","""",L7<>L6,M8/COUNTIFS(K7:K102,""working"",L7:L102,L7),L7=L6,"""")"")"
.Formula = .Value
End With
With Range("O7:O" & Lastrow)
.Formula = "=IF((MOD(ROW(O7),2)),""=IFS(L7=""Saturday"","""",L7=""Sunday"","""",L7<>L6,""hours of poor performance"",L7=L6,"""")"",""=IFS(L7=""Saturday"","""",L7=""Sunday"","""",L7<>L6,(15*M8)/60,L7=L6,"""")"")"
.Formula = .Value
End With
'
End Sub
解决方案
如评论中所述,嵌套公式不会""
围绕它们并且不=
,只是嵌套它们:
Sub Daily_Analysis()
'
' Daily_Analysis Macro
'
Lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
ActiveSheet.Range("L7:L" & Lastrow).Formula = "=Text(A7,""dddd"")"
With ActiveSheet.Range("M7:M" & Lastrow)
.Formula = "=IF((MOD(ROW(M7),2)),IFS(L7=""Saturday"","""",L7=""Sunday"","""",L7<>L6,""Number of times over 75%"",L7=L6,""""),IFS(L7=""Saturday"","""",L7=""Sunday"","""",L6<>L5,COUNTIFS(D7:D103,"">75%"",K7:K103,""Working"")+COUNTIFS(G7:G103,"">75%"",K7:K103,""Working""),L6=L5,""""))"
.Value = .Value
End With
With ActiveSheet.Range("N7:N" & Lastrow)
.Formula = "=IF((MOD(ROW(N7),2)),IFS(L7=""Saturday"","""",L7=""Sunday"","""",L7<>L6,""Percentage of the day"",L7=L6,""""),IFS(L7=""Saturday"","""",L7=""Sunday"","""",L7<>L6,M8/COUNTIFS(K7:K102,""working"",L7:L102,L7),L7=L6,""""))"
.Value = .Value
End With
With ActiveSheet.Range("O7:O" & Lastrow)
.Formula = "=IF((MOD(ROW(O7),2)),IFS(L7=""Saturday"","""",L7=""Sunday"","""",L7<>L6,""hours of poor performance"",L7=L6,""""),IFS(L7=""Saturday"","""",L7=""Sunday"","""",L7<>L6,(15*M8)/60,L7=L6,""""))"
.Value = .Value
End With
'
End Sub
顺便说一句,取出类似的项目并重新排列我们可以缩短公式:
Sub Daily_Analysis()
'
' Daily_Analysis Macro
'
Lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
ActiveSheet.Range("L7:L" & Lastrow).Formula = "=Text(A7,""dddd"")"
With ActiveSheet.Range("M7:M" & Lastrow)
.Formula = "=IF(OR(WEEKDAY(A7,2)>5,l7=l6),"""",IF(Mod(row(),2),""Number of times over 75%"",COUNTIFS(D7:D103,"">75%"",K7:K103,""Working"")+COUNTIFS(G7:G103,"">75%"",K7:K103,""Working"")))"
.Value = .Value
End With
With ActiveSheet.Range("N7:N" & Lastrow)
.Formula = "=IF(OR(WEEKDAY(A7,2)>5,l7=l6),"""",IF(Mod(row(),2),""Percentage of the day"",M8/COUNTIFS(K7:K102,""working"",L7:L102,L7)))"
.Value = .Value
End With
With ActiveSheet.Range("O7:O" & Lastrow)
.Formula = "=IF(OR(WEEKDAY(A7,2)>5,l7=l6),"""",IF(Mod(row(),2),""hours of poor performance"",L7<>L6,(15*M8)/60))"
.Value = .Value
End With
'
End Sub
推荐阅读
- jenkins - Groovy Jenkins:评估 BUILD_LOG_REGEX 并在发送电子邮件之前检查是否为空
- codenameone - 代号一:向 InfiniteProgress() 添加文本
- visual-studio-code - Flutter Doctor 没有在我的 PC 上检测到 Visual Studio Code
- google-tag-manager - 为什么我的 GTM 点击 ID 触发器会在所有点击时触发?
- c - 访问二维数组中的元素时出错
- javascript - CKEditor 插件在删除后缓存在磁盘中。如何强制浏览器重新扫描或删除该缓存?
- webpack - 使用 webpack style-loader 捆绑的包不兼容 SSR,因此无法导入 Next.js
- vue.js - 类型 vuejs pwa data() 函数上不存在属性
- c# - 美元 ($) 前缀字段对存储无效
- date - Hippo CMS 文件夹的创建日期