looking for the column

Irek1974

Board Regular
Joined
Jul 17, 2009
Messages
64
hi,
I would like to write part of the code that select a couple of cells in a row (for example : Range("A1:A10).select) than:
-find a text which is in one of these cells (for example "HP") - and gives me back column in which this text is input

texts in selected cells will always be the same (for example : HP, CH, SD, .... ect.)

could someone help pls
Irek
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try something like this

Code:
Sub test()
Dim Found As Range
Set Found = Range("A1:A10").Find(what:="HP", lookat:=xlWhole, LookIn:=xlValues)
If Found Is Nothing Then
    MsgBox "Not found", vbExclamation
Else
    MsgBox "Found in Row " & Found.Row, vbInformation
End If
End Sub
 
Upvote 0
hi
it gives me the number of the row - but I need column (I changed MsgBox "Found in Row " & Found.Row, vbInformation into MsgBox "Found in Row " & Found.column, vbInformation

it gives me number of the column but I need it gives me the result not as a number but for example C, D , ect


Try something like this

Code:
Sub test()
Dim Found As Range
Set Found = Range("A1:A10").Find(what:="HP", lookat:=xlWhole, LookIn:=xlValues)
If Found Is Nothing Then
    MsgBox "Not found", vbExclamation
Else
    MsgBox "Found in Row " & Found.Row, vbInformation
End If
End Sub
 
Upvote 0
Hmm. If you are looking in A1:A10 then the column has to be A surely?

Perhaps you meant something like

Code:
Sub test()
Dim Found As Range
Set Found = Range("A1:F10").Find(what:="HP", lookat:=xlWhole, LookIn:=xlValues)
If Found Is Nothing Then
    MsgBox "Not found", vbExclamation
Else
    MsgBox "Found in Column " & Chr(64 + Found.Column), vbInformation
End If
End Sub
 
Upvote 0
thank you

Hmm. If you are looking in A1:A10 then the column has to be A surely?

Perhaps you meant something like

Code:
Sub test()
Dim Found As Range
Set Found = Range("A1:F10").Find(what:="HP", lookat:=xlWhole, LookIn:=xlValues)
If Found Is Nothing Then
    MsgBox "Not found", vbExclamation
Else
    MsgBox "Found in Column " & Chr(64 + Found.Column), vbInformation
End If
End Sub
 
Upvote 0
I have one more problem.
what about finding date ?

I'm looking for the month from cell C4 in a range of cells D5:O5 - and need to get the column in msgbox

I have a code :

Dim month
month = Range("C4")
Set month = Range("D5:O5").Find(what:=month, lookat:=xlWhole, LookIn:=xlValues)
MsgBox Chr(64 + month.Column), vbInformation

it stops at line "msgbox ......




Hmm. If you are looking in A1:A10 then the column has to be A surely?

Perhaps you meant something like

Code:
Sub test()
Dim Found As Range
Set Found = Range("A1:F10").Find(what:="HP", lookat:=xlWhole, LookIn:=xlValues)
If Found Is Nothing Then
    MsgBox "Not found", vbExclamation
Else
    MsgBox "Found in Column " & Chr(64 + Found.Column), vbInformation
End If
End Sub
 
Upvote 0
You should avoid using month as a variable name as it is the name of a VBA function. Also, check whether the month is found. Assuming that you are searching for text try something like

Code:
Dim amonth As String, xmonth As Range
amonth = Range("C4").Value
Set xmonth = Range("D5:O5").Find(what:=amonth, lookat:=xlWhole, LookIn:=xlValues)
If xmonth Is Nothing Then
    MsgBox "Not found", vbExclamation
Else
    MsgBox Chr(64 + xmonth.Column), vbInformation
End If

The a
 
Upvote 0
It doesn't work.
as a temporary solution I did as follows:

below cell C4 (in C5) I have a text which could be found in range below D5:O5 (in D6:O6). In this situation it works but as I mentioned it's only temporary solution as I need to look in a range for a data which is in cell C4.
Maybe the problem is with "chr(64 ......) " as 256 is the max of arguments chr could take ?

what do you think ?


You should avoid using month as a variable name as it is the name of a VBA function. Also, check whether the month is found. Assuming that you are searching for text try something like

Code:
Dim amonth As String, xmonth As Range
amonth = Range("C4").Value
Set xmonth = Range("D5:O5").Find(what:=amonth, lookat:=xlWhole, LookIn:=xlValues)
If xmonth Is Nothing Then
    MsgBox "Not found", vbExclamation
Else
    MsgBox Chr(64 + xmonth.Column), vbInformation
End If

The a
 
Upvote 0
Try this

Code:
Function ColumnLetter(anyCell As Range) As String
ColumnLetter = Left(anyCell.Address(False, False), 1 - CInt(anyCell.Column > 26))
End Function

Sub test()
Dim amonth As String, xmonth As Range
amonth = Range("C4").Value
Set xmonth = Range("D5:IV5").Find(what:=amonth, lookat:=xlWhole, LookIn:=xlValues)
If xmonth Is Nothing Then
    MsgBox "Not found", vbExclamation
Else
    MsgBox ColumnLetter(xmonth)
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,030
Members
448,940
Latest member
mdusw

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