Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 16th, 2007, 06:24 PM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Compare numbers and letters in same cell

I don't know if this is possible. But what I am tring to do is this. I have two columns of letters and numbers in the same cell. I would like to compare B1 with all rows in Column A that meets two conditions. And if two conditions are met, a TRUE value will go to c1. The two conditions are if the letters match, and if the numbers are within 1000, based on the number in B1, it would return a true value.

For example, if B1 cell is A5000, the conditions would be true, if column A has data between A4000 to A5000. And a true would go to C1.

Thank you for any help with this.







 
Old January 21st, 2007, 03:30 PM
Registered User
 
Join Date: Jan 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Eric,

Code below may do the trick, provided I have understood you correctly

Function CellLet(ByVal st As String)
Dim i As Integer
Dim cellletter As String
For i = 1 To Len(st)
Select Case Asc(Mid(st, i, 1))
Case 65 To 90
cellletter = cellletter & Mid(st, i, 1)
Case 97 To 122
cellletter = cellletter & Mid(st, i, 1)
End Select
Next
CellLet = cellletter
End Function

Function CellNum(ByVal st As String)
Dim i As Integer
Dim cellnumber As Long
For i = 1 To Len(st)
Select Case Asc(Mid(st, i, 1))
Case 48 To 57
cellnumber = cellnumber & Mid(st, i, 1)
End Select
Next
CellNum = cellnumber
End Function

Sub Datacheck()

Dim cell As Range
Dim b As Boolean
Dim cNumber As Long
Dim cLetters As String
Dim myRows As Long

b = True
cNumber = CellNum(Cells(1, 2).Value)
cLetters = CellLet(Cells(1, 2).Value)
myRows = Range("A65536").End(xlUp).Row

For Each cell In Range(Cells(1, 1), Cells(myRows, 1))

If Not IsEmpty(cell) Then

If StrComp(cLetters, CellLet(cell), 1) <> 0 Then
b = False
End If

If cNumber - CellNum(cell) > 1000 Or CellNum(cell) - cNumber > 1000 Then
'change the above condition if required
b = False
End If

End If

Next

Cells(1, 3).Value = b

End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
compare a cell with nothing popaerou Excel VBA 2 October 26th, 2008 12:35 PM
HOW TO COMPARE NUMBERS IN VB, PLEASE HELP chasey1 Beginning VB 6 1 June 24th, 2006 09:52 AM
Remove letters from numbers Corey Access 7 December 18th, 2005 09:09 PM
fillin array with letters from a to Z and numbers sajid C# 10 May 3rd, 2005 03:38 PM
Taking both numbers and letters as input HateMe C# 1 May 13th, 2004 11:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.