VBAプログラミングフィールドの初心者です。私は多くのデータを持つピボットファイルを持っています。今私はそのピボットから8つの異なるセグメントカットを行う必要があります。各セグメントのカットのうち、第1列のデータ情報に基づいて20の異なるシートにデータを解析またはグループ化する必要があります。これは、8つのセグメントカットすべてに対して実行する必要があります。次に、8つの異なるセグメントカットをそれぞれ8つのワークシートを含む20のワークブックにコンパイルする必要があります。だから私は1つの特定のセグメントのカットのためのデータを解析し、それぞれ1つのワークシートで20種類のワークブックを作った。今は、さまざまなワークブックを継続的にアクティブ化するのは非常に面倒で混乱しているので、最善のアプローチは何かを知りたがっています。 事前に感謝!複数のワークブックの複数のワークシートへのデータの解析
-1
A
答えて
0
まず、ピボットテーブルをcopy> paste special>値として保存し、次にスクリプトを実行する必要があると思います。
Sub Copy_To_Worksheets()
'Note: This macro use the function LastRow
Dim My_Range As Range
Dim FieldNum As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim ws2 As Worksheet
Dim Lrow As Long
Dim cell As Range
Dim CCount As Long
Dim WSNew As Worksheet
Dim ErrNum As Long
'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
'and the header of the first column, D is the last column in the filter range.
'You can also add the sheet name to the code like this :
'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
'No need that the sheet is active then when you run the macro when you use this.
Set My_Range = Range("A1:D" & LastRow(ActiveSheet))
My_Range.Parent.Select
If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is protected", _
vbOKOnly, "Copy to new worksheet"
Exit Sub
End If
'This example filters on the first column in the range(change the field if needed)
'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
FieldNum = 1
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Add a worksheet to copy the a unique list and add the CriteriaRange
Set ws2 = Worksheets.Add
With ws2
'first we copy the Unique data from the filter field to ws2
My_Range.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("A1"), Unique:=True
'loop through the unique list in ws2 and filter/copy to a new sheet
Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In .Range("A2:A" & Lrow)
'Filter the range
My_Range.AutoFilter Field:=FieldNum, Criteria1:="=" & _
Replace(Replace(Replace(cell.Value, "~", "~~"), "*", "~*"), "?", "~?")
'Check if there are no more then 8192 areas(limit of areas)
CCount = 0
On Error Resume Next
CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible) _
.Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas for the value : " & cell.Value _
& vbNewLine & "It is not possible to copy the visible data." _
& vbNewLine & "Tip: Sort your data before you use this macro.", _
vbOKOnly, "Split in worksheets"
Else
'Add a new worksheet
Set WSNew = Worksheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
WSNew.Name = cell.Value
If Err.Number > 0 Then
ErrNum = ErrNum + 1
WSNew.Name = "Error_" & Format(ErrNum, "0000")
Err.Clear
End If
On Error GoTo 0
'Copy the visible data to the new worksheet
My_Range.SpecialCells(xlCellTypeVisible).Copy
With WSNew.Range("A1")
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
' Remove this line if you use Excel 97
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With
End If
'Show all data in the range
My_Range.AutoFilter Field:=FieldNum
Next cell
'Delete the ws2 sheet
On Error Resume Next
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
On Error GoTo 0
End With
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
If ErrNum > 0 Then
MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
& vbNewLine & "There are characters in the name that are not allowed" _
& vbNewLine & "in a sheet name or the worksheet already exist."
End If
'Restore ScreenUpdating, Calculation, EnableEvents, ....
My_Range.Parent.Select
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
https://www.rondebruin.nl/win/s3/win006_4.htm
また、下記のリンクをチェックしてください。
関連する問題
- 1. 複数のワークブックからのワークシート・データのコピーとワークシートによるマスター・データ・ファイルへの貼り付け
- 2. 複数のワークシートにわたる配列の解析
- 3. 複数のワークシートへの値のコピー
- 4. 列をベースにした複数のワークブックへのデータの分割
- 5. 複数のExcelワークシートへのSQLエクスポート
- 6. 複数のワークシートでのデータの抽出
- 7. SQLserver 2008テーブルのワークブック内に複数のExcelワークシートを作成
- 8. 複数のワークシートを1つのワークブックにマージする - 画像エラー
- 9. 複数行のJSOUP解析
- 10. 複数レベルの解析テキスト
- 11. 複数変数の複数valusの解析
- 12. Microsoft Excel複数のワークシートとデータ
- 13. 複数のシートのデータを新しいワークブックの複数のシートにコピー
- 14. 複数のワークブックの複数のシートを1つのワークブックにコピーしたい
- 15. 複数のワークブックの複数のシートを1つのワークブックに統合する
- 16. 関数へのJsonデータの解析
- 17. 複数のワークシートのExcelコピーデータ
- 18. PowerQueryを使用して複数のワークブックを複数のサブディレクトリ内の複数のワークブックから取り出す
- 19. 2つの列に基づく複数のワークブックへのワークブックの分割
- 20. 複数のテキストファイルを既存のワークブック内の別々のワークシートにインポートする
- 21. 複数のネストされたWebページからのデータの解析
- 22. 複数のオブジェクトを含むリストビューへのJSON解析Android
- 23. 複数のJSONファイル、解析、表へのロード
- 24. Haskell - 複数の解析エラーの取得
- 25. 複数のサイトのリモート解析トラッキング
- 26. 解析スウィフトの保存複数のオブジェクト
- 27. 解析複数のJSONオブジェクトのみ
- 28. 複数の配列の解析
- 29. 複数のワークシートの列の値に基づいて新しいワークブックを生成
- 30. 複数のテキストファイルのデータをCSVに解析する
あなたの質問の質を改善するために、(https://stackoverflow.com/help/mcve)[、最小完全、かつ検証例を作成する方法]を見てください。より多くの人々は、全話を読むよりも、素早く正確な質問を得るのが好きです。 – DevDio