I have multiple pivot tables in different work sheets in excel (1 per worksheet). I would like to copy them all to a new worksheet but I want them to be below one another with a 2 row gap between each.
I have the code to copy a table from one worksheet to another, but I cannot figure out how to copy another to the same worksheet without pasting it over the previous table....
'Copy table 1
Sheet1.PivotTables(1).TableRange2.Copy
With Sheet7.Range(Sheet1.PivotTables(1).TableRange2.Address)
.PasteSpecial xlPasteValuesAndNumberFormats
.PasteSpecial xlPasteColumnWidths
End With
Application.CutCopyMode = False
Each pivot table can be dynamic in height (and width) and so the offset for the subsequent table will be dependent on the size of the previous one....
Does anyone have any idea how to implement this?
Sub CopyPT()
Dim rngDest As Range
Dim sht As Worksheet, tr As Range
Set rngDest = Sheet7.Range("B2")
For Each sht In ThisWorkbook.Worksheets
If sht.Name <> Sheet7.Name Then
If sht.PivotTables.Count = 1 Then
Set tr = sht.PivotTables(1).TableRange2
'Debug.Print sht.Name, tr.Rows.Count
tr.Copy
With rngDest
.PasteSpecial xlPasteValuesAndNumberFormats
.PasteSpecial xlPasteColumnWidths
End With
Set rngDest = rngDest.Offset(tr.Rows.Count + 2, 0)
End If
End If
Next sht
Application.CutCopyMode = False
End Sub
Related
Trying to combine multiple excel sheets is there a way to modify the below so that it does not copy table headers from the other sheets into a sheet called "Combined"?
Sub Combine()
'UpdatebyExtendoffice
Dim J As Integer
On Error Resume Next
Sheets(1).Select
Worksheets.Add
Sheets(1).Name = "Combined"
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=Sheets(1).Range("A1")
For J = 2 To Sheets.Count
Sheets(J).Activate
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
Next
End Sub
Quick rewrite and introducing some variables to hold the new sheet and copy range which should help for debugging.
This also incorporates the "skip two header rows" requirement:
Sub Combine()
Dim J As Integer
'Set the newly added sheet to a variable so we can reference.
Dim CombineSheet as Worksheet
Set CombineSheet = Worksheets.Add
CombineSheet.Name = "Combined"
'Assume you are copying the header here. It's a
' little risky hoping that Sheets(2) is going
' to be the one you want.
'Furthermore, getting rid of select/activate
' stuff. Instead directly say which sheet and
' range you want to copy and its destination.
Sheets(2).Range("A1").EntireRow.Copy Destination:=CombineSheet.Range("A1")
'Introducing a new variable to hold the range that will be copied
Dim copyRange as Range
For J = 2 To Sheets.Count
'Cutting out the activates and selects here
Set copyRange = Sheets(J).Range("A1").CurrentRegion
'Offset it and resize skipping 2 header rows and resizing the whole
' range to be 2 rows smaller (the 2 rows we just skipped)
copyRange = copyRange.Offset(2).Resize(copyRange.Rows.Count - 2)
'Copy/Paste
copyRange.Copy Destination:=CombineSheet.Range("A65536").End(xlUp)
Next
End Sub
The biggest change here, besides the removal of the .Select and .Activate is just offsetting by 2 rows and then resizing that range by -2 rows to accommodate that offset.
Try the next updated code, please:
Sub CombineSheets()
Dim J As Long, sh As Worksheet
Worksheets.Add Before:=Sheets(1)
Sheets(1).Name = "Combined"
Sheets(2).Range("A1").EntireRow.Copy Destination:=Sheets(1).Range("A1")
For Each sh In ActiveWorkbook.Sheets
If sh.Name <> Sheets(1).Name Then
sh.Range("A1").CurrentRegion.Offset(2).Resize(sh.Range("A" & sh.rows.Count).End(xlUp).row - 1).Copy _
Destination:=Sheets(1).Range("A" & sh.rows.Count).End(xlUp)(2)
End If
Next
End Sub
Selecting, activating only consume Excel resources and make the code slower...
I have a few tables in different worksheets that I want to compile in another sheet, however I’m having trouble getting the formatting to paste across.
My current code is:
Sub Compiler()
Dim wbRaw As Workbook
Set wbRaw = ThisWorkbook
Dim wsCompiled As Worksheet
Set wsCompiled = wbRaw.Sheets("ALL PROGRAMMES COMPILED")
Dim wsACF As Worksheet
Set wsACF = wbRaw.Sheets("ACF")
Dim wsASPIRE As Worksheet
Set wsASPIRE = wbRaw.Sheets("ASPIRE")
Application.ScreenUpdating = False
wsCompiled.Cells.ClearContents
wsACF.Cells(1, 1).CurrentRegion.Copy
With wsCompiled
.Range("A1").PasteSpecial xlPasteFormats
.Range("A1").PasteSpecial xlPasteValues
End With
Application.CutCopyMode = False
wsASPIRE.Cells(1, 1).CurrentRegion.Offset(1).Copy
With wsCompiled
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteFormats
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End With
Application.CutCopyMode = False
End Sub
The first table pastes across fine with the correct formatting, but the next table only pastes the values, with no formatting.
If I remove the offset from the copy line:
wsASPIRE.Cells(1, 1).CurrentRegion.Copy
The formatting pastes fine, but this then includes the headers from the second table which messes up the compiled data.
Can anyone explain why this happens and any suggestions how I can get around this?
I have not managed to find out why the formatting of tables does not paste across, however I figured out a workaround to the problem by looping through the tables in each worksheet and then unlisting them.:
Sub LoopTables()
Dim tbl As ListObject
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
For Each tbl In ws.ListObjects
tbl.Unlist
Next tbl
Next ws
End Sub
I have two sheets. One has the complete data and the other is based on the filter applied on the first sheet.
Name of the data sheet : Data
Name of the filtered Sheet : Hoky
I am just taking a small portion of data for simplicity. MY objective is to copy the data from Data Sheet, based on the filter. I have a macro which somehow works but its hard-coded and is a recorded macro.
My problems are:
The number of rows is different everytime. (manual effort)
Columns are not in order.
Sub TESTTHIS()
'
' TESTTHIS Macro
'
'FILTER
Range("F2").Select
Selection.AutoFilter
ActiveSheet.Range("$B$2:$F$12").AutoFilter Field:=5, Criteria1:="hockey"
'Data Selection and Copy
Range("C3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Hockey").Select
Range("E3").Select
ActiveSheet.Paste
Sheets("Data").Select
Range("D3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Hockey").Select
Range("D3").Select
ActiveSheet.Paste
Sheets("Data").Select
Range("E3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Hockey").Select
Range("C3").Select
ActiveSheet.Paste
End Sub
Best way of doing it
Below code is to copy the visible data in DBExtract sheet, and paste it into duplicateRecords sheet, with only filtered values. Range selected by me is the maximum range that can be occupied by my data. You can change it as per your need.
Sub selectVisibleRange()
Dim DbExtract, DuplicateRecords As Worksheet
Set DbExtract = ThisWorkbook.Sheets("Export Worksheet")
Set DuplicateRecords = ThisWorkbook.Sheets("DuplicateRecords")
DbExtract.Range("A1:BF9999").SpecialCells(xlCellTypeVisible).Copy
DuplicateRecords.Cells(1, 1).PasteSpecial
End Sub
I suggest you do it a different way.
In the following code I set as a Range the column with the sports name F and loop through each cell of it, check if it is "hockey" and if yes I insert the values in the other sheet one by one, by using Offset.
I do not think it is very complicated and even if you are just learning VBA, you should probably be able to understand every step. Please let me know if you need some clarification
Sub TestThat()
'Declare the variables
Dim DataSh As Worksheet
Dim HokySh As Worksheet
Dim SportsRange As Range
Dim rCell As Range
Dim i As Long
'Set the variables
Set DataSh = ThisWorkbook.Sheets("Data")
Set HokySh = ThisWorkbook.Sheets("Hoky")
Set SportsRange = DataSh.Range(DataSh.Cells(3, 6), DataSh.Cells(Rows.Count, 6).End(xlUp))
'I went from the cell row3/column6 (or F3) and go down until the last non empty cell
i = 2
For Each rCell In SportsRange 'loop through each cell in the range
If rCell = "hockey" Then 'check if the cell is equal to "hockey"
i = i + 1 'Row number (+1 everytime I found another "hockey")
HokySh.Cells(i, 2) = i - 2 'S No.
HokySh.Cells(i, 3) = rCell.Offset(0, -1) 'School
HokySh.Cells(i, 4) = rCell.Offset(0, -2) 'Background
HokySh.Cells(i, 5) = rCell.Offset(0, -3) 'Age
End If
Next rCell
End Sub
When i need to copy data from filtered table i use range.SpecialCells(xlCellTypeVisible).copy. Where the range is range of all data (without a filter).
Example:
Sub copy()
'source worksheet
dim ws as Worksheet
set ws = Application.Worksheets("Data")' set you source worksheet here
dim data_end_row_number as Integer
data_end_row_number = ws.Range("B3").End(XlDown).Row.Number
'enable filter
ws.Range("B2:F2").AutoFilter Field:=2, Criteria1:="hockey", VisibleDropDown:=True
ws.Range("B3:F" & data_end_row_number).SpecialCells(xlCellTypeVisible).Copy
Application.Worksheets("Hoky").Range("B3").Paste
'You have to add headers to Hoky worksheet
end sub
it needs to be .Row.count not Row.Number?
That's what I used and it works fine
Sub TransfersToCleared()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = Application.Worksheets("Export (2)") 'Data Source
LastRow = Range("A" & Rows.Count).End(xlUp).Row
ws.Range("A2:AB" & LastRow).SpecialCells(xlCellTypeVisible).Copy
I have some rows in copySheet.sheet1 having some special keyword in first column as "Ojha" . So I want to copy those entire row having "Ojha" in first row & paste it into another pasteSheet.sheet2 . At first I found that cell Having "Ojha". So I put it in Foundcell. So now I used...:
Foundcell.EntireRow.Copy
& in another sheet first I find the vacant rows from where the rows will paste, so
RowCount = WorksheetFunction.CountA(pasteSheet.Range("A:A")) + 1
pasteSheet.Range("A" & RowCount).PasteSpecial xlPasteValues
so it pasted only first row having "Ojha"
So now I want to copy all those rows which are having "Ojha" in the first column & paste to pasteSheet next to next.
If you just want to loop through the cells, this will work
Sub Loopy()
Dim sh As Worksheet, ws As Worksheet
Dim Rws As Long, rng As Range, c As Range
Set sh = Sheets("Sheet1")
Set ws = Sheets("Sheet2")
Application.ScreenUpdating = 0
With sh
Rws = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng = .Range(.Cells(1, 1), .Cells(Rws, 1))
End With
With ws
For Each c In rng.Cells
If c = "Ojha" Then
c.EntireRow.Copy
.Cells(.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
End If
Next c
End With
Application.CutCopyMode = 0
End Sub
You can also use an autofilter macro...
Is it possible to copy format of one excel sheet to another worksheet using VBA.
Like manually we can do by selecting entire sheet and then click on format button. And then select other worksheet and format will be copied. Is it possible to do by code.
Thanks & Regards
Sahil Chaudhary
Absolutely. Below is sample code.
see https://msdn.microsoft.com/en-us/library/office/ff837425.aspx
Sub Wsh_PasteSpecial()
Dim WshSrc As Worksheet
Dim WshTrg As Worksheet
Rem Set working worksheets
Set WshSrc = ThisWorkbook.Worksheets("Source")
Set WshTrg = ThisWorkbook.Worksheets("Target")
WshSrc.Cells.Copy
With WshTrg.Cells
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats
Application.CutCopyMode = False
End With
End Sub
Find below the full code to paste the format of one Worksheet named "Source", including Color, ColumnWidth, RowHeight, Comment, DataValidation, except the contents (Values, Formulas) of the cells to all other Worksheets in the same Workbook excluding a List of Worksheets as an Array
Option Explicit
Sub Wsh_PasteSpecial_Test()
Dim aWshExcluded As Variant, vWshExc As Variant
aWshExcluded = Array("Exclude(1)", "Exclude(2)")
Dim WshSrc As Worksheet
Dim WshTrg As Worksheet
Rem Set Source Worksheet
Set WshSrc = ThisWorkbook.Worksheets("Source")
Application.ScreenUpdating = 0
Rem Process All Worksheets
For Each WshTrg In WshSrc.Parent.Worksheets
Rem Exclude Worksheet Source
If WshTrg.Name <> WshSrc.Name Then
Rem Validate Worksheet vs Exclusion List
For Each vWshExc In aWshExcluded
If WshTrg.Name = vWshExc Then GoTo NEXT_WshTrg
Next
Rem Process Worksheet Target
With WshTrg.Cells
WshSrc.Cells.Copy
.PasteSpecial Paste:=xlPasteFormats 'Source format is pasted.
.PasteSpecial Paste:=xlPasteComments 'Comments are pasted.
.PasteSpecial Paste:=xlPasteValidation 'Validations are pasted.
Application.CutCopyMode = False
Application.Goto .Cells(1), 1
End With: End If:
NEXT_WshTrg:
Next
Application.Goto WshSrc.Cells(1), 1
Application.ScreenUpdating = 1
End Sub