您的位置:程序门 -> vb -> vba



大家帮忙写一个宏吧


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


大家帮忙写一个宏吧[已结贴,结贴人:jiubingcs01]
发表于:2007-08-10 15:28:50 楼主
在word画一个报表,金额那一项做了个文字型窗体域,并给了书签,要通过书签来取值,最后打印。现在要把金额那一项改成详细的亿千百十万千百十元角分的形式,我想在每个格里都做一个文字型窗体域,然后分别写宏,在窗体里调用。这个宏该如何写呢?我现在不清楚书签里的值要如何调用,请指教~或者大家有其他什么建议,请提出。
发表于:2007-08-10 17:01:421楼 得分:0
看不太懂...是不是要把表格里某列的数字变成中文?
发表于:2007-08-10 17:19:352楼 得分:0
不是,就是把金额的数字每一位分开写,就跟收据上面添金额那样,一格一格的
发表于:2007-08-10 17:44:253楼 得分:0
sub   金额拆分示例()
'
'   金额拆分示例   macro
'   宏在   2007-8-10   由   kmlxk@yahoo.com.cn   录制
'
        dim   mynewtable   as   table
        dim   i   as   long,   j   as   long
        dim   smoney   as   string
        dim   dmoney   as   double
        dim   lmoney100   as   long
       
        '新建5行9列的表
        set   mynewtable   =   activedocument.tables.add(range:=selection.range,   numrows:=5,   numcolumns:=   _
                9,   defaulttablebehavior:=wdword9tablebehavior,   autofitbehavior:=   _
                wdautofitfixed)
        with   mynewtable
                '填充表头
                .cell(1,   1).select:   selection.text   =   "项目 "
                .cell(1,   2).select:   selection.text   =   "金额 "
                .cell(1,   3).select:   selection.text   =   "万 "
                .cell(1,   4).select:   selection.text   =   "千 "
                .cell(1,   5).select:   selection.text   =   "百 "
                .cell(1,   6).select:   selection.text   =   "十 "
                .cell(1,   7).select:   selection.text   =   "元 "
                .cell(1,   8).select:   selection.text   =   "角 "
                .cell(1,   9).select:   selection.text   =   "分 "
                '填充金额
                .cell(2,   2).select:   selection.text   =   "645.3 "
                .cell(3,   2).select:   selection.text   =   "95.32 "
                .cell(4,   2).select:   selection.text   =   "786.80 "
                .cell(5,   2).select:   selection.text   =   "12351.51 "
       
                for   i   =   2   to   5   '循环每行
                        .cell(i,   2).select
                        dmoney   =   val(selection.text)   '取得金额
                        lmoney100   =   dmoney   *   100   '金额扩大100倍,去除小数点
                        smoney   =   right(string(10,   "0 ")   &   cstr(lmoney100),   7)   '金额前填充10个0,然后截取后面7位
                        for   j   =   1   to   len(smoney)   '循环每一位填充到表格中
                                .cell(i,   j   +   2).select
                                selection.text   =   mid(smoney,   j,   1)
                        next
                next
               
        end   with
end   sub
发表于:2007-08-10 17:56:054楼 得分:0
金额限制条件  
最大:亿           小数:两位
sub   金额拆分示例()
'
'   金额拆分示例   macro
'   宏在   2007-8-10   由   kmlxk@yahoo.com.cn   录制
'
        dim   mynewtable   as   table
        dim   i   as   long,   j   as   long
        dim   smoney   as   string
        dim   dmoney   as   double
        dim   lmoney100
        '新建5行13列的表
        set   mynewtable   =   activedocument.tables.add(range:=selection.range,   numrows:=5,   numcolumns:=   _
                13,   defaulttablebehavior:=wdword9tablebehavior,   autofitbehavior:=   _
                wdautofitfixed)
        with   mynewtable
                '填充表头
                .cell(1,   1).select:   selection.text   =   "项目 "
                .cell(1,   2).select:   selection.text   =   "金额 "
                .cell(1,   3).select:   selection.text   =   "亿 "
                .cell(1,   4).select:   selection.text   =   "千 "
                .cell(1,   5).select:   selection.text   =   "百 "
                .cell(1,   6).select:   selection.text   =   "十 "
                .cell(1,   7).select:   selection.text   =   "万 "
                .cell(1,   8).select:   selection.text   =   "千 "
                .cell(1,   9).select:   selection.text   =   "百 "
                .cell(1,   10).select:   selection.text   =   "十 "
                .cell(1,   11).select:   selection.text   =   "元 "
                .cell(1,   12).select:   selection.text   =   "角 "
                .cell(1,   13).select:   selection.text   =   "分 "
                '填充金额
                .cell(2,   2).select:   selection.text   =   "49645.3 "
                .cell(3,   2).select:   selection.text   =   "697395.32 "
                .cell(4,   2).select:   selection.text   =   "786.80 "
                .cell(5,   2).select:   selection.text   =   "289712351.51 "
                '循环每行
                for   i   =   2   to   5
                        .cell(i,   2).select
                        dmoney   =   val(selection.text)   '取得金额
                        lmoney100   =   dmoney   *   100   '金额扩大100倍,去除小数点
                        smoney   =   right(string(12,   "0 ")   &   cstr(lmoney100),   11)   '金额前填充12个0,然后截取后面11位
                        for   j   =   1   to   len(smoney)   '循环每一位填充到表格中
                                .cell(i,   j   +   2).select
                                selection.text   =   mid(smoney,   j,   1)
                        next
                next
        end   with
end   sub
发表于:2007-08-13 09:25:525楼 得分:0
我是楼主,我还有个问题啊,因为要求是从书签读取金额的数据,这个怎么弄呢?比如金额的书签为ki011。数据值并不是直接输入的
发表于:2007-08-13 09:51:116楼 得分:0
能不能具体点,什么样的书签

仅仅指向数据开头,还是包含头尾的书签
发表于:2007-08-13 10:05:587楼 得分:0
还有啊,这样写的话,如果金额比较小时,前面都空格里都是0,2342.32就成了000002342.32了,这就不对了,有什么解决的方法没有?
发表于:2007-08-13 10:12:298楼 得分:0
书签就是打印这些凭证的会计科目所对应的书签,比如:本位金额(借)   第一行:ki011,第二行:ki012。。。以此类推。总共5行。通过这些书签来取值
发表于:2007-08-13 11:38:349楼 得分:0
还有啊,如果现在所有的格都分好了,也就是不用新建那个表,要怎么做呢?
发表于:2007-08-13 16:07:2410楼 得分:0
这就要看表的格式了...

你可以描述下,或者...把word文档发到kmlxk@yahoo.com.cn我看看
发表于:2007-08-14 10:18:3111楼 得分:100
以获取第一行金额为例,代码如下


sub   获取金额示例()
'
'   获取金额示例   macro
'   宏在   2007-8-14   由   kmlxk@yahoo.com.cn   录制
'
        dim   smoney   as   string
        dim   sbit   as   string
        dim   i   as   long
        dim   x   as   long,   y   as   long
       
       
        smoney   =   space(12)   '长度为12的字符串,储存金额
        mid(smoney,   10,   1)   =   ". "   '小数点
       
       
        activedocument.bookmarks( "ki011 ").select   '选择书签
       
        selection.selectcell   '选择书签所在单元格
        mid(smoney,   12,   1)   =   left(selection.text,   1)   '保存书签指向的   '分 '
       
        '获取单元格   行列
        x   =   selection.cells(1).columnindex   -   1   '前一单元格
        y   =   selection.cells(1).rowindex
       
        selection.tables(1).cell(y,   x).select     '选择前一单元格   '角 '
        mid(smoney,   11,   1)   =   left(selection.text,   1)   '保存
       
        '循环选择整数部分
        for   i   =   1   to   9
                selection.tables(1).cell(y,   x   -   i).select
                sbit   =   left(selection.text,   1)
                if   isnumeric(sbit)   then   '如果单元格中填有数字
                        mid(smoney,   10   -   i,   1)   =   sbit   '保存
                end   if
        next
        msgbox   smoney   '最后金额
end   sub


sub   金额拆分示例()
'金额限制条件
'最大:   亿   小数:   两位
'
'   金额拆分示例   macro
'   宏在   2007-8-10   由   kmlxk@yahoo.com.cn   录制
'
        dim   mynewtable   as   table
        dim   i   as   long,   j   as   long
        dim   smoney   as   string
        dim   dmoney   as   double
        dim   lmoney100
        '新建5行13列的表
        set   mynewtable   =   activedocument.tables.add(range:=selection.range,   numrows:=5,   numcolumns:=   _
                13,   defaulttablebehavior:=wdword9tablebehavior,   autofitbehavior:=   _
                wdautofitfixed)
        with   mynewtable
                '填充表头
                .cell(1,   1).select:   selection.text   =   "项目 "
                .cell(1,   2).select:   selection.text   =   "金额 "
                .cell(1,   3).select:   selection.text   =   "亿 "
                .cell(1,   4).select:   selection.text   =   "千 "
                .cell(1,   5).select:   selection.text   =   "百 "
                .cell(1,   6).select:   selection.text   =   "十 "
                .cell(1,   7).select:   selection.text   =   "万 "
                .cell(1,   8).select:   selection.text   =   "千 "
                .cell(1,   9).select:   selection.text   =   "百 "
                .cell(1,   10).select:   selection.text   =   "十 "
                .cell(1,   11).select:   selection.text   =   "元 "
                .cell(1,   12).select:   selection.text   =   "角 "
                .cell(1,   13).select:   selection.text   =   "分 "
                '填充金额
                .cell(2,   2).select:   selection.text   =   "49645.3 "
                .cell(3,   2).select:   selection.text   =   "697395.32 "
                .cell(4,   2).select:   selection.text   =   "786.80 "
                .cell(5,   2).select:   selection.text   =   "289712351.51 "
                '循环每行
                for   i   =   2   to   5
                        .cell(i,   2).select
                        dmoney   =   val(selection.text)   '取得金额
                        lmoney100   =   dmoney   *   100   '金额扩大100倍,去除小数点
                        smoney   =   right(string(12,   "   ")   &   cstr(lmoney100),   11)   '金额前填充12个0,然后截取后面11位
                        for   j   =   1   to   len(smoney)   '循环每一位填充到表格中
                                .cell(i,   j   +   2).select
                                selection.text   =   mid(smoney,   j,   1)
                        next
                next
        end   with
end   sub

发表于:2007-08-14 11:38:1112楼 得分:0
谢谢啦~我会继续努力地


快速检索

最新资讯
热门点击