標題:
[發問]
總表匯集試問
[打印本頁]
作者:
macro0029
時間:
2018-1-4 15:21
標題:
總表匯集試問
我第一個是這樣寫,但是跳第二個偵測最下列的部分一直有問題
Sheets("主題1").Select
ActiveSheet.Range("A2", ActiveSheet.Range("B2").End(xlDown)).Select
Selection.Copy
Sheets("總表").Select
Range("A2").Select
ActiveSheet.Paste
Range("a65536").End(xlUp).Select
Range("A" & ActiveCell.Row + 1).Value = "↑主題1"
Sheets("主題2").Select
ActiveSheet.Range("A2", ActiveSheet.Range("B2").End(xlDown)).Select
Selection.Copy
Sheets("總表").Select
....
....從這裡偵測最下列+1的部分,我有上網看文章試過很多方法但是都不大行
再麻煩各位大大幫我看看 ..
附上檔案舉例說明
作者:
GBKEE
時間:
2018-1-5 14:22
回復
1#
macro0029
試試看
Option Explicit
Sub Ex()
Dim Sh As Worksheet, Rng As Range, xRng As Range, i As Long
Set Sh = Sheets("總表") '**資料庫的工作表
Set xRng = Sh.Range("a1", Sh.Range("a1").End(xlToRight).End(xlDown)) '**資料庫的範圍
Set Rng = Sh.Cells(1, Columns.Count - 1).Resize(2, 2) '**進階篩選的準則範圍
Rng.Range("B1") = Sh.Range("A1").End(xlToRight) '**進階篩選的準則欄位 [類別]
Rng.Range("A1") = Sh.Range("A1") & "A" '**進階篩選的準則欄位 [製票機構A]
'*PS:重要** 計算式準則時,準則的欄位不可與資料庫的欄位同****
Rng.Range("B2") = "收支調整" '**進階篩選的準則欄位 [類別] 條件
With Sheets("主題1")
.Cells.Clear
'***[製票機構]此欄資料的數字為文字格式****
Rng.Range("A2") = "=製票機構<>""7070""" '**進階篩選的準則欄位 [製票機構A] 條件
xRng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Rng, CopyToRange:=.[a1]
'**xRng.AdvancedFilter:資料庫的進階篩選方法
'**參數 xlFilterCopy 資料庫的進階篩選動作
'**參數 CriteriaRange :準則範圍
'**參數 CopyToRange: 如果 Action 為 xlFilterCopy,此引數即為複製列的目的範圍。否則,會忽略此引數。
.Range("B:C").Delete '(發動機構,列帳機構) 刪除
.Range("E1") = "會計科目"
i = 2
Do
.Cells(i, "E") = .Cells(i, "E") & .Cells(i, "F") '(總,細),合併至E欄(會計科目)
i = i + 1
Loop Until .Cells(i, "E") = ""
.Range("E:E").NumberFormatLocal = "@" '設為文字格式
.Range("F:F").Delete
End With
With Sheets("主題2")
.Cells.Clear
' Rng.Range("A2") = "=製票機構=""7070"""
xRng.AdvancedFilter xlFilterCopy, Rng, .[a1]
.Range("b:c").Delete
.Range("E1") = "會計科目"
i = 2
Do
.Cells(i, "E") = .Cells(i, "E") & .Cells(i, "F")
i = i + 1
Loop Until .Cells(i, "E") = ""
.Range("E:E").NumberFormatLocal = "@"
.Range("F:F").Delete
End With
Rng.Clear
End Sub
複製代碼
作者:
Hsieh
時間:
2018-1-5 16:42
回復
1#
macro0029
Sub ex()
Dim ar(), ay()
With Sheets("總表")
r = 2
Do Until .Cells(r, 1) = ""
If .Cells(r, "J") = "收支調整" Then
If .Cells(r, "A") = "7070" Then
ReDim Preserve ar(s)
ar(s) = Array(.Cells(r, "A"), .Cells(r, "D"), .Cells(r, "E"), .Cells(r, "F"), .Cells(r, "G") & .Cells(r, "H"), .Cells(r, "I"), .Cells(r, "J"))
s = s + 1
Else
ReDim Preserve ay(i)
ay(i) = Array(.Cells(r, "A"), .Cells(r, "D"), .Cells(r, "E"), .Cells(r, "F"), .Cells(r, "G") & .Cells(r, "H"), .Cells(r, "I"), .Cells(r, "J"))
i = i + 1
End If
End If
r = r + 1
Loop
End With
Sheets("製票機構是7070且類別為收支調整").[A2].Resize(s, 7) = Application.Transpose(Application.Transpose(ar))
Sheets("製票機構非7070且類別為收支調整").[A2].Resize(i, 7) = Application.Transpose(Application.Transpose(ay))
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)