|
【测试源文件1】和【测试源文件2】 各有两个sheets
其中sheets("单户资金日报汇总表") 无论是公式状态还是值的状态
都能被【汇总文件】的宏取数并赋值给自己
但是sheets("资金流水汇总表") 只有单元格里存储的是值的时候
才能被【汇总文件】的宏取数并赋值给自己
求解决方案
自己不太好的思路是把sheets("资金流水汇总表")转为值 代码如下——但并没有实际效果 原因不明
fn = ThisWorkbook.Path & "\" & filename
Set wb = GetObject(fn) '在后台打开一张表
filename = Dir(ThisWorkbook.Path & "\*.xls") '获取该文件夹下的所有表的表名
Do While filename <> ""
If filename = ThisWorkbook.Name Then '为了避免合并的总表自己调用自己
Set Sht1 = wb.Worksheets("资金流水汇总表") '只合并该工作簿中的第一张表
Sht1.UsedRange = Sht1.UsedRange.Value
- Sub comb()
-
- Application.ScreenUpdating = False
- Dim rng1 As Range, rng2 As Range, rng3 As Range
- Debug.Print e
- Set rng1 = ThisWorkbook.Worksheets("资金余额采集").Range("a2:l400")
- rng1.ClearContents
- Set rng2 = ThisWorkbook.Worksheets("资金流入采集").Range("a2:f400")
- rng2.ClearContents
- Set rng3 = ThisWorkbook.Worksheets("资金流出采集").Range("a2:f400")
- rng3.ClearContents
- Dim r As Long, bkcol1 As Long, bkcol2 As Long
- r = 2
- bkcol1 = 14 'c的值是为了控制有几列,可以根据实际情况调整
- bkcol2 = 5 'c的值是为了控制有几列,可以根据实际情况调整
-
-
- Dim filename As String, fn As String, flag As String, wb As Workbook, sht As Worksheet
- Dim erow As Long, erow1 As Long, erow2 As Long, arr As Variant, arr1 As Variant, arr2 As Variant, arr3 As Variant
- filename = Dir(ThisWorkbook.Path & "\*.xls") '获取该文件夹下的所有表的表名
- Do While filename <> ""
- If filename = ThisWorkbook.Name Then '为了避免合并的总表自己调用自己
- Else
- erow = ThisWorkbook.Worksheets("资金余额采集").Range("a1").CurrentRegion.Rows.Count + 1 '为了找出要粘贴到汇总表的位置
- erow1 = ThisWorkbook.Worksheets("资金流入采集").Range("a1").CurrentRegion.Rows.Count + 1 '为了找出要粘贴到汇总表的位置
- erow2 = ThisWorkbook.Worksheets("资金流出采集").Range("a1").CurrentRegion.Rows.Count + 1 '为了找出要粘贴到汇总表的位置
- 'MsgBox (erow)
- 'MsgBox (erow1)'!!!
- 'MsgBox (erow2)
-
- fn = ThisWorkbook.Path & "" & filename
- Set wb = GetObject(fn) '在后台打开一张表
-
-
- '第一张表
- Set sht = wb.Worksheets("单户资金日报汇总表") '数据源sheets表单1
- arr = sht.Range(sht.Cells(10, "b"), sht.Cells(65536, "b").End(xlUp).Offset(0, bkcol1)) 'arr找到要复制的区域,运行此句时含表头
-
- ThisWorkbook.Worksheets("资金余额采集").Cells(erow, "a").Resize(UBound(arr, 1), UBound(arr, 2)) = arr 'UBound(arr, 1)计算出行数,UBound(arr, 2)计算出列数
-
- '第二张表
- Set Sht1 = wb.Worksheets("资金流水汇总表") '只合并该工作簿中的第一张表
- Sht1.UsedRange = Sht1.UsedRange.Value
- Dim a, b, c, r1 As Integer
- With Sht1
- Sum = Application.WorksheetFunction.Sum(.Range("e1:e200"))
- 'Dim t1, t2, t3, t4, t5
- 't1 = sht1.Cells(2, "e").Value
- 't2 = sht1.Cells(3, "e").Value
- 't3 = sht1.Cells(4, "e").Value
- 't4 = sht1.Cells(5, "e").Value
- 't5 = sht1.Cells(6, "e").Value
- 'MsgBox ("t1=" & t1 & ",t2=" & t2 & ",t3=" & t3 & ",t4=" & t4 & ",t5=" & t5)
- 'MsgBox "sumr=" & Sum
- If Sum = 0 Then
- Else
- For a = 200 To 1 Step -1
- b = Application.WorksheetFunction.Sum(.Range(CStr("e1:e" & a)))
- c = Application.WorksheetFunction.Sum(.Range(CStr("e1:e" & a - 1)))
- If b <> c Then
- r1 = a
-
- Exit For
- Else
- End If
- Next
- 'MsgBox r1
- arr1 = Sht1.Range(.Cells(2, "a"), .Cells(r1, "e")).Value
- ThisWorkbook.Worksheets("资金流入采集").Cells(erow1, 1).Resize(UBound(arr1, 1), UBound(arr1, 2)) = arr1
- End If
- End With
-
-
- '第三张表
- Dim x, y, z, r2 As Integer
- With Sht1
- Sum = Application.WorksheetFunction.Sum(.Range("j1:j200"))
- MsgBox "sumc=" & Sum
- If Sum = 0 Then
- Else
- For x = 200 To 1 Step -1
- y = Application.WorksheetFunction.Sum(.Range(CStr("j1:j" & x)))
- z = Application.WorksheetFunction.Sum(.Range(CStr("j1:j" & x - 1)))
- If y <> z Then
- r2 = x
-
- Exit For
- Else
- End If
- Next
- 'MsgBox r2
- MsgBox CStr("f2:j" & r2)
- arr2 = Sht1.Range(CStr("f2:j" & r2)).Value
- ThisWorkbook.Worksheets("资金流出采集").Cells(erow2, 1).Resize(UBound(arr2, 1), UBound(arr2, 2)) = arr2
- End If
- End With
- wb.Close False '将刚才打开的表关闭
- End If
- filename = Dir '运行此句时filename获取下一个表的表名
- Loop
- Application.ScreenUpdating = True
- End Sub
复制代码
|
|