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
Related
I am still new with VBA but decided using VBA to streamline a Monthly report I am working on for our payroll would be the most efficient way, I wish to export a range I3:U2270 into a new sheet named from a cell in the summary sheet. Everything works fine except formatting.
If I use xlPasteAll the formatting is perfect, but it shows values as formulas.
If I use:
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteFormats
The data and cell widths are perfect, but because some data is in Tables, it doesn't bring the formatting across.
Any ideas would be appreciated!
Sub MonthlySummaryExport()
Dim NewSheetName As String
Dim Newsheet As Object
On Error Resume Next
NewSheetName = Worksheets("Monthly Summary").Range("T1")
If NewSheetName = "" Then Exit Sub
Set Newsheet = Sheets(NewSheetName)
If Not Newsheet Is Nothing Then
MsgBox "Sheet cannot be created as there is already a worksheet with the same name" & " " & (NewSheetName)
Exit Sub
End If
Sheets.Add(, Sheets(Sheets.Count)).Name = NewSheetName
'Copy and PasteSpecial a between worksheets
Worksheets("Monthly Summary").Range("I3:U2270").Copy
With Worksheets(NewSheetName).Range("A1")
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteFormats
End With
'Disable marching ants around copied range
Application.CutCopyMode = False
End Sub
Copy everything including formulas, then change formulas to values in the copy
'Copy and PasteSpecial a between worksheets
dim dest as range
set dest = Worksheets(NewSheetName).Range("A1")
Worksheets("Monthly Summary").Range("I3:U2270").Copy dest
dest.currentregion.copy
dest.pastespecial xlPasteValues
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
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
As the title I tried this one, but it overwrites existing data, I am looking for something that
add the header row in all sheets moving the data down. I have got 50 sheets that's why I am asking :-)
Sub CopyToAllSheets()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("sheet1")
Sheets.FillAcrossSheets ws.Range("1:1")
End Sub
Thank you in advance
You could insert a line in each sheet before filling the headers:
Sub CopyToAllSheets()
Dim sheet As Worksheet
For Each sheet In Sheets
sheet.Rows("1:1").Insert Shift:=xlDown
Next sheet
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("sheet1")
Sheets.FillAcrossSheets ws.Range("1:1")
End Sub
I will be assuming that your header will be coming from another sheet. Recording a macro gives me:
Sub Macro4()
Sheets("Sheet1").Select
Rows("1:1").Select
Selection.Copy
Sheets("Sheet2").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
End Sub
Cleaning it up gives:
Sub InsertOnTop()
Sheets("Sheet1").Rows("1:1").Copy
Sheets("Sheet2").Rows("1:1").Insert Shift:=xlDown
Application.CutCopyMode = False
End Sub
Applying it safely across all sheets except the source sheet:
Sub InsertOnTopOfEachSheet()
Dim WS As Worksheet, Source As Worksheet
Set Source = ThisWorkbook.Sheets("Sheet1") 'Modify to suit.
Application.ScreenUpdating = False
For Each WS In ThisWorkbook.Worksheets
If WS.Name <> Source.Name Then
Source.Rows("1:1").Copy
WS.Rows("1:1").Insert Shift:=xlDown
End If
Next WS
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Let us know if this helps.
I believe you will need to loop through each sheet and insert a blank row, or just use the range.insert method on each sheet. Perhaps something like:
Option Explicit
Sub CopyToAllSheets()
Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
If Not WS.Name = "Sheet1" Then
If WorksheetFunction.CountA(WS.Rows(1)) > 0 Then _
WS.Rows(1).Insert
End If
Next WS
Worksheets.FillAcrossSheets Worksheets("Sheet1").Rows(1), xlFillWithAll
End Sub
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