Macro Speed Improvement - vba

I have written a VBA program to clean up and sort many lines of data on to separate sheets. I am quite happy with it, and it's doing exactly what I need it to do. Unfortunately it can take up to ten minutes to run, as there is around 650,000 lines of data. While stepping through the macros individually, I have determined that the macros that actually search, cut then paste data onto a separate sheet are causing the problem. Does anyone have any tips on what I can do to improve this? I will leave a macro here for you guys to take a look at.
So you know, this is what she does in order: Delete unnecessary data, remove duplicates, sort into separate sheets, then count like addresses.
I have a "RUN" macro that a "Start" button is linked to, to call all macros in the necessary order. In this macro I disable calculation and screen updating, then enable when all macros have completed.
As I mentioned here is one of my sorting macros:
Sub CorpSheet() 'Moves corporate memberships to new sheet
Dim Check As Range, r As Long, lastrow2 As Long, LastRow As Long
'Application.ScreenUpdating = False
LastRow = Worksheets("PASTE DATA HERE").UsedRange.Rows.Count
lastrow2 = Worksheets("Corporate").UsedRange.Rows.Count
If lastrow2 = 1 Then lastrow2 = 0
For r = LastRow To 2 Step -1
If Range("E" & r).Value > 0 Then
Rows(r).Cut Destination:=Worksheets("Corporate") _
.Range("A" & lastrow2 + 1)
lastrow2 = lastrow2 + 1
Else:
End If
Next r
'Application.ScreenUpdating = True
End Sub
Any help that you can provide is appreciated!

Filter the sheet on ColE >0 - copy/paste the remaining rows to Corporate. Then delete the visible rows from the filtered table
Sub Faster()
Dim rngSrc As Range
Set rngSrc = Sheet1.Range("a1").CurrentRegion
rngSrc.AutoFilter Field:=5, Criteria1:=">0"
rngSrc.Copy Sheet2.Cells(Rows.Count, 1).End(xlUp)
rngSrc.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
rngSrc.AutoFilter
End Sub

One thing that would save a lot of time is only doing 1 copy. UNION the rows then copy them to the other sheet and delete them from the original in 1 step:
Sub CorpSheet() 'Moves corporate memberships to new sheet
Dim Check As Range, r As Long, lastrow2 As Long, LastRow As Long
Dim rng As Range
Application.ScreenUpdating = False
LastRow = Worksheets("PASTE DATA HERE").UsedRange.Rows.Count
lastrow2 = Worksheets("Corporate").UsedRange.Rows.Count
If lastrow2 = 1 Then lastrow2 = 0
For r = LastRow To 2 Step -1
If Range("E" & r).Value > 0 Then
If rng Is Nothing Then
Set rng = Rows(r)
Else
Set rng = Union(rng, Rows(r))
End If
End If
Next r
rng.Copy ThisWorkbook.Sheets("Corporate").Range("A" & lastrow2 + 1)
rng.Delete xlUp
Application.ScreenUpdating = True
End Sub
Then you can clean some things up a bit, fully qualify your ranges, and remove some other unnecessary code:
Sub CorpSheet() 'Moves corporate memberships to new sheet
Dim rng As Range
Dim rw As Range
Application.ScreenUpdating = False
For Each rw In Worksheets("PASTE DATA HERE").UsedRange
If rw.Range("E1").Value > 0 Then
If rng Is Nothing Then
Set rng = rw.EntireRow
Else
Set rng = Union(rng, rw.EntireRow)
End If
End If
Next r
rng.Copy ThisWorkbook.Sheets("Corporate").Range("A" & _
Worksheets("Corporate").UsedRange.Rows.Count + 1)
rng.Delete xlUp
Application.ScreenUpdating = True
End Sub

Related

Reverse Loop ignores some cells

I have written a small code that allow me to:
in a defined range (xrng) in column F, find all the cells that contain certain text and once found, select all the cells in the range A:G on the same row and delete them. I have a reverse loop, which work partially, as ignores some cells in the range, specifically the 2nd and the 3rd. Below a before and after pic:
Here my code:
Sub removeapp()
Dim g As Long, xrng As Range, lastrow As Long, i As Long
i = 4
lastrow = Cells(Rows.Count, "F").End(xlUp).Row
Set xrng = Range(Cells(lastrow, "F"), Cells(i, "F"))
For g = xrng.Count To i Step -1
If xrng.Cells(g).Value = "Adjustment" Or xrng.Cells(g).Value = "Approved" Then
Range(Cells(xrng.Cells(g).Row(), "A"), Cells(xrng.Cells(g).Row(), "G")).Delete
End If
Next
End Sub
Could you help me to figure out why?
Also, the code runs really slow... if you have any tip to make it slighlty faster would be great!
Try this, please:
Sub removeappOrig()
Dim xrng As Range, lastrow As Long, sh As Worksheet
Set sh = ActiveSheet 'good to put here your real sheet
lastrow = sh.Cells(sh.Rows.count, "F").End(xlUp).Row
Set xrng = sh.Range("A4:F" & lastrow)
xrng.AutoFilter field:=6, Criteria1:="=Adjustment", Operator:=xlOr, _
Criteria2:="=Approved", VisibleDropDown:=False
Application.DisplayAlerts = False
xrng.SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
sh.AutoFilterMode = False
End Sub
The next code is also fast enough since it iterates between array elements (in memory), not deletes row by row (it creates a ranges Union) and delete all at once:
Private Sub remoRangesAtOnce()
Dim i As Long, lastRow As Long, sh As Worksheet
Dim arrF As Variant, rng As Range, rngDel As Range
Set sh = ActiveSheet 'please name it according to your sheet name
lastRow = sh.Cells(sh.Rows.count, "F").End(xlUp).Row
Set rng = sh.Range("F4:F" & lastRow)
arrF = rng.Value
For i = LBound(arrF) To UBound(arrF)
If arrF(i, 1) = "Adjustment" Or arrF(i, 1) = "Approved" Then
If rngDel Is Nothing Then
Set rngDel = sh.Range(sh.Range("A" & i + 3), sh.Range("F" & i + 3))
Else
Set rngDel = Union(rngDel, sh.Range(sh.Range("A" & i + 3), sh.Range("F" & i + 3)))
End If
End If
Next i
If Not rngDel Is Nothing Then rngDel.Delete xlShiftUp
End Sub

Copying the matched row in another sheet

I have two Sheets, sheet1 and sheet 2.
I am looking into column T of sheet1 and pasting the complete row if T contains 1 in sheet 2.
The code, works good, but it paste the result in sheet2 in the same row in sheet1.
This results in blanks, between the rows. Can anyone suggest, what i should Change with my code, so that i get them in sequence without any blank rows.
Also, how can I copy the Header in row 1 from sheet 1 to sheet2?
Sub Test()
For Each Cell In Sheets(1).Range("T:T")
If Cell.Value = "1" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets(2).Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets(1).Select
End If
Next
End Sub
There's no need to use Select and Selection to copy paste, it will only slows down your code's run-time.
Option Explicit
Sub Test()
Dim Cell As Range
Dim NextRow as Long
Application.ScreenUpdating = False
For Each Cell In Sheets(1).Range("T1:T" & Sheets(1).Cells(Sheets(1).Rows.Count, "T").End(xlUp).Row)
If Cell.Value = "1" Then
NextRow = Sheets(2).Cells(Sheets(2).Rows.Count, "T").End(xlUp).Row
Rows(Cell.Row).Copy Destination:=Sheets(2).Range("A" & NextRow + 1)
End If
Next
Application.ScreenUpdating = True
End Sub
Not For Points
Apologies, but I couldn't stop myself from posting an answer. It pains me when I see someone wanting to use an inferior way of doing something :(
I am not in favor of looping. It is very slow as compared to Autofilter.
If you STILL want to use looping then you can make it faster by not copying the rows in the loop but in the end in ONE GO...
Also if you do not like living dangerously then always fully qualify your object else you may end up copying the wrong row.
Option Explicit
Sub Sample()
Dim wsI As Worksheet, wsO As Worksheet
Dim lRow As Long, i As Long, r As Long
Dim copyRng As Range
Set wsI = Sheet1: Set wsO = Sheet2
wsO.Cells.Clear
'~~> first available row in sheet2
r = 2
With wsI
lRow = .Range("T" & .Rows.Count).End(xlUp).Row
'~~> Copy Headers
.Rows(1).Copy wsO.Rows(1)
For i = 1 To lRow
If .Range("T" & i).Value = 1 Then
If copyRng Is Nothing Then
Set copyRng = .Rows(i)
Else
Set copyRng = Union(copyRng, .Rows(i))
End If
End If
Next i
End With
If Not copyRng Is Nothing Then copyRng.Copy wsO.Rows(r)
End Sub
Screenshot

Copy columns from multiple spreadsheets. Data moving up when column empty on spreadsheet

I have the below code. The code will go into each of the 17 workbooks and extract certain columns based on the columns headers name. This will repeat and add to the bottom of the master workbook, until the last one has been extracted.
Unfortunately, if there is nothing in one of the columns on one of the individual 17 workbooks, the data from the next workbook gets moved up in the cells. Is there anyway to sort this. I have added the code below.
Option Explicit
Sub CopyColumns()
Dim CopyFromPath As String, FileName As String
Dim CopyToWb As Workbook, wb As Workbook, CopyToWs As Worksheet
Dim lastRow As Long, NextRow As Long, lcol As Long, c As Long, sv As Integer
Dim ws As Worksheet
Dim myCol As Long
Dim myHeader As Range
r\"
Set CopyToWb = ActiveWorkbook
Set c).End(xlUp).Row
If lastRow = 1 Then GoTo nxt
Range(Cells(2, c), Cells(lastRow, c)).Copy
CopyToWs.Activate
Set myHeader = CopyToWs.Rows(1).Find(What:=.Cells(1, c).Value, Lookat:=xlWhole)
With CopyToWs
If Not myHeader Is Nothing Then
myCol = myHeader.Column
NextRow = .Cells(Rows.Count, myCol).End(xlUp).Row + 1
.Cells(NextRow, myCol).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Set myHeader = Nothing
End If
nxt:
End With
End If
Next c
wb.Close saveChanges:=False
End With
FileName = Dir
Loop
Application.ScreenUpdating = True
End Sub
Thank you in advance
Calculate NextRow only once per workbook, and then use it for all columns:
Do While Len(FileName) > 0
'Calculate the next row to be populated for all columns, based on the last
'used cell in column A
'(I used column A, but pick whatever destination column will always be
'populated in every workbook.)
With CopyToWs
NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
'Process this workbook
Set wb = Workbooks.Open(CopyFromPath & FileName)
With wb.Sheets("Open Issue Actions")
lcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For c = 1 To lcol
'...
With CopyToWs
If Not myHeader Is Nothing Then
myCol = myHeader.Column
'NextRow = .Cells(Rows.Count, myCol).End(xlUp).Row + 1
.Cells(NextRow, myCol).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Set myHeader = Nothing
End If
End With
nxt:
'...
Actually you want one row per sheet. Nothing else. Nothing more. You do not even need to calculate it. You need to increment it lngRow = lngRow+1.
Try to use the following into your code:
Option Explicit
Sub CopyColumns()
Dim lngRow As Long: lngRow = 1
Do While Len(FileName) > 0
Set wb = Workbooks.Open(CopyFromPath & FileName)
With wb.Sheets("Open Issue Actions")
lngRow = lngRow + 1
With CopyToWs
If Not myHeader Is Nothing Then
myCol = myHeader.Column
.Cells(lngRow, myCol).PasteSpecial xlPasteValues
Set myHeader = Nothing
End If
End With
End With
wb.Close saveChanges:=False
Loop
Application.ScreenUpdating = True
End Sub
In the code you add/edit three things:
The line Dim lngRow as Long: lngRow=1 on top with the other Dim
lngRow = lngRow + 1 after the With wb.Sheets("Open Issue Actions")
the paste values should be like this .Cells(lngRow, myCol).PasteSpecial xlPasteValues
The whole code is here: https://pastebin.com/kXdzkGZ1
The idea is to have lngRow and to increment it for every WorkSheet that you open. And do not do anything else with it.
In general, your code can be optimized in some ways, if it works ok after the change, put it here for further ideas: https://codereview.stackexchange.com/

Excel VBA - Merge rows until last row

I'm trying to make a macro that will scroll through a spreadsheet an entire row at a time and merge all cells in the active row if they have data. It should do this until the last row.
The code currently sees all rows as empty and therefor skips them, I need an if condition or do until statement that will help detect and skip empty rows, detect rows with data and merge their cells and stop entirely when it reaches the last row.
My current code:
Sub merge()
Dim LastRow As Long, i As Long
Sheets("Body").Activate
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Rows("1:1").Select
For i = 1 To LastRow
If Range("A" & i).Value = "*" Then
Selection.merge = True
Selection.Offset(1).Select
Else
Selection.Offset(1).Select
End If
Next i
End Sub
I have also tried:
sub merge2()
Dim LastRow As Long, i As Long
Sheets("Body").Activate
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Rows("1:1").Select
Do Until ActiveCell.EntireRow > LastRow
'this line below was a concept
If ActiveCell.EntireRow & ActiveCell.Column.Value = "*" Then
Selection.merge = True
Selection.Offset(1).Select
Else
Selection.Offset(1).Select
End If
Loop
End Sub
This is untested but should do what you want.
Option Explicit
Sub merge()
Dim ws As Worksheet
Dim LastRow As Integer, i As Integer
Set ws = ThisWorkbook.Sheets("Body")
ws.Activate
With ws
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
For i = 1 To LastRow
If Not IsEmpty(Range("A" & i)) And ws.Cells(i, Columns.Count).End(xlToLeft).Column > 1 Then
ws.Rows(i & ":" & i).merge
End If
Next i
End Sub
This If will test for a) whether the cell in column A is empty and b) whether there are any other cells in that row. if statement a evaluates to false AND statement b is greater than 1 it will execute the If statement
#Tom I've taken your code and added in an error handler that makes it work without fault, thank you very much for your patience, you've been a fantastic help.
Sub merge2()
Dim ws As Worksheet
Dim LastRow As Integer, i As Integer
Set ws = ThisWorkbook.Sheets("Body")
ws.Activate
With ws
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
For i = 1 To LastRow
If Not IsEmpty(Range("A" & i)) And ws.Cells(i, Columns.Count).End(xlToLeft).Column >= 1 Then
On Error Resume Next
ws.Rows(i & ":" & i).merge = True
End If
Next i
End Sub

I need to create a vba script or macro that can transpose and format data all at once

I have found the code
Sub Test()
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = Cells.Find("*", SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row
Dim lColumn As Long
Dim x As Long
Dim rng As Range
For Each rng In Range("A1:A" & LastRow)
lColumn = Cells(rng.Row, Columns.Count).End(xlToLeft).Column
For x = 1 To lColumn - 2
Range(Cells(rng.Row, "A"), Cells(rng.Row, "B")).Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
Sheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = rng.Offset(0, x + 1)
Next x
Next rng
Application.ScreenUpdating = True
End Sub
I am trying to modify it to suit my needs but it isn't quite doing what I need it to do.
Basically, my table is like this:
A B C D
FILENAME ID FIELD1 FIELD2
1 2 3 4
and I want it to look like this:
A FILENAME 1
B ID 2
C FIELD1 3
D FIELD2 4
however, sometimes there may be more columns or rows associated with a given part of the range that is related to a set of data. right now the columns that
I don't know nearly enough about excel and vba to modify this code to do that, but it would be nice if I could.
below are a couple of links that explain closely how I want the final table to look.
http://pastebin.com/1i5MqTL7
http://imgur.com/a/PKAcy
The ID's are not unique product pointers, but that's the REAL world. Different considerations and assumptions about the consistency of your input data, but try this:
Private Sub TransposeBits()
Application.ScreenUpdating = False
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet2")
'start will be the starting row of each set
Dim start As Long
start = 2
'finish will be the last row of each set
Dim finish As Long
finish = start
Dim lastRow As Long
lastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'printRow will keep track of where to paste-transpose each set
Dim printRow As Long
printRow = lastRow + 2
'lastCol will measure the column count of each set
Dim lastCol As Long
'Just dealing with a single entry here - delete as necessary
If lastRow < 3 Then
lastCol = Cells(start, 1).End(xlToRight).Column
With ws
.Range(.Cells(start, 1), .Cells(finish, lastCol)).Copy
.Cells(printRow, 1).PasteSpecial Transpose:=True
End With
Application.ScreenUpdating = True
'in the trivial case, we can exit the sub after dealing with the one-line transpose
Exit Sub
End If
'more general case
For i = 3 To lastRow
If Not Range("A" & i).Value = Range("A" & i - 1).Value Then
'the value is different than above, so set the finish to the above row
finish = i - 1
lastCol = Cells(start, 1).End(xlToRight).Column
'copy the range from start row to finish row and paste-transpose
With ws
.Range(.Cells(start, 1), .Cells(finish, lastCol)).Copy
.Cells(printRow, 1).PasteSpecial Transpose:=True
End With
'after finding the end of a set, reset the start and printRow variable
start = i
printRow = printRow + lastCol
End If
Next i
'here we deal with the last set after running through the loop
finish = lastRow
lastCol = Cells(start, 1).End(xlToRight).Column
With ws
.Range(.Cells(start, 1), .Cells(finish, lastCol)).Copy
.Cells(printRow, 1).PasteSpecial Transpose:=True
End With
Application.ScreenUpdating = True
End Sub
You can use the Paste Special that #Jeeped uses - just write it in code:
Sub TransposeData()
Dim rLastCell As Range
With ThisWorkbook.Worksheets("Sheet1")
'NB: If the sheet is empty this will throw an error.
Set rLastCell = .Cells.Find("*", SearchDirection:=xlPrevious)
'Copy everything from A1 to the last cell.
.Range(.Cells(1, 1), rLastCell).Copy
'Paste/Transpose in column A, one row below last row containing data.
.Cells(rLastCell.Row + 1, 1).PasteSpecial Transpose:=True
End With
End Sub