Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default hlookup in VBA

Hi,
is it possible to use HLOOKUP in vba. I'm having problems, which may be
syntax ??

Trying to look across row1 to find which column has contents
"specific_word", then put that column number into a variable.

Any tips welcome.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default hlookup in VBA

Sub FindWord()
Dim specific_word As String
Dim rng As Range
Dim vVal As Variant
specific_word = "abcd"
Set rng = Range(Cells(1, 10), Cells(2, 30))
vVal = Application.HLookup(specific_word, rng, 2, False)
If IsError(vVal) Then
MsgBox specific_word & " was not found"
Else
MsgBox specific_word & " found, row 2 value " & vVal
End If

End Sub

However, Hlookup doesn't return the location/column where the match was
found. It returns a value in a corresponding row. In the example, I look in
row 1 and return the value below the match in row 2.

You would use MATCH to find the column

Sub FindColumn()
Dim specific_word As String
Dim rng As Range
Dim vVal As Variant
specific_word = "abcd"
Set rng = Range(Cells(1, 10), Cells(1, 30))
vVal = Application.Match(specific_word, rng,0)
If IsError(vVal) Then
MsgBox specific_word & " was not found"
Else
MsgBox specific_word & " found at column " & rng(1,vVal).column
End If

End Sub




--
Regards,
Tom Ogilvy


Dennis wrote in message
...
Hi,
is it possible to use HLOOKUP in vba. I'm having problems, which may be
syntax ??

Trying to look across row1 to find which column has contents
"specific_word", then put that column number into a variable.

Any tips welcome.




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default hlookup in VBA

Dennis,
As an alternative to Tom's approach (since I read a lot of ranges into
2-dimension VBA arrays), if you have reason to do that ...

ArrayX=Sheets("TestSheet").Range("TestRange")

gives you a 2D matrix. Then try....

colnum = FindCOLinArray("abcd",1,1,ArrayX)

if colnum<1 then
'Error condition: abcd" not found



Function FindCOLInArray(Target, row, startcol, xArray) as Long
FindCOLInArray = 0
For j = startcol To UBound(xArray, 2)
If xArray(row, j)= Target Then
FindCOLInArray = j
Exit Function
End If
Next j
End Function

(I've skipped all the good variable declaration stuff that Tom has included
for the purpose of brevity. Use HIS coding techniques, not mine.)

Hope this might be helpful,
Alex






"Tom Ogilvy" wrote in message
...
Sub FindWord()
Dim specific_word As String
Dim rng As Range
Dim vVal As Variant
specific_word = "abcd"
Set rng = Range(Cells(1, 10), Cells(2, 30))
vVal = Application.HLookup(specific_word, rng, 2, False)
If IsError(vVal) Then
MsgBox specific_word & " was not found"
Else
MsgBox specific_word & " found, row 2 value " & vVal
End If

End Sub

However, Hlookup doesn't return the location/column where the match was
found. It returns a value in a corresponding row. In the example, I look

in
row 1 and return the value below the match in row 2.

You would use MATCH to find the column

Sub FindColumn()
Dim specific_word As String
Dim rng As Range
Dim vVal As Variant
specific_word = "abcd"
Set rng = Range(Cells(1, 10), Cells(1, 30))
vVal = Application.Match(specific_word, rng,0)
If IsError(vVal) Then
MsgBox specific_word & " was not found"
Else
MsgBox specific_word & " found at column " & rng(1,vVal).column
End If

End Sub




--
Regards,
Tom Ogilvy


Dennis wrote in message
...
Hi,
is it possible to use HLOOKUP in vba. I'm having problems, which may be
syntax ??

Trying to look across row1 to find which column has contents
"specific_word", then put that column number into a variable.

Any tips welcome.






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
hlookup help stew Excel Discussion (Misc queries) 5 June 24th 09 01:12 PM
HLookup help Txlonghorn76 Excel Worksheet Functions 1 September 16th 08 09:04 PM
Hlookup (sum) Shane Excel Worksheet Functions 2 February 11th 08 01:51 PM
Hlookup? Eden397 Excel Worksheet Functions 1 June 6th 07 03:43 PM
HLOOKUP and SUM [email protected] Excel Discussion (Misc queries) 1 October 26th 06 05:27 PM


All times are GMT +1. The time now is 02:24 PM.

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

About Us

"It's about Microsoft Excel"