Slow update Macro

NiiBoi_Gifted

New Member
Joined
Jun 23, 2024
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Can you please help with this macro? It is an update macro but any time I run it, it takes a long time to update

Sub UpdateAmountBasedOnACCT()
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Dim primaryKey As String
Dim lookupRange As Range
Dim cell As Range
Dim FoundCell As Range

' Set the worksheets
Set wsSource = ThisWorkbook.Sheets("Search")
Set wsTarget = ThisWorkbook.Sheets("DATA")

' Define the lookup range in DATA
Set lookupRange = wsTarget.Range("B:N") ' Adjust the range as necessary

' Loop through each cell in Search where ACCT are located
For Each cell In wsTarget.Range("A:G") ' Adjust the range as necessary
If Not IsEmpty(cell.Value) Then
primaryKey = Sheet1.Range("B7")

' Find the ACCT in DATA
Set FoundCell = lookupRange.Columns(1).Find(What:=primaryKey, LookIn:=xlValues, LookAt:=xlWhole)

' If found, update the corresponding amount in DATA
If Not FoundCell Is Nothing Then
wsTarget.Cells(FoundCell.Row, 16).Value = Sheet1.Range("B13")
wsTarget.Cells(FoundCell.Row, 18).Value = Sheet1.Range("E13")
wsTarget.Cells(FoundCell.Row, 17).Value = Sheet1.Range("G13")
wsTarget.Cells(FoundCell.Row, 19).Value = Sheet1.Range("B14")
wsTarget.Cells(FoundCell.Row, 20).Value = Sheet1.Range("E14")
wsTarget.Cells(FoundCell.Row, 21).Value = Sheet1.Range("G14")
wsTarget.Cells(FoundCell.Row, 22).Value = Sheet1.Range("E15")
wsTarget.Cells(FoundCell.Row, 23).Value = Sheet1.Range("B15")
wsTarget.Cells(FoundCell.Row, 24).Value = Sheet1.Range("G15")
End If





End If
Next cell

MsgBox "Update complete!"
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You haven't given us much to go on in terms of a data example, but this line:
VBA Code:
For Each cell In wsTarget.Range("A:G")
is not helping because it is processing every cell in 1,048,576 rows by 7 columns = 7,340,032.

In the first instance try limiting it to the range that actually contains data.
VBA Code:
    Dim LastRow As Long
    LastRow = wsTarget.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each cell In wsTarget.Range("A" & LastRow & ":G" & LastRow) ' Adjust the range as necessary
    If Not IsEmpty(cell.Value) Then
 
Upvote 0
Looping can be very slow. Using arrays will speed things up considerably. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach screenshots (not pictures) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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