ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

EH搜索     
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
Excel不给力? 何不试试FoxTable! Excel 2016函数公式学习大典 Office知识技巧免费学 打造核心竞争力的职场宝典
300集Office 2010微视频教程 Tableau-数据可视化工具 精品推荐-800套精选PPT模板,点击获取 ExcelHome出品 - VBA代码宝免费下载
你的Excel 2010实战技巧学习锦囊 欲罢不能, 过目难忘的 Office 新界面 Excel VBA经典代码实践指南
查看: 468|回复: 14

[求助] VBA自定义函数判断日期

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-1-15 18:42 | 显示全部楼层 |阅读模式
本帖最后由 黑狼-诺 于 2020-1-16 19:29 编辑

Public Function LL(dt As Range)   
If dt > #10/24/2015# Then LL = 4.75
Else: LL = 5.5
End Function
我想把dt作为单元格内的时间,然后判断dt在哪个时间范围内,就用那个时间段的贷款利率来计算贷款,但是我这个一直提示函数错误,怎么回事呢?求大神指教
能不能更高级一点,直接实现VBA函数能够计算,在打款日期和截止日期之间总共产生多少利息?

我在最新上传的附件里边用公式解决了问题,但是公式太复杂了,而且太繁复,将来想验证和检查都会困难。所以我觉得VB和数组函数应该能更简单的解决问题,而且方便检查。

VB的循环计算语句应该是最优的选择,因为本金是定值,时间段和不同的时间段产生的利率是变量,每次都是在循环和迭代计算,每次循环的时候能够带入计算不同的时间段和利率,这个循环会很复杂吗?
我不会这个,有大神能够用吗?谢谢

以下是我在表格里编的计算公式:
’=IF(D3>DATE(2015,10,24), DATEDIF(D3,E3,"Y")*C3*K3+DATEDIF(D3,E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2015,8,26),DATEDIF(D3,"2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2015,6,28),DATEDIF(D3,"2015/8/26","D")*(C3*M3/J3)+
DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2015,5,11),DATEDIF(D3,"2015/6/28","D")*(C3*N3/J3)+
DATEDIF("2015/6/28","2015/8/26","D")*(C3*M3/J3)+DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2015,3,11),DATEDIF(D3,"2015/5/11","D")*(C3*O3/J3)+
DATEDIF("2015/5/11","2015/6/28","D")*(C3*N3/J3)+DATEDIF("2015/6/28","2015/8/26","D")*(C3*M3/J3)+DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2014,11,22),DATEDIF(D3,"2015/3/11","D")*(C3*P3/J3)+
DATEDIF("2015/3/11","2015/5/11","D")*(C3*O3/J3)+DATEDIF("2015/5/11","2015/6/28","D")*(C3*N3/J3)+DATEDIF("2015/6/28","2015/8/26","D")*(C3*M3/J3)+DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2014,9,11),DATEDIF(D3,"2014/11/22","D")*(C3*Q3/J3)+
DATEDIF("2014/11/22","2015/3/11","D")*(C3*P3/J3)+DATEDIF("2015/3/11","2015/5/11","D")*(C3*O3/J3)+DATEDIF("2015/5/11","2015/6/28","D")*(C3*N3/J3)+DATEDIF("2015/6/28","2015/8/26","D")*(C3*M3/J3)+DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2012,7,6),DATEDIF(D3,"2014/9/11","YD")*C3*R3+DATEDIF(D3,"2014/9/11","YD")*(C3*R3/J3)+
DATEDIF("2014/9/11","2015/11/22","D")*(C3*Q3/J3)+DATEDIF("2014/11/22","2015/3/11","D")*(C3*P3/J3)+DATEDIF("2015/3/11","2015/5/11","D")*(C3*O3/J3)+DATEDIF("2015/5/11","2015/6/28","D")*(C3*N3/J3)+DATEDIF("2015/6/28","2015/8/26","D")*(C3*M3/J3)+DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2012,6,8),DATEDIF(D3,"2012/7/6","D")*(C3*S3/J3)+
DATEDIF("2012/7/6","2014/9/11","Y")*C3*R3+DATEDIF("2012/7/6","2014/9/11","YD")*(C3*R3/J3)+DATEDIF("2014/9/11","2015/11/22","D")*(C3*Q3/J3)+DATEDIF("2014/11/22","2015/3/11","D")*(C3*P3/J3)+DATEDIF("2015/3/11","2015/5/11","D")*(C3*O3/J3)+DATEDIF("2015/5/11","2015/6/28","D")*(C3*N3/J3)+DATEDIF("2015/6/28","2015/8/26","D")*(C3*M3/J3)+DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2011,7,7),DATEDIF(D3,"2012/6/8","D")*(C3*T3/J3)+
DATEDIF("2012/6/8","2012/7/6","YD")*(C3*S3/J3)+DATEDIF("2012/7/6","2014/9/11","Y")*C3*R3+DATEDIF("2012/7/6","2014/9/11","YD")*(C3*R3/J3)+DATEDIF("2014/9/11","2015/11/22","D")*(C3*Q3/J3)+DATEDIF("2014/11/22","2015/3/11","D")*(C3*P3/J3)+DATEDIF("2015/3/11","2015/5/11","D")*(C3*O3/J3)+DATEDIF("2015/5/11","2015/6/28","D")*(C3*N3/J3)+DATEDIF("2015/6/28","2015/8/26","D")*(C3*M3/J3)+DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2011,4,6),DATEDIF(D3,"2011/7/7","D")*(C3*U3/J3)+
DATEDIF("2011/7/7","2012/6/8","D")*(C3*T3/J3)+DATEDIF("2012/6/8","2012/7/6","D")*(C3*S3/J3)+DATEDIF("2012/7/6","2014/9/11","Y")*C3*R3+DATEDIF("2012/7/6","2014/9/11","YD")*(C3*R3/J3)+DATEDIF("2014/9/11","2015/11/22","D")*(C3*Q3/J3)+DATEDIF("2014/11/22","2015/3/11","D")*(C3*P3/J3)+DATEDIF("2015/3/11","2015/5/11","D")*(C3*O3/J3)+DATEDIF("2015/5/11","2015/6/28","D")*(C3*N3/J3)+DATEDIF("2015/6/28","2015/8/26","D")*(C3*M3/J3)+DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2011,2,9),DATEDIF(D3,"2011/4/6","D")*(C3*V3/J3)+
DATEDIF("2011/4/6","2011/7/7","D")*(C3*U3/J3)+DATEDIF("2011/7/7","2012/6/8","D")*(C3*T3/J3)+DATEDIF("2012/6/8","2012/7/6","D")*(C3*S3/J3)+DATEDIF("2012/7/6","2014/9/11","Y")*C3*R3+DATEDIF("2012/7/6","2014/9/11","YD")*(C3*R3/J3)+DATEDIF("2014/9/11","2015/11/22","D")*(C3*Q3/J3)+DATEDIF("2014/11/22","2015/3/11","D")*(C3*P3/J3)+DATEDIF("2015/3/11","2015/5/11","D")*(C3*O3/J3)+DATEDIF("2015/5/11","2015/6/28","D")*(C3*N3/J3)+DATEDIF("2015/6/28","2015/8/26","D")*(C3*M3/J3)+DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),
IF(D3>DATE(2010,12,26),DATEDIF(D3,"2011/2/9","D")*(C3*W3/J3)+
DATEDIF("2011/2/9","2011/4/6","D")*(C3*V3/J3)+DATEDIF("2011/4/6","2011/7/7","D")*(C3*U3/J3)+DATEDIF("2011/7/7","2012/6/8","D")*(C3*T3/J3)+DATEDIF("2012/6/8","2012/7/6","D")*(C3*S3/J3)+DATEDIF("2012/7/6","2014/9/11","Y")*C3*R3+DATEDIF("2012/7/6","2014/9/11","YD")*(C3*R3/J3)+DATEDIF("2014/9/11","2015/11/22","D")*(C3*Q3/J3)+DATEDIF("2014/11/22","2015/3/11","D")*(C3*P3/J3)+DATEDIF("2015/3/11","2015/5/11","D")*(C3*O3/J3)+DATEDIF("2015/5/11","2015/6/28","D")*(C3*N3/J3)+DATEDIF("2015/6/28","2015/8/26","D")*(C3*M3/J3)+DATEDIF("2015/8/26","2015/10/24","D")*(C3*L3/J3)+DATEDIF("2015/10/24",E3,"Y")*C3*K3+DATEDIF("2015/10/24",E3,"YD")*(C3*K3/J3),0)))))))))))))


贷款利率表.jpg
表格.PNG

利息计算.rar

123.66 KB, 下载次数: 16

贷款利率表,.rar

8.7 KB, 下载次数: 6

利息计算(用公式解决了问题)但是用VB或者数组是不是更容易解决??.rar

12.72 KB, 下载次数: 9

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-1-15 19:52 来自手机 | 显示全部楼层
用数组函数是不是能很简单的实现?

TA的精华主题

TA的得分主题

发表于 2020-1-15 19:56 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-1-15 23:03 来自手机 | 显示全部楼层
YZC51 发表于 2020-1-15 19:56
需要自己建一个贷款利率表!

是把利率表做成数组吗?

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-1-16 10:10 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-1-16 19:08 | 显示全部楼层
VB的循环计算语句应该是最优的选择。
因为本金是定值,时间段和不同的时间段产生的利率是变量。
公式里边每次都是在重复的计算不同时间段产生的利息,这是不是就是在循环和迭代计算?
如果用循环语句的话,每次循环的时候能够带入计算不同的时间段和利率,这个循环会很复杂吗?
我不会这个,有大神能够用吗?谢谢
这是我自己琢磨的思路,不知道正确不正确。希望大神指正

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-1-16 19:38 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-1-16 19:40 | 显示全部楼层
YZC51 发表于 2020-1-15 19:56
需要自己建一个贷款利率表!

大神有时间能再看看吗??我自己编了个公式计算,虽然也能实现想要的结果。但是不方便后期的检查和纠误

TA的精华主题

TA的得分主题

发表于 2020-1-17 10:56 | 显示全部楼层
简直就是高手中的高手,佩服,密集恐惧症发了

TA的精华主题

TA的得分主题

发表于 2020-1-17 12:33 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关注官方微信,高效办公专列,每天发车

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

GMT+8, 2020-10-1 23:50 , Processed in 0.065728 second(s), 16 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2021 Wooffice Inc.

   

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

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

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