Excel vba code cleanup - vba

The code below works but isn't fast and I'm sure there are ways it could simplified. I'm not a coder -- I just compiled a few samples that I found. I really don't understand the range/end/offset pieces for the pasting aspect. Here's what I'm trying to do:
1. Print the first three worksheets in the workbook
2. Create three new worksheets at the end of the workbook
3. copy and paste values, formats, and column widths to the three new worksheets from the first three.
Thanks for any help you can provide!
Option Explicit
Option Base 1
Sub Print_copy_Current_Workbook()
'Prints the current active workbook in Excel
Sheets("Draw").PrintOut
Sheets("Calculations").PrintOut
Sheets("AIN").PrintOut
Application.ScreenUpdating = False
Dim Tabs As Variant
Dim I As Byte
Tabs = Array("Draw Final", "AIN Final", "Calculations Final")
For I = LBound(Tabs) To UBound(Tabs)
Sheets.Add(After:=Sheets(Worksheets.Count), Count:=1).Name = Tabs(I)
Next I
Sheets("Draw").Range("A1:L1000").Copy
With Sheets("Draw Final").Range("iv1").End(xlToLeft).Offset(, 1)
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
End With
Sheets("AIN").Range("A1:L1000").Copy
With Sheets("AIN Final").Range("iv1").End(xlToLeft).Offset(, 1)
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
End With
Sheets("Calculations").Range("A1:L1000").Copy
With Sheets("Calculations Final").Range("iv1").End(xlToLeft).Offset(, 1)
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

Suggestion below.
Also I would avoid Option Base 1 - it's rarely used, and will just cause problems later when you get used to working with zero-based arrays.
Sub Print_copy_Current_Workbook()
Dim Tabs As Variant
Dim I As Long
Application.ScreenUpdating = False
Tabs = Array("Draw", "AIN", "Calculations")
For I = LBound(Tabs) To UBound(Tabs)
Sheets(Tabs(I)).PrintOut
Sheets.Add(After:=Sheets(Worksheets.Count)).Name = Tabs(I) & " Final"
CopyPaste Sheets(Tabs(I)).Range("A1:L1000")
Next I
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Sub CopyPaste(rng As Range)
rng.Copy
'this is a new sheet we're pasting to, so why not just Range("A1") ?
With Sheets(rng.Parent.Name & " Final").Range("iv1").End(xlToLeft).Offset(, 1)
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
End With
End Sub

Related

Copy and PasteSpecial Table Values and Formatting

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

Copy a range with multiple non-adjacent cells to another sheet on the same cells

I wrote the code below which works fine except that it takes forever and looks as if Excel is having an epileptic seizure.
Any help with something less prehistoric would be much appreciated.
Sub Data()
Sheets("2").Unprotect "Joe"
Worksheets("3").Range("a").Copy
Worksheets("2").Range("D10").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("b").Copy
Worksheets("2").Range("L10").PasteSpecial Paste:=xlPasteValues
Worksheets("2").Range("L18").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("c").Copy
Worksheets("2").Range("D11").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("d").Copy
Worksheets("2").Range("L11").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("e").Copy
Worksheets("2").Range("D17").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("f").Copy
Worksheets("2").Range("L17").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("g").Copy
Worksheets("2").Range("D18").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("h").Copy
Worksheets("2").Range("D19").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("i").Copy
Worksheets("2").Range("L19").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("j").Copy
Worksheets("2").Range("D20").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("k").Copy
Worksheets("2").Range("E22").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("l").Copy
Worksheets("2").Range("E23").PasteSpecial Paste:=xlPasteValues
Worksheets("3").Range("m").Copy
Worksheets("2").Range("E24").PasteSpecial Paste:=xlPasteValues
End Sub
Sub Data()
Dim rng As Range
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
Sheets("2").Unprotect "Joe"
With Worksheets("2")
Set rng = Range("a")
.Range("D10").Resize(rng.Rows.Count, rng.Columns.Count).Value2 = rng.Value2
Set rng = Range("b")
.Range("L10").Resize(rng.Rows.Count, rng.Columns.Count).Value2 = rng.Value2
'...and so on
End With
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
Note that because you're using range names for a, b, c then there is no need to qualify them with a sheetname unless they are locally scoped.
Instead of copy-pasting, you could also set the value of your destination cell equal to the value of your original cell. For example:
Worksheets("2").Range("D10").Value = Worksheets("3").Range("a").Value
To prevent the seizure like behavior in the application window of Excel, do as Excelosaurus suggested and turn off screen updating at the start of your macro. (And make sure to turn it back on at the end).
Put Application.ScreenUpdating = False at the beginning of your sub, and Application.ScreenUpdating = True at the end.

Paste special method of range class failed vba

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

Macro VBA formatting

Hello I have the following MACRO...
Sub RunThis()
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
ThisWorkbook.Sheets("Conditions").Range("A27:H54").Copy
With ThisWorkbook.Sheets("Project")
.Range("A1").PasteSpecial xlPasteValues
.Range("A1").PasteSpecial xlPasteFormats
End With
ThisWorkbook.Sheets("Conditions").Range("A56:H88").Copy
With ThisWorkbook.Sheets("Project")
.Range("A29").PasteSpecial xlPasteValues
.Range("A29").PasteSpecial xlPasteFormats
End With
Call DeleteCellsWithNo
End With
**With ThisWorkbook.Sheets("Project")
.Cells("A, 1").Select
End With**
End Sub
What I'm trying to do is Make is select the Cell A1 - also, I'm trying to format the width of cell A1 to autowidth....Any idea?
EDIT: the two stars next to WITH is what i'm trying to do but it gives me an error
EDIT2:
With ThisWorkbook.Sheets("Project")
.Cells(1, "A").Select
.Cells(1, "A").Width = 50
End With
I have this but gives me errors on the WIDTH part. I guess I want to set the width because the values are of different length so 50 will work for all
I might misunderstand your point, but if you want to autowidth your cell (your column) this works:
ThisWorkbook.Sheets("Project").Columns("A:A").AutoFit

Conditionally move rows into another worksheet

I'm hoping someone can help me with this. I have a spreadsheet with 2 sheets one called Details and another called Reconciled. I have 1000+ rows in Details and I want to cut all rows that have 0 or a - in column E (I want to cut the entire row) and paste it into sheet Details. If possible I would like to copy and paste the headers from Reconciled into Details as well.
I've tried using this code (modified slightly) used in another post
Sub Test()
For Each Cell In Sheets("Details").Range("E:E")
If Cell.Value = "0" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("Reconcile").Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets("Details").Select
End If
Next
End Sub
But there are 2 problems. Because some values - (numbers are truly) those get moved, but the ones that are 0.00 do not get moved because they are rounded (I think that's why they are not being moved). Also, the screen updates oddly, and I'm sorry I can't explain it more than.
Any help would be appreciated
Sub Test()
Application.ScreenUpdating = False
On Error Goto Finish
For Each Cell In Sheets("Details").Range("E:E")
If Cell.Value = 0 Or Cell.Value = "-" Then cell.EntireRow.copy Sheets("Reconcile").Rows(cell.Row)
Next
Finish:
Application.ScreenUpdating = True
End Sub
Notice: dont put quotes around the 0, this will make numeric comparison
Using Autofilter:
Public Sub Test()
Application.ScreenUpdating = False
With Worksheets("Details").UsedRange
.Parent.AutoFilterMode = False
.AutoFilter
.AutoFilter Field:=5, Criteria1:="0"
.Copy
With Worksheets("Reconciled").Cells(1, 1)
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteAll
.Parent.Activate: .Select
End With
.Rows(1).Hidden = True
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
.Rows(1).Hidden = False
.AutoFilter
.Parent.Activate
.Cells(1, 1).Activate
End With
Application.ScreenUpdating = True
End Sub