动态行插入没有固定标题
2008-12-05 15:04
162 查看
今天写的代码我感觉比较优雅,虽然用的是不熟悉的语言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里的那个什么伪代码的方法写代码比较好。
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里的那个什么伪代码的方法写代码比较好。
相关文章推荐
- html固定标题列、标题头table具体实现代码
- 面试题:一个数组插入另一个数组固定的位置
- Android 自定义listview滑到顶部标题固定 轻松实现360软件详情页效果
- 如何固定表格的标题行和标题列
- LaTex学习教程——插入较复杂的表格(合并、换行以及加标题)
- ASP.NET 固定标题列与栏位的具体实现
- (1_255)Qt拖动没有标题栏的窗体
- 文章标题编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 动态的把固定格式的json数据以菜单形式插入
- 没有躲过的坑--map的初始化(插入数据)
- sql表没有自增列,插入自增列值
- 爱情没有固定的模样
- 如何让GridView控件在绑定的数据源没有数据时也显示标题
- 固定表格的标题列(CSS)
- 程序返回插入数据库成功,但是数据库内却没有数据
- JAVA中怎么向一个字符串的固定位置插入字符串
- 实现标题固定,上滑覆盖标题的效果
- 鼠标拖动窗口和拉伸(没有标题栏时)
- [原]暂时没有标题
- ACER笔记本插入耳机没有声音