Help with VBA

spg2112

Active Member
Joined
Feb 27, 2002
Messages
316
Hello all,

I write some sloppy VBA, that's why I'm here. I need help with a subroutine to check 6 random numbers for duplicates in the array. Here's what I have so far, but it doesn't work...please help.

The numbers are in K5:P5

Sub sgCheck()
'*** check for duplicates
If "L5" = "K5" Or "M5" = "K5" Or "N5" = "K5" Or "O5" = "K5" Or "P5" = "K5" Then
sgRandom
Else
If "M5" = "L5" Or "N5" = "L5" Or "O5" = "L5" Or "P5" = "L5" Then
sgRandom
Else
If "N5" = "M5" Or "O5" = "M5" Or "P5" = "M5" Then
sgRandom
Else
If "O5" = "N5" Or "P5" = "N5" Then
sgRandom
Else
If "P5" = "O5" Then
sgRandom
End If
End If
End If
End If
End If
End Sub


Steve
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Well, if a duplicate is found it runs the sgRandom routine until I have a set of 6 numbers (from 1 to 54) with no duplicates.

Steve
 
Upvote 0
Steve

Play around with this. I tested it.

Sub sgCheck()
Dim icol As Integer
Dim iCnt As Integer

For iCnt = 11 To 16
For icol = 11 To 16
If icol = iCnt Then Exit For
If Cells(5, iCnt).Value = _
Cells(5, icol).Value Then sgRandon
Next
Next
End Sub


Search VBA help for cells to understand the syntax. You do not use cell references in VBA the same way you do in formulas.
Your code looks like it will work fine.
Change all of your cell references like this.

Change this:


If "P5" = "O5" Then

to this:

If Range("P5").value = Range("O5").value Then

Tom
This message was edited by TsTom on 2002-04-04 21:41
This message was edited by TsTom on 2002-04-04 21:42
 
Upvote 0
Thanks Tom! I couldn't get the 1st routine you suggested to work, but adding the "Range" before the cell reference did the trick!

Steve
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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