Hi Everyone,
I'm relatively new to using VBA so my technique at the moment is to amend code I find online to suit my needs. I know that this is not an efficient way of doing things and I suspect that I could do the same job in less time (processing time) if I used more efficient VBA. If anyone is willing to help I would be interested if there are some pointers you could give me to make the following more efficient, it would help me in the future too!
Quick explanation of what I am trying to achieve
I take a data dump of survey responses from one of my systems at work, I use the following code to organise and separate satisfied from dissatisfied responses.Each has its own tab in excel. The code bellow is 100% working as I want it but I am sure it could be improved.
There are 3 tabs
Remedy Export, Satisfied Archive, Dissatisfied Archive
The data goes into a table on Rem' Export and is sorted into the other 2 tabs accordingly.
Sub SortingButton_Click()
'*** Turn Screen Updateing Off ***
Application.ScreenUpdating = False
'*** Stop Errors ***
On Error Resume Next
'*** Delete Duplicates On Remedy Export ***
'*** NOT YET COMPLETE ***
'*** Sorting Dissatisfied from Satisfied ***
Dim lr As Long
Dim lr2 As Long
Dim r As Long
lr = Sheets("Remedy Export").Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Sheets("Satisfied Archive").Cells(Rows.Count, "A").End(xlUp).Row
lr3 = Sheets("Dissatisfied Archive").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
If Range("G" & r).Value = "Dissatisfied " Then
Rows(r).Cut Destination:=Sheets("Dissatisfied Archive").Range("A" & lr2 + 1)
lr2 = Sheets("Dissatisfied Archive").Cells(Rows.Count, "A").End(xlUp).Row
End If
If Range("G" & r).Value = "Satisfied " Then
Rows(r).Cut Destination:=Sheets("Satisfied Archive").Range("A" & lr3 + 1)
lr3 = Sheets("Satisfied Archive").Cells(Rows.Count, "A").End(xlUp).Row
End If
Range("A1").Select
Next r
'*** Resize Satisfied and Dissatisfied Tables ***
Dim StopLeft As String
Dim SrightCol1 As String
Dim SrowCell As String
StopLeft = "$A$5"
SrightCol1 = "M"
SrowCell = "$A$2"
With Sheets("Satisfied Archive")
.ListObjects("Table14").Resize .Range(StopLeft & ":$" & SrightCol1 & "$" & .Range(SrowCell).Value + .Range(StopLeft).Row)
End With
With Sheets("Dissatisfied Archive")
.ListObjects("Table15").Resize .Range(StopLeft & ":$" & SrightCol1 & "$" & .Range(SrowCell).Value + .Range(StopLeft).Row)
End With
'*** Delete Blank Table Rows in Satisfied Archive Tables ***
Dim Srng As Range
Dim Si As Long
Set Srng = ThisWorkbook.Sheets("Satisfied Archive").Range("A1:A10000")
With Srng
For Si = .Rows.Count To 1 Step -1
If .Item(Si) = "" Then
.Item(Si).EntireRow.Delete
End If
Next Si
End With
'*** Delete Blank Table Rows in Dissatisfied Archive Tables ***
Dim Drng As Range
Dim Di As Long
Set Drng = ThisWorkbook.Sheets("Dissatisfied Archive").Range("A1:A10000")
With Drng
For Di = .Rows.Count To 1 Step -1
If .Item(Di) = "" Then
.Item(Di).EntireRow.Delete
End If
Next Di
End With
'*** Delete Blank Table Rows in Remedy Export Tables ***
Dim Rrng As Range
Dim Ri As Long
Set Rrng = ThisWorkbook.Sheets("Remedy Export").Range("A1:A10000")
With Rrng
For Ri = .Rows.Count To 1 Step -1
If .Item(Ri) = "" Then
.Item(Ri).EntireRow.Delete
End If
Next Ri
End With
'*** Turn Screen Updateing On ***
Application.ScreenUpdating = True
End Sub
I'm relatively new to using VBA so my technique at the moment is to amend code I find online to suit my needs. I know that this is not an efficient way of doing things and I suspect that I could do the same job in less time (processing time) if I used more efficient VBA. If anyone is willing to help I would be interested if there are some pointers you could give me to make the following more efficient, it would help me in the future too!
Quick explanation of what I am trying to achieve
I take a data dump of survey responses from one of my systems at work, I use the following code to organise and separate satisfied from dissatisfied responses.Each has its own tab in excel. The code bellow is 100% working as I want it but I am sure it could be improved.
There are 3 tabs
Remedy Export, Satisfied Archive, Dissatisfied Archive
The data goes into a table on Rem' Export and is sorted into the other 2 tabs accordingly.
Sub SortingButton_Click()
'*** Turn Screen Updateing Off ***
Application.ScreenUpdating = False
'*** Stop Errors ***
On Error Resume Next
'*** Delete Duplicates On Remedy Export ***
'*** NOT YET COMPLETE ***
'*** Sorting Dissatisfied from Satisfied ***
Dim lr As Long
Dim lr2 As Long
Dim r As Long
lr = Sheets("Remedy Export").Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Sheets("Satisfied Archive").Cells(Rows.Count, "A").End(xlUp).Row
lr3 = Sheets("Dissatisfied Archive").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
If Range("G" & r).Value = "Dissatisfied " Then
Rows(r).Cut Destination:=Sheets("Dissatisfied Archive").Range("A" & lr2 + 1)
lr2 = Sheets("Dissatisfied Archive").Cells(Rows.Count, "A").End(xlUp).Row
End If
If Range("G" & r).Value = "Satisfied " Then
Rows(r).Cut Destination:=Sheets("Satisfied Archive").Range("A" & lr3 + 1)
lr3 = Sheets("Satisfied Archive").Cells(Rows.Count, "A").End(xlUp).Row
End If
Range("A1").Select
Next r
'*** Resize Satisfied and Dissatisfied Tables ***
Dim StopLeft As String
Dim SrightCol1 As String
Dim SrowCell As String
StopLeft = "$A$5"
SrightCol1 = "M"
SrowCell = "$A$2"
With Sheets("Satisfied Archive")
.ListObjects("Table14").Resize .Range(StopLeft & ":$" & SrightCol1 & "$" & .Range(SrowCell).Value + .Range(StopLeft).Row)
End With
With Sheets("Dissatisfied Archive")
.ListObjects("Table15").Resize .Range(StopLeft & ":$" & SrightCol1 & "$" & .Range(SrowCell).Value + .Range(StopLeft).Row)
End With
'*** Delete Blank Table Rows in Satisfied Archive Tables ***
Dim Srng As Range
Dim Si As Long
Set Srng = ThisWorkbook.Sheets("Satisfied Archive").Range("A1:A10000")
With Srng
For Si = .Rows.Count To 1 Step -1
If .Item(Si) = "" Then
.Item(Si).EntireRow.Delete
End If
Next Si
End With
'*** Delete Blank Table Rows in Dissatisfied Archive Tables ***
Dim Drng As Range
Dim Di As Long
Set Drng = ThisWorkbook.Sheets("Dissatisfied Archive").Range("A1:A10000")
With Drng
For Di = .Rows.Count To 1 Step -1
If .Item(Di) = "" Then
.Item(Di).EntireRow.Delete
End If
Next Di
End With
'*** Delete Blank Table Rows in Remedy Export Tables ***
Dim Rrng As Range
Dim Ri As Long
Set Rrng = ThisWorkbook.Sheets("Remedy Export").Range("A1:A10000")
With Rrng
For Ri = .Rows.Count To 1 Step -1
If .Item(Ri) = "" Then
.Item(Ri).EntireRow.Delete
End If
Next Ri
End With
'*** Turn Screen Updateing On ***
Application.ScreenUpdating = True
End Sub