ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: 小星光

再次求助大师

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-5-27 15:07 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
LIUZHU 发表于 2024-5-27 14:32
这个“资料源”表是从财务系统导出的数据吗?盲目的插行插列,我也是服了

习惯就好。。。

TA的精华主题

TA的得分主题

发表于 2024-5-27 15:37 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
不知道结果对不对?
录制_2024_05_27_15_37_17_309.gif

TA的精华主题

TA的得分主题

发表于 2024-5-27 15:38 | 显示全部楼层
JSA代码,参考下
  1. function 获取上季度数据(){
  2.         const [year,month]=["i1","i2"].map(x=>Sheets.Item("指定时间段").Range(x).Value2);
  3.         const sm=(Math.ceil(month/3)-1)*3, em=sm+2;
  4.         const sdate=new Date(year,sm-3,1,8), edate=new Date(year,em-2,0,8);
  5.         const [sty,stm]=[sdate.getFullYear(), sdate.getMonth() + 1];
  6.         const [ety,etm]=[edate.getFullYear(), edate.getMonth() + 1];
  7.         const arr=Sheets.Item("资料源").Range("a1").CurrentRegion.Value2;
  8.         let res=arr.slice(1).reduce((res,x)=>{
  9.                 if (x[0]>=sty && x[0]<=ety && x[1]>=stm && x[1]<=etm){
  10.                         res.push([x[5],x[3],x[4]]);
  11.                 }
  12.                 return res;
  13.         },[["代码","借方金额","贷方金额"]]);
  14.         Sheets.Item("季度表").Range("u5").Resize(res.length,res[0].length).Value2=res;
  15. }
复制代码

TA的精华主题

TA的得分主题

发表于 2024-5-27 15:39 | 显示全部楼层
附件,WPS打开测试

获取上季度数据.zip

34.83 KB, 下载次数: 2

TA的精华主题

TA的得分主题

发表于 2024-5-27 16:28 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
练习一下JS
  1. function test(){
  2.         let [year,month]=["i1","i2"].map(x=>Number(Sheets.Item("指定时间段").Range(x).Value2));  //年、月
  3.         const dic_jd={1:[1,3],2:[4,6],3:[7,9],4:[10,12]}   //季度和月份的关系
  4.         if(month<=3) year-=1;   //1-3月,上季度为去年
  5.                 jd=month<=3?4:month<=6?1:month<=9?2:3;   //根据月份得出上季度的季度值
  6.                 let s=dic_jd[jd][0],e=dic_jd[jd][1];    //起始、结束月份     
  7.         const arr=Sheets.Item("资料源").Range("a1:k"+Sheets.Item("资料源").Cells(Rows.Count,1).End(3).Row).Value2;
  8.         let res=arr.slice(1).filter(x=>x[0]==year && x[1]>=s && x[1]<=e).map(y=>[y[5],y[3],y[4]])
  9.         res.unshift(["代码","借方金额","贷方金额"]);
  10.         Sheets.Item("季度表").Range("Y5").Resize(res.length,res[0].length).Value2=res;
  11. }
复制代码

TA的精华主题

TA的得分主题

发表于 2024-5-27 16:29 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2024-5-27 16:40 | 显示全部楼层
小改一下,不用字典。
  1. function test(){
  2.         let [year,month]=["i1","i2"].map(x=>Number(Sheets.Item("指定时间段").Range(x).Value2));  //年、月
  3.         const dic_jd=[[10,12],[1,3],[4,6],[7,9]]   //每季度的起始月份(4季度按0季度计)
  4.         if(month<=3) year-=1;   //1-3月,上季度为去年
  5.                 jd=Math.floor((month-1)/3);   //根据月份得出上季度的季度值
  6.                 let s=dic_jd[jd][0],e=dic_jd[jd][1];    //起始、结束月份     
  7.         const arr=Sheets.Item("资料源").Range("a1:k"+Sheets.Item("资料源").Cells(Rows.Count,1).End(3).Row).Value2;
  8.         let res=arr.slice(1).filter(x=>x[0]==year && x[1]>=s && x[1]<=e).map(y=>[y[5],y[3],y[4]])
  9.         res.unshift(["代码","借方金额","贷方金额"]);
  10.         Sheets.Item("季度表").Range("Y5").Resize(res.length,res[0].length).Value2=res;
  11. }
复制代码

TA的精华主题

TA的得分主题

发表于 2024-5-27 16:51 | 显示全部楼层

学习15楼的代码,有所获,改写了下,同样达到效果
  1. function test1(){
  2.         const obj={0:[10,12], 1:[1,3], 2:[4,6], 3:[7,9], 4:[10,12]}   //季度和月份的关系
  3.     let [year,month]=["i1","i2"].map(x=>Sheets.Item("指定时间段").Range(x).Value2);  //年、月
  4.     let ye=year-(month<=3), [st,ed]=obj[Math.ceil(month/3)-1];
  5.     const arr=Sheets.Item("资料源").Range("a1").CurrentRegion.Value2;
  6.     let res=arr.reduce((res,[a,b,,c,d,e])=>(a==ye && b>=st && b<=ed?res.push([e,c,d]):null,res),[["代码","借方金额","贷方金额"]]);
  7.     Sheets.Item("季度表").Range("Y5").Resize(res.length,res[0].length).Value2=res;
  8. }
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-6-26 21:35 , Processed in 0.031995 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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