#1   Report Post  
Dan Chupinsky
 
Posts: n/a
Default Working with a range

Can someone suggest visual basic code for a macro to check for the occurance
of a single digit (1-9) in a range such as A1:C3. Each cell in the range
must contain a digit once, ie. no repeats, and no blanks.

Dan


  #3   Report Post  
Dave Peterson
 
Posts: n/a
Default

You could use a worksheet function that counts unique values in that range:

=SUMPRODUCT((A1:C3<"")/COUNTIF(A1:C3,A1:C3&""))

You can use the equivalent worksheet function in code like:

Option Explicit
Sub testme()

Dim myRng As Range
Dim wks As Worksheet
Dim UniqueCount As String

Set wks = Worksheets("Sheet1")
Set myRng = wks.Range("a1:c3")

UniqueCount = Application.Evaluate _
("SUMPRODUCT((" & myRng.Address(external:=True) _
& "<"""")/COUNTIF(" & myRng.Address(external:=True) _
& "," & myRng.Address(external:=True) & "&""""))")

If UniqueCount < 9 Then
MsgBox UniqueCount
Else
MsgBox "All there!"
End If

End Sub

This previous routine will look for 9 distinct entries. It won't care if you
type 1.5, 2.5, 3.3, ASDF, etc as long as you have 9 of them.

If you want to check to see if each of the numbers 1-9 are there, you could be
more careful with something like this:

Sub testme2()

Dim myRng As Range
Dim wks As Worksheet
Dim iCtr As Long
Dim AllThere As Boolean

Set wks = Worksheets("Sheet1")
Set myRng = wks.Range("a1:c3")

AllThere = True
For iCtr = 1 To 9
If Application.CountIf(myRng, iCtr) < 1 Then
AllThere = False
Exit For
End If
Next iCtr

If AllThere = True Then
MsgBox "all there"
Else
MsgBox "not all there"
End If

End Sub

Dan Chupinsky wrote:

Can someone suggest visual basic code for a macro to check for the occurance
of a single digit (1-9) in a range such as A1:C3. Each cell in the range
must contain a digit once, ie. no repeats, and no blanks.

Dan


--

Dave Peterson
  #4   Report Post  
Dan Chupinsky
 
Posts: n/a
Default

Thanks to Don who gave me direction that I can use elsewhere in my project
and to Dave who gave me the exact code [testme2()] that I wanted to create.

This forum is terrific.

Dan
"Dan Chupinsky" wrote in message
. ..
Can someone suggest visual basic code for a macro to check for the
occurance of a single digit (1-9) in a range such as A1:C3. Each cell in
the range must contain a digit once, ie. no repeats, and no blanks.

Dan



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I create a schedule from a list of dates ? Gavin Morris Charts and Charting in Excel 2 October 28th 10 04:08 PM
3d range searching thephoenix12 Excel Discussion (Misc queries) 0 June 15th 05 06:18 PM
Reveal cell formats and extendable range in tool/statusbar/icon. Danny O'Hern ([email protected]) Excel Worksheet Functions 0 April 29th 05 01:16 PM
Can a formula check for a certain value in a range? Lee IT Excel Discussion (Misc queries) 3 April 8th 05 07:36 AM
Define a range based on another named range Basil Excel Worksheet Functions 2 February 21st 05 01:47 PM


All times are GMT +1. The time now is 08:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"