Sub MakeTables()
Dim wbTarget As Object
Set wb = Workbooks.Open("C:\Users\A9900899\Desktop\Desmond\VBAProject\GenerateTablesFormulas.xlsx")
Set wbTarget = Workbooks.Open("C:\Users\A9900899\Desktop\Desmond\VBAProject\USDReport.xlsx")
With wb.Sheets("Sheet1").UsedRange
.Copy
' Create the new sheet and name it at the end
With wbTarget.Sheets("HK").Range("D82:X97")
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
End With
End Sub
Can someone tell me what mistake I made here. It gives me the error that range class failed. Thank you
if you want to keep the copied range size, then:
With wb.Sheets("Sheet1").UsedRange
.Copy
' Create the new sheet and name it at the end
With wbTarget.Sheets("HK").Range("D82") '.Resize(.Rows.Count, .Columns.Count) '<--| you can omit the 'Resize' part but it can be useful to make it clear (code can be read in years to come) you want to stick to copied range size
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
End With
Application.CutCopyMode = False '<--| empty clipboard
otherwise if you want to force the copied range size to the to-be-pasted one, then:
' Create the new sheet and name it at the end
With wbTarget.Sheets("HK").Range("D82:X97") '<--| reference range you want to paste values to and keep sizes of
wb.Sheets("Sheet1").UsedRange.Resize(.Rows.Count, .Columns.Count).Copy '<--| copy the source sheet range with same sizes as referenced range
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
Application.CutCopyMode = False '<--| empty clipboard
Related
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 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 a long column of words, need to copy and paste the first 30 or so into a column on another sheet, then paste the next 30 in the following column, etc.
I recorded the start of a macro, but have no idea how to make it do the whole thing without writing each bit out individually.
Sub asdasd()
'
Range("A2:A29").Select
Selection.Copy
Sheets("COMMON WORDS").Select
Range("AG2").Select
ActiveSheet.Paste
Sheets("COMMON - SINGLE LIST").Select
Range("A30").Select
Range("A30,A57").Select
Range("A57").Activate
Range("A30:A57").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("COMMON WORDS").Select
Range("AH2").Select
ActiveSheet.Paste
End Sub
Sub LoopCopy()
Dim rngCopy As Range, rngPaste As Range
Set rngCopy = Sheets("COMMON - SINGLE LIST").Range("A2:A29")
Set rngPaste = Sheets("COMMON WORDS").Range("AG2")
'copy while there's data in rngCopy...
Do while application.counta(rngCopy) > 0
rngCopy.copy rngPaste
set rngCopy = rngCopy.offset(rngCopy.rows.count, 0) '<< move copy range
set rngPaste = rngPaste.offset(0, 1) '<< move paste postion over
Loop
End Sub
Sub CopyPaste()
'
' CopyPaste Macro
'
' Keyboard Shortcut: Ctrl+Shift+P
'
Range("A2:C5").Select
Selection.Copy
Sheets("A").Select
Range("A2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("A6:C11").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("B").Select
Range("A2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("A12:C17").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("C").Select
Range("A2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("A18:C21").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("D").Select
Range("A2").Select
ActiveSheet.Paste
End Sub
I have trying making a Macro to do a basic task but I can't seem to figure it out, can anyone help please! I'm trying to create a macro that will copy data from one worksheet and place into another worksheet based on specific letter.
For example all "A" item will paste automatically into new worksheet name "A". This I can do with no problem. But, when I want to use the same macro with another row with different no of column is where I have my problem.
I already use recorded macro and then if the row from copy worksheet have been reduced, it will paste wrongly in new worksheet.
Is there any way to solve it?
thanks in advance.
P/S--> the new worksheet will have header in it. so it would be nice if they can paste start from A2 row. Can refer image below for example.
See Example / and see comment on the code
Option Explicit
Public Sub Example()
'Declare your Variables
Dim Sht As Worksheet
Dim rng As Range
Dim List As Collection
Dim varValue As Variant
Dim i As Long
With ThisWorkbook
'Set your Sheet name
Set Sht = ActiveWorkbook.Sheets("Sheet1")
'set your auto-filter, A1
With Sht.Range("A1")
.AutoFilter
End With
'Set your agent Column range # (1) that you want to filter it
Set rng = Range(Sht.AutoFilter.Range.Columns(1).Address)
'Create a new Collection Object
Set List = New Collection
'Fill Collection with Unique Values
On Error Resume Next
For i = 2 To rng.Rows.Count
List.Add rng.Cells(i, 1), CStr(rng.Cells(i, 1))
Next i
'Start looping in through the collection Values
For Each varValue In List
'Filter the Autofilter to macth the current Value
rng.AutoFilter Field:=1, Criteria1:=varValue
'Copy the AutoFiltered Range to new Workbook
Sht.AutoFilter.Range.Copy
Worksheets.Add.Paste
ActiveSheet.Name = Left(varValue, 30)
Next ' Loop back
'Go back to main Sheet and removed filters
Sht.AutoFilter.ShowAllData
Sht.Activate
End With
End Sub
Make sure to have header on your data, see below
I'm trying to copy and paste the values form another workbook with the specific sheet named Sheet1. But when I run this code:
Dim wb as Workbook
Dim conso as Worksheet
set conso = wb.Worksheets("Sheet1")
With conso
.Rows(1).EntireRow.Copy
End With
With ActiveSheet
'.Paste
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
End With
I got an error saying:
PasteSpecial method of Worksheet class failed.
I'm trying to copy all the entire row of the Sheet1 sheet to another workbook. Any help?
Try indicating where you need to paste the cells you copied:
With ActiveSheet.Rows(1)
'.Paste
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
End With