ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 247|回复: 4

[求助] 超长公式如何用VBA写入表格

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-8-2 08:31 | 显示全部楼层 |阅读模式
各位老师好

我有个公式想插入表格的H2单元格,但是录制宏的时候发现公式太长,代码框写不下,我用&_连字符换行也不行,麻烦老师指导下如何分段写。公式如下:
公式如下:=INT((SUMIFS(生产领料序时簿!K:K,生产领料序时簿!M:M,IF(MONTH(TODAY())-1=0,YEAR(TODAY())-1&"-12",YEAR(TODAY())&"-"&MONTH(TODAY())-1),生产领料序时簿!H:H,B2)+SUMIFS(生产领料序时簿!K:K,生产领料序时簿!M:M,IF(MONTH(TODAY())-2=0,YEAR(TODAY())-1&"-12",IF(MONTH(TODAY())-2=-1,YEAR(TODAY())-1&"-11",YEAR(TODAY())&"-"&MONTH(TODAY())-2)),生产领料序时簿!H:H,B2)+SUMIFS(生产领料序时簿!K:K,生产领料序时簿!M:M,IF(MONTH(TODAY())-3=0,YEAR(TODAY())-1&"-12",IF(MONTH(TODAY())-3=-1,YEAR(TODAY())-1&"-11",IF(MONTH(TODAY())-3=-2,YEAR(TODAY())-1&"-10",YEAR(TODAY())&"-"&MONTH(TODAY())-3))),生产领料序时簿!H:H,B2)+SUMIFS(生产领料序时簿!K:K,生产领料序时簿!M:M,IF(MONTH(TODAY())-4=0,YEAR(TODAY())-1&"-12",IF(MONTH(TODAY())-4=-1,YEAR(TODAY())-1&"-11",IF(MONTH(TODAY())-4=-2,YEAR(TODAY())-1&"-10",IF(MONTH(TODAY())-4=-3,YEAR(TODAY())-1&"-9",YEAR(TODAY())&"-"&MONTH(TODAY())-4)))),生产领料序时簿!H:H,B2)+SUMIFS(生产领料序时簿!K:K,生产领料序时簿!M:M,IF(MONTH(TODAY())-5=0,YEAR(TODAY())-1&"-12",IF(MONTH(TODAY())-5=-1,YEAR(TODAY())-1&"-11",IF(MONTH(TODAY())-5=-2,YEAR(TODAY())-1&"-10",IF(MONTH(TODAY())-5=-3,YEAR(TODAY())-1&"-9",IF(MONTH(TODAY())-5=-4,YEAR(TODAY())-1&"-8",YEAR(TODAY())&"-"&MONTH(TODAY())-5))))),生产领料序时簿!H:H,B2)+SUMIFS(生产领料序时簿!K:K,生产领料序时簿!M:M,IF(MONTH(TODAY())-6=0,YEAR(TODAY())-1&"-12",IF(MONTH(TODAY())-6=-1,YEAR(TODAY())-1&"-11",IF(MONTH(TODAY())-6=-2,YEAR(TODAY())-1&"-10",IF(MONTH(TODAY())-6=-3,YEAR(TODAY())-1&"-9",IF(MONTH(TODAY())-6=-4,YEAR(TODAY())-1&"-8",IF(MONTH(TODAY())-6=-5,YEAR(TODAY())-1&"-7",YEAR(TODAY())&"-"&MONTH(TODAY())-6)))))),生产领料序时簿!H:H,B2))/6)

TA的精华主题

TA的得分主题

发表于 2024-8-2 08:36 | 显示全部楼层
     Range("c16") = "=SUMIF(A2:A6,""b"",B2:B6)" 遇到单引号就把单引号加倍
但是使用公式容易卡顿

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-8-2 17:55 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
如果没记错的话,公式不能超过255个字符,VBA也得遵守基本法
真要那么长,用名称嵌套吧

TA的精华主题

TA的得分主题

发表于 2024-8-2 20:01 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
用VBA的话,就不要用那么长的公式了,没必要。

TA的精华主题

TA的得分主题

发表于 2024-8-3 15:52 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
没附件不好测试

Sub InsertFormula()
    Dim ws As Worksheet
    Dim formulaPart1 As String
    Dim formulaPart2 As String
    Dim formulaPart3 As String
    Dim formulaPart4 As String
    Dim formulaPart5 As String
    Dim formulaPart6 As String
    Dim finalFormula As String

    Set ws = ThisWorkbook.Worksheets("Sheet1") '请根据实际情况修改工作表名称

    formulaPart1 = "=INT((SUMIFS(生产领料序时簿!K:K,生产领料序时簿!M:M,"
    formulaPart2 = "IF(MONTH(TODAY())-1=0,YEAR(TODAY())-1&""-12"",YEAR(TODAY())&""-""&MONTH(TODAY())-1),生产领料序时簿!H:H,B2)+"
    formulaPart3 = "SUMIFS(生产领料序时簿!K:K,生产领料序时簿!M:M,"
    formulaPart4 = "IF(MONTH(TODAY())-2=0,YEAR(TODAY())-1&""-12"",IF(MONTH(TODAY())-2=-1,YEAR(TODAY())-1&""-11"",YEAR(TODAY())&""-""&MONTH(TODAY())-2)),生产领料序时簿!H:H,B2)+"
    formulaPart5 = "SUMIFS(生产领料序时簿!K:K,生产领料序时簿!M:M,"
    formulaPart6 = "IF(MONTH(TODAY())-3=0,YEAR(TODAY())-1&""-12"",IF(MONTH(TODAY())-3=-1,YEAR(TODAY())-1&""-11"",IF(MONTH(TODAY())-3=-2,YEAR(TODAY())-1&""-10"",YEAR(TODAY())&""-""&MONTH(TODAY())-3))),生产领料序时簿!H:H,B2)+"
    finalFormula = formulaPart1 & formulaPart2 & formulaPart3 & formulaPart4 & formulaPart5 & formulaPart6 & "))/6)"

    ws.Range("H2").Formula = finalFormula
End Sub

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-18 08:21 , Processed in 0.035174 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表