Move entire row to another sheet based on dynamic cell value

jiveass1960

New Member
Joined
Aug 20, 2019
Messages
19
Hello,

I'm trying to tweak a few different things to improve upon my current code. The current code works but seems extremely slow. Ultimately what it does is update a master sheet with shipping data when the temp sheet has a matching unique shipping ID on the master sheet.

Private Sub CommandButton1_Click()

Application.Interactive = False

Dim Cl As Range
Dim Dic As Object
Set Dic = CreateObject("scripting.dictionary")
With Sheets("Shipment")
For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
Dic.Item(Cl.Value) = Cl.Offset(, 1).Resize(, 5)
Next Cl
End With
With Sheets("Master")
For Each Cl In .Range("O2", .Range("O" & Rows.Count).End(xlUp))
If IsEmpty(Cl.Offset(, 2).Value) Then
Cl.Offset(, 2).Resize(, 5) = Dic.Item(Cl.Value)
End If
Next Cl
End With
Sheets("Shipment").Range("A2:A100").ClearContents
Sheet4.Activate
Application.Interactive = True
End Sub

This works but it takes quite a while (30-45 seconds) which isn't the worst thing in the world. Why I'm posting this is to find out if there is a better way of accomplishing this? I found this code which looked promising but it uses a static value "Done". I need this value to be dynamic as they matching would be based on the shipping ID's that are unique. The user who wrote this was trying to match two sheets(sheet1 & sheet2) and when there is a match, move the row to a new sheet (sheet3). Here is what I need to figure out. I need to compare sheet(Shipment), column A with sheet(Master) column O. When there is a match, copy sheet(Shipment), matching row (column B thru F) to sheet(Master), matching row (column Q thru U) and then clear the row that was copied from sheet(Shipment).


Sub Test()

Dim xRg As Range
Dim yRg As Range
Dim I As Long
Dim J As Long

I = Worksheets("Sheet1").UsedRange.Rows.Count
J = Worksheets("Sheet3").UsedRange.Rows.Count
secRow = Worksheets("Sheet2").UsedRange.Rows.Count
If J = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Sheet3").UsedRange) = 0 Then J = 0
End If

Set xRg = Worksheets("Sheet1").Range("B2:B100")
Set yRg = Worksheets("Sheet2").Range("C1:C" & secRow)

On Error Resume Next
Application.ScreenUpdating = False
Dim M As Long
Dim N As Long

For N = 1 To xRg.Count
For M = 1 To yRg.Count
If CStr(xRg(N).Value) = CStr(yRg(M).Value) Then
yRg(M).EntireRow.Copy Destination:=Worksheets("Sheet3").Range("A" & J + 1)
yRg(M).EntireRow.Delete
If CStr(xRg(N).Value) = CStr(yRg(M).Value) Then
N = N - 1
End If
J = J + 1
End If
Next

Next

Application.ScreenUpdating = True
End Sub
 
That shouldn't cause a problem, but you can try, at the start of the code
VBA Code:
   With Application
      .Interactive = False
      .Calculation = xlCalculationManual
      .ScreenUpdating = False
   End With
and at the end
VBA Code:
   With Application
      .Interactive = True
      .Calculation = xlCalculationAutomatic
   End With
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I'll give that a shot. Anything wrong with the little change i made to prevent the script from deleting existing entries when running? Originally after using it, I noticed that every time it ran it would update the new records but delete or overwrite any previous entries so I added the first and third row:

If IsEmpty(Cl.Offset(, 2).Value) Then
Cl.Offset(, 2).Resize(, 5) = Dic.Item(Cl.Value)
End If

There may be a better way of handling that than what I did.
 
Upvote 0
Genius! Seriously, it happens so quickly now, it isn't even noticeable. Before you would have the mouse icon as a spinning circle and in the bottom left corner, it would flash "Calculating" dozens and dozens of times. Now it is lightning quick! I'll post the final code below just in case you see something else that should be tweaked. I really appreciate your continued support on this. Thank you.

VBA Code:
Sub ProcessShipping()

With Application
    .Interactive = False
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
End With

Dim Cl As Range
Dim Dic As Object
  
Set Dic = CreateObject("scripting.dictionary")
With Sheets("Shipment")
    For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    Dic.Item(Cl.Value) = Cl.Offset(, 1).Resize(, 5)
    Next Cl
End With
   
With Sheets("Master")
    For Each Cl In .Range("O2", .Range("O" & Rows.Count).End(xlUp))
        If IsEmpty(Cl.Offset(, 2).Value) Then
        Cl.Offset(, 2).Resize(, 5) = Dic.Item(Cl.Value)
        End If
    Next Cl
End With

Sheets("Shipment").Range("A2:A100").ClearContents

With Application
    .Interactive = True
    .Calculation = xlCalculationAutomatic
End With

End Sub
 
Last edited by a moderator:
Upvote 0
I never set it back to true as there is no need, it happens automatically once the code has finished.
 
Upvote 0
My pleasure & thanks for the feedback.
 
Upvote 0
Hi Fluff. I had one more question regarding this. I wrote a vba prompt before this runs that tells the user how many rows will be updated. This is just a basic count function. I ran it today and had 60 records to update but only 48 actually updated. I only determined this by manually counting and checking. Is there anyway to return to the user how many were actually updated or better yet, the number that couldn't be due to not matching? I'm not sure what would be more beneficial or even possible but thought I would ask the genius.

Thanks again.
 
Upvote 0
Are you saying that some records where not updated when they should have been?
 
Upvote 0

Forum statistics

Threads
1,215,823
Messages
6,127,071
Members
449,358
Latest member
Snowinx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top