Macro Freezes PC

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
A friend has very kindly given me a Macro that does what I need it to do. The problem is after it has been running a few minutes Excel stops responding and freezes and I have to ctrl+alt+del. The macro is a very small code but has to look through a lot of data. What can be done to speed macros up?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Sub MatchAndUpdate()
R = Sheets("Source").Range("A" & Rows.Count).End(xlUp).Row
For a = 1 To R
q = Sheets("Source").Cells(a, 12).Text
RR = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For aa = 1 To RR
If Sheets("Sheet1").Cells(aa, 12).Text = q Then
x = Sheets("Sheet1").Cells(aa, 9).Text
xx = Sheets("Source").Cells(a, 9).Text
If x = xx Then GoTo nextaa
If Mid$(xx, 1, Len(x)) = x Then
Sheets("Sheet1").Cells(aa, 9) = Sheets("Source").Cells(a, 9)
Sheets("Sheet1").Cells(aa, 9).Interior.ColorIndex = 6
End If
End If
nextaa:
Next aa
Next a
End Sub

I guess by reading the code you can see what it has to do but this link tells you:-

http://www.mrexcel.com/forum/showthread.php?t=531235
 
Upvote 0
Try

Code:
Sub MatchAndUpdate()
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
R = Sheets("Source").Range("A" & Rows.Count).End(xlUp).Row
For a = 1 To R
    q = Sheets("Source").Cells(a, 12).Text
    RR = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    For aa = 1 To RR
        If Sheets("Sheet1").Cells(aa, 12).Text = q Then
            x = Sheets("Sheet1").Cells(aa, 9).Text
            xx = Sheets("Source").Cells(a, 9).Text
        If x = xx Then GoTo nextaa
        If Mid$(xx, 1, Len(x)) = x Then
            Sheets("Sheet1").Cells(aa, 9) = Sheets("Source").Cells(a, 9)
            Sheets("Sheet1").Cells(aa, 9).Interior.ColorIndex = 6
        End If
        End If
nextaa:
    Next aa
Next a
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With
End Sub
When posting code please use code tags:


[code]
your code here
[/code]
 
Upvote 0
Still freezing I'm afraid, how do I post codes with tags also.
 
Upvote 0
I don't see why that should freeze but maybe if you have event code

Code:
Sub MatchAndUpdate()
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    .EnableEvents = False
End With
R = Sheets("Source").Range("A" & Rows.Count).End(xlUp).Row
For a = 1 To R
    q = Sheets("Source").Cells(a, 12).Text
    RR = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    For aa = 1 To RR
        If Sheets("Sheet1").Cells(aa, 12).Text = q Then
            x = Sheets("Sheet1").Cells(aa, 9).Text
            xx = Sheets("Source").Cells(a, 9).Text
        If x = xx Then GoTo nextaa
        If Mid$(xx, 1, Len(x)) = x Then
            Sheets("Sheet1").Cells(aa, 9) = Sheets("Source").Cells(a, 9)
            Sheets("Sheet1").Cells(aa, 9).Interior.ColorIndex = 6
        End If
        End If
nextaa:
    Next aa
Next a
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
End With
End Sub

To use code tags (which preserves the indenting}



type [code]

paste in the code

type [/code]

 
Upvote 0
Still the same, when I ctrl+alt+del a error box comes up briefly saying

Run-time error '-2147417848 (80010108)':

Method 'Text' of object 'Range' failed

end, debug etc..
 
Upvote 0
Click Debug. Which line of code is highlighted? Are there error values like #N/A in the range?
 
Upvote 0
I think it was
Code:
If Sheets("Sheet1").Cells(aa, 12).Text = q Then

that was highlighted yellow. It went to quick!
 
Upvote 0
Maybe like this. You might need additional checks for errors

Code:
Sub MatchAndUpdate()
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    .EnableEvents = False
End With
R = Sheets("Source").Range("A" & Rows.Count).End(xlUp).Row
For a = 1 To R
    q = Sheets("Source").Cells(a, 12).Text
    RR = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    For aa = 1 To RR
        If Not IsError(Sheets("Sheet1").Cells(aa, 12)) Then
            If Sheets("Sheet1").Cells(aa, 12).Text = q Then
                x = Sheets("Sheet1").Cells(aa, 9).Text
                xx = Sheets("Source").Cells(a, 9).Text
            If x = xx Then GoTo nextaa
            If Mid$(xx, 1, Len(x)) = x Then
                Sheets("Sheet1").Cells(aa, 9) = Sheets("Source").Cells(a, 9)
                Sheets("Sheet1").Cells(aa, 9).Interior.ColorIndex = 6
            End If
            End If
        End If
nextaa:
    Next aa
Next a
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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