標題:
[分享]
從多個分頁資料把合併到主頁
[打印本頁]
作者:
kimbal
時間:
2010-8-2 00:36
標題:
從多個分頁資料把合併到主頁
本帖最後由 kimbal 於 2010-8-3 22:06 編輯
把分頁資料合併到主頁的方法
單純把欄抄出,不做彙總.
彙總的話, 請先了解內建"樞紐表"和"合併彙總"用法
欄位位置不拘,但A欄為主鍵(可手工調節),
分頁可以不放欄位.
如果欄名跟主頁不一致,可在主頁欄位註解上填上,用豆號分隔
除主鍵外其他欄內容可以為空.
[attach]2152[/attach]
歡迎留意見/討論
Option Explicit
Public Sub refresh_all()
Dim shtCurr As Worksheet
Dim rngHeader As Range, rngResult As Range, strHeaderArray, strHeader
Dim lngLastPosition As Long
With ActiveSheet
.[A1].CurrentRegion.Offset(1).Clear
lngLastPosition = 2
For Each shtCurr In Sheets
If shtCurr.Name <> .Name Then
For Each rngHeader In .[A1].CurrentRegion.Rows(1).Cells
If (rngHeader.Comment Is Nothing) Then
strHeaderArray = Split(rngHeader.Value)
Else
strHeaderArray = Split(rngHeader.Comment.Text & "," & rngHeader.Value, ",")
End If
For Each strHeader In strHeaderArray
Set rngResult = shtCurr.Rows(1).Find(strHeader, LookIn:=xlValues, LookAt:=xlWhole)
If Not rngResult Is Nothing Then
shtCurr.Range(rngResult.Offset(1), _
rngResult.Offset(rngResult.CurrentRegion.SpecialCells(xlCellTypeLastCell).Row - 1)).Copy Destination:= _
.Cells(lngLastPosition, rngHeader.Column)
Exit For
End If
Next
Next
lngLastPosition = .Cells(Rows.Count, 1).End(xlUp).Row + 1
End If
Next
End With
End Sub
複製代碼
感謝GBKEE大出手相助簡化, 借花敬佛, 再加些東西.
作者:
GBKEE
時間:
2010-8-2 19:30
本帖最後由 GBKEE 於 2010-8-2 19:31 編輯
回復
1#
把分頁資料合併到主頁的方法
單純把欄抄出,不做彙總.
kimbal板主
Public Sub refresh_all()
Dim Sh As Worksheet, R As Range, F As Range
With ActiveSheet
.[A1].CurrentRegion.Offset(1).Clear
For Each R In .[A1].CurrentRegion.Rows(1).Cells
For Each Sh In Sheets
If Sh.Name <> .Name Then
Set F = Sh.Rows(1).Find(R, LookIn:=xlValues, LookAt:=xlWhole)
Sh.Range(F.Offset(1), F.End(xlDown)).Copy .Cells(Rows.Count, R.Column).End(xlUp).Offset(1)
End If
Next
Next
End With
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)