动态行插入没有固定标题
来源:互联网 发布:vb如何连接access 编辑:程序博客网 时间:2024/06/04 18:23
今天写的代码我感觉比较优雅,虽然用的是不熟悉的语言VBA。解决问题用了table lookup,对于没有固定标题的情况下我又新增了一个变量来得到不确定的行数。代码如下:
Option Explicit
Public insertAllNum As Long 'query里插入的所有的行数
Public dynNum As Integer '动态插入一个标题则加一
Dim InsertTitle(3) As String '记录四个动态的标题
Const C_conCompanyCol As Integer = 3
'p_queryRows:query起始行位置
'p_queryCol: query起始列位置
'p_conRows:workbook起始行位置
'p_conCol:workbook起始列位置
'p_ContentSheet: 报表的sheet名称
'p_linesToReplace: 要被替换的列数
'p_insertType: 插入的类型即插入到那一个标题下
Sub insert(p_queryRows As Long, p_queryCol As Long, p_conRows As Long, p_conCol As Long, _
p_ContentSheet As String, p_linesToReplace As Integer, p_insertType As Integer)
Dim Count As Long '复制的列数
Dim oneTimeInsert As Long '本次插入的行数
'结果清零
For Count = 6 To 10
Worksheets(p_ContentSheet).Cells(p_conRows - 1, Count).Value = 0
Next Count
'先将数据全部清除
'选中要删除的区间C_deltePlace
While Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol).Value <> "" '根据空行判断是否删除
'选中删除
Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol).Select
Selection.EntireRow.Delete
Wend
'如果标题不一样则不存在此标题,那么退出过程
If InsertTitle(p_insertType) <> Worksheets("QUERY").Cells(p_queryRows, p_queryCol - 2).Value Then
Exit Sub
Else
'插入标题数
dynNum = dynNum + 1
End If
With Worksheets("QUERY")
'在query最后一行结束的标记为"结果"且数据不能为空,而且不能到达最后一行
While Cells(p_queryRows, p_queryCol - 1).Value <> "结果" And _
Cells(13, 1).Value <> "未找到可用的数据." And _
Cells(p_queryRows, p_queryCol - 2).Value <> "总计结果"
'报表里插入新行
Worksheets(p_ContentSheet).Rows(p_conRows).insert Shift:=xlDown
'填入填报单位
Worksheets(p_ContentSheet).Cells(p_conRows, C_conCompanyCol).Value = Cells(10, 2).Value
'复制linesToReplace列
For Count = 0 To p_linesToReplace - 1
Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol + Count).Value = _
.Cells(p_queryRows, p_queryCol + Count).Value
Next Count
'更新赋值的行号
insertAllNum = insertAllNum + 1
oneTimeInsert = oneTimeInsert + 1
'更新行号
p_queryRows = p_queryRows + 1
Wend
'序号填充
For Count = 0 To oneTimeInsert - 1
Worksheets(p_ContentSheet).Cells(p_conRows + Count, 2).Value = Count + 1
Worksheets(p_ContentSheet).Cells(p_conRows + Count, 2).Select
' Selection.Borders(xlEdgeRight).LineStyle = xl
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
End With
Next Count
'引用合计结果
'年初余额
Worksheets(p_ContentSheet).Cells(p_conRows - 1, p_conCol + 2).Value = _
Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 2).Value
'年初值准备
Worksheets(p_ContentSheet).Cells(p_conRows - 1, p_conCol + 3).Value = _
Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 3).Value
'年末余额
Worksheets(p_ContentSheet).Cells(p_conRows - 1, p_conCol + 4).Value = _
Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 4).Value
'年末减值准备
Worksheets(p_ContentSheet).Cells(p_conRows - 1, p_conCol + 5).Value = _
Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 5).Value
'本年收益(损失以“-”号填列)
Worksheets(p_ContentSheet).Cells(p_conRows - 1, p_conCol + 6).Value = _
Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 6).Value
End With
End Sub
Sub Main()
insertAllNum = 0
dynNum = 0
InsertTitle(0) = "一、交易性金融资产"
InsertTitle(1) = "二、可供出售金融资产"
InsertTitle(2) = "三、持有至到期投资"
InsertTitle(3) = "四、长期应收款"
'一、交易性金融资产
insert 14, 3, 8, 4, "集团内部金融资产情况表", 7, 0
'二、可供出售金融资产
insert 14 + insertAllNum + dynNum, 3, 8 + insertAllNum + 1, 4, "集团内部金融资产情况表", 7, 1
'三、持有至到期投资
insert 14 + insertAllNum + dynNum, 3, 8 + insertAllNum + 2, 4, "集团内部金融资产情况表", 7, 2
'四、长期应收款
insert 14 + insertAllNum + dynNum, 3, 8 + insertAllNum + 3, 4, "集团内部金融资产情况表", 7, 3
End Sub
上面的是报表界面。引用的query界面如下:
写代码还是要想清楚再写,之前写的时候就没有考虑到dynNum这个变量,所以还是用code complete里的那个什么伪代码的方法写代码比较好。
- 动态行插入没有固定标题
- 固定行标题和列标题
- 如何固定表格的标题行和标题列
- 如何固定表格的标题行和标题列
- 在Excel中固定行标题和列标题
- Excel 中固定行标题和列标题
- Html:如何固定表格的标题行和标题列
- 动态标题行
- 没有标题
- 没有标题
- 没有标题
- 没有标题
- 没有标题
- 没有标题
- 没有标题的标题
- 固定DataGrid的标题
- jquery实现标题固定
- Tablayoutdemo标题固定
- 处理中文rss解析的一点经验
- 是否该将大学生作为成人来对待
- MessageBox的返回值
- package/ import/权限修饰符/extends /overload/overwrite(override)
- Linux内核配置文件之解读Makefile
- 动态行插入没有固定标题
- 用GDB调试程序(笔记)
- 输变电设备状态检修试验规程 Q/GDW 168-2008
- 忘记MySQL密码的解决办法
- 一个ASP.NET长文章分页显示函数(转自:鹰之旅途)
- robots.txt 搜索
- 视频游戏中音频系统的基本功能
- RSS4J解析网络上指定URL的RSS文档的例子
- webkit paint html 调用栈