I have a 2 Workbooks A and B. The following loop normally sits in workbook B and it takes a couple of seconds to run.
But when I try to run this function in Workbook B by having the code sit in Workbook A, it ends up taking over an hour.
Can someone explain why and is there maybe a better way for me to copy data?
The follow is what I want...
Workbook B is a database updated daily. I want code from Workbook A to search through Workbook B database ("SEC Sheet delay data") for order numbers associated with yesterdays date, find all other orders in the database with the same order number from previous dates before that, then copy and paste all selected orders onto the 'Timestamp' worksheet in Workbook B.
I am currently doing this with my code by highlighting yesterdays orders in the database red, then search database for any orders with the same values of those highlighted red, and highlight them red too.
Then copy and paste these selected orders onto 'Timestamp'.
If there is a better way than to copy and paste without highlighting as well, that would be greatly appreciated.
----------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------
TIA
But when I try to run this function in Workbook B by having the code sit in Workbook A, it ends up taking over an hour.
Can someone explain why and is there maybe a better way for me to copy data?
The follow is what I want...
Workbook B is a database updated daily. I want code from Workbook A to search through Workbook B database ("SEC Sheet delay data") for order numbers associated with yesterdays date, find all other orders in the database with the same order number from previous dates before that, then copy and paste all selected orders onto the 'Timestamp' worksheet in Workbook B.
I am currently doing this with my code by highlighting yesterdays orders in the database red, then search database for any orders with the same values of those highlighted red, and highlight them red too.
Then copy and paste these selected orders onto 'Timestamp'.
If there is a better way than to copy and paste without highlighting as well, that would be greatly appreciated.
----------------------------------------------------------------------------------------------------------------
VBA Code:
Sub updateData()
'Copy and Paste Highlighted
Dim TransIDField As Range
Dim TransIDCell As Range
Dim ATransWS As Worksheet
Dim HTransWS As Worksheet
With ThisWorkbook
Set ATransWS = .Worksheets("SEC Sheet delay data")
Set TransIDField = ATransWS.Range("C12000", ATransWS.Range("C12000").End(xlDown))
Set HTransWS = .Worksheets("Timestamp")
End With
ATransWS.Activate
'Highlight
Dim R As Range, WOCell As Range, datetoday As Range
Dim WO As String, Var As String
Set R = Range("C12000", Range("C12000").End(xlDown))
Set datetoday = Range("E12000", Range("E12000").End(xlDown))
Columns(3).Interior.Color = xlNone
For Each Cell In datetoday
If Cell.Value = Date - 1 Then 'Highlight all WO from yesterday
Cell.Offset(0, -2).Interior.Color = RGB(255, 0, 0)
Var = Cell.Offset(0, -2).Value
For Each WOCell In R 'Highlight all cells with same WO different dates
If InStr(WOCell.Value, Var) > 0 Then
WOCell.Interior.Color = RGB(255, 0, 0)
End If
Next WOCell
End If
Next
HTransWS.Range("A2:P" & Rows.Count).Clear
For Each TransIDCell In TransIDField
If TransIDCell.Interior.Color = RGB(255, 0, 0) Then
TransIDCell.Resize(1, 16).Copy Destination:= _
HTransWS.Range("A1").Offset(HTransWS.Rows.Count - 1, 0).End(xlUp).Offset(1, 0)
End If 'WHEN EXECUTING MACRO IN WORKBOOK B FROM WORKBOOK A, THE CODE TAKES AGES AT THIS 'END IF'
End Sub
------------------------------------------------------------------------------
TIA
Last edited by a moderator: