Excel crashes after running VBA script

Razor147

New Member
Joined
Mar 27, 2013
Messages
13
The following VBA script runs okay but when I click in a cell afterwards excel does not respond and I have to use <CTRL+ALT+DEL> to close excel. Can anybody help me please, I am new to VBA code. I am using Excel 2010. Sub Random_Name()
Dim rNum, gName, DupYN
Dim rng2 As Range
Dim rng As Range
Set rng = Range("B:B")
Set rng2 = Range("C2:C81")
rng2 = ""
For Each cell In rng
cell.Select
here:
rNum = Int((80 - 1 + 1) * Rnd + 1)
gName = Application.VLookup(rNum, Range("A2:B148"), 2, False)
DupYN = Application.WorksheetFunction. _
CountIf(Range("C2:C81"), gName)
If DupYN = 1 Or ActiveCell.Value = gName Then
GoTo here
Else
ActiveCell.Offset(0, 1).Value = gName
End If
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Does this work for you?

Code:
Sub Random_Name()
    Dim rNum, gName, DupYN
    Dim rng2 As Range
    Dim rng As Range
    Dim cell As Range
    Set rng = Range("B2:B81")
    Set rng2 = Range("C2:C81")
    rng2 = ""
    For Each cell In rng
        cell.Select
here:
        rNum = Int((80 - 1 + 1) * Rnd + 1)
        gName = Application.VLookup(rNum, Range("A2:B148"), 2, False)
        DupYN = Application.WorksheetFunction. _
        CountIf(Range("C2:C81"), gName)
        If DupYN = 1 Or ActiveCell.Value = gName Then
            GoTo here
        Else
            ActiveCell.Offset(0, 1).Value = gName
        End If
    Next cell
 End Sub
 
Upvote 0
Sorry, skip that code and try:

Code:
Sub Random_Name()
    Dim Rng As Range
    Dim Cell As Range
    Dim rNum, gName, DupYN
    Set Rng = Range("C2:C81")
    Rng.ClearContents
    For Each Cell In Rng
here:
        rNum = Int(148 * Rnd + 1)
        gName = Application.VLookup(rNum, Range("A2:B148"), 2, False)
        DupYN = Application.WorksheetFunction. _
            CountIf(Rng, gName)
        If DupYN = 1 Or Cell.Value = gName Then
            GoTo here
        Else
            Cell.Value = gName
        End If
    Next Cell
 End Sub
 
Upvote 0
The following VBA script runs okay but when I click in a cell afterwards excel does not respond and I have to use <CTRL+ALT+DEL>to close excel. Can anybody help me please, I am new to VBA code. I am using Excel 2010. Sub Random_Name()
Dim rNum, gName, DupYN
Dim rng2 As Range
Dim rng As Range
Set rng = Range("B:B")
Set rng2 = Range("C2:C81")
rng2 = ""
For Each cell In rng
cell.Select
here:
rNum = Int((80 - 1 + 1) * Rnd + 1)
gName = Application.VLookup(rNum, Range("A2:B148"), 2, False)
DupYN = Application.WorksheetFunction. _
CountIf(Range("C2:C81"), gName)
If DupYN = 1 Or ActiveCell.Value = gName Then
GoTo here
Else
ActiveCell.Offset(0, 1).Value = gName
End If
End Sub
Excel would eventually respond to you if you waited long enough. The problem with your code is in the line I highlighted in red... the rng variable was set to the entire column B (see line I highlighted in purple) and that column has over 1000000 cells in it, so your For..Next loop will iterate over 1000000 times executing the code in that loop on each iteration. A million plus iterations takes time to execute... you just didn't wait long enough. Andrew has given you code which may be what you actually want to do so you should follow up with him... I just wanted to give you an explanation for what you described was happening.
 
Upvote 0
Thanks Andrew. I pasted your code into my spreadsheet and ran it, I am now getting the following error - Run-time error '13' Type mismatch
 
Upvote 0

Forum statistics

Threads
1,203,075
Messages
6,053,392
Members
444,661
Latest member
liamoohay

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