小春网

 找回密码
 注册账号
查看: 2157|回复: 39
收起左侧

[IT 交流] 这段VBA代码怎么实现

[复制链接]
发表于 2011-11-28 13:38:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册账号

x
两张格式相同的有表头的表的部分列数据一对一对的相乘,相乘到Descolumncount-1列为止,剩下数据同运算结果复制到第三张表里。
发表于 2011-11-28 22:56:35 来自手机 | 显示全部楼层
好抽象的要求呀。每个sheet里有几张表?还是对三本excel操作?
回复

使用道具 举报

发表于 2011-11-28 23:01:43 | 显示全部楼层
sundf 发表于 2011-11-28 22:56
好抽象的要求呀。每个sheet里有几张表?还是对三本excel操作?

估计是针对一个Excel文件的三个Sheets进行操作
回复

使用道具 举报

发表于 2011-11-28 23:09:29 | 显示全部楼层
那就直接在CELL里写公式[.5EBB90.]
回复

使用道具 举报

 楼主| 发表于 2011-11-29 00:19:05 | 显示全部楼层
sundf 发表于 2011-11-28 22:56
好抽象的要求呀。每个sheet里有几张表?还是对三本excel操作?

一张表,就一张表
回复

使用道具 举报

 楼主| 发表于 2011-11-29 00:19:21 | 显示全部楼层
sundf 发表于 2011-11-28 23:09
那就直接在CELL里写公式

动态的,不是固定的
回复

使用道具 举报

发表于 2011-11-29 10:16:36 来自手机 | 显示全部楼层
两张sheet格式一样,但表的位置,大小不固定是吧?
回复

使用道具 举报

 楼主| 发表于 2011-11-29 10:33:31 | 显示全部楼层
sundf 发表于 2011-11-29 10:16
两张sheet格式一样,但表的位置,大小不固定是吧?

对。而且后边的分组列也是动态选取的,可能是三列,也可能是一列
回复

使用道具 举报

发表于 2011-11-29 22:33:00 | 显示全部楼层
还是不大明白你想做什么,Descolumncount又是什么?参数?
表头位置怎么找,非空字符算吗?
随便写了个function,不知道你想要怎么样的。参数是3张sheet的sheet名和你的Descolumncount。。。

Function dosheet(sheet1 As String, sheet2 As String, sheet3 As String, descount As Integer)

Dim StartRow, StartCol, EndRow, EndCol, tmpr, tmpc As Integer
EndRow = Sheets(sheet1).Cells.SpecialCells(xlLastCell).Row
EndCol = Sheets(sheet1).Cells.SpecialCells(xlLastCell).Column

'找表头。。。
For tmpr = 1 To EndRow
    For tmpc = 1 To EndCol
        If Trim(Sheets(sheet1).Cells(tmpr, tmpc)) <> "" Then
            StartRow = tmpr + 1
            StartCol = tmpc
            tmpr = EndRow + 1
            tmpc = EndCol + 1
        End If
    Next
Next
'COPY表头
For tmpc = StartCol To EndCol
    Sheets(sheet3).Cells(StartRow - 1, tmpc) = Sheets(sheet1).Cells(StartRow - 1, tmpc)
Next

For tmpr = StartRow To EndRow
    For tmpc = StartCol To EndCol
        'descount-1为止相乘
        If tmpc < descount Then
            Sheets(sheet3).Cells(tmpr, tmpc) = Sheets(sheet1).Cells(tmpr, tmpc) * Sheets(sheet2).Cells(tmpr, tmpc)
        Else
        '其他COPY
            Sheets(sheet3).Cells(tmpr, tmpc) = Sheets(sheet1).Cells(tmpr, tmpc)
        End If
    Next
Next
End Function

评分

参与人数 1 +88 收起 理由
憨吃迷糊睡 + 88 大赞

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2011-11-30 00:26:35 | 显示全部楼层
sundf 发表于 2011-11-29 22:33
还是不大明白你想做什么,Descolumncount又是什么?参数?
表头位置怎么找,非空字符算吗?
随便写了个func ...

十分感谢,我研究一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小春网
常务客服微信
微信订阅号
手机客户端
扫一扫,查看更方便! 快速回复 返回顶部 返回列表