Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Defining a range for a look up

I have a vlookup formula, but want to programatically change the range the
look up looks at.
The data set is filtered by vba by user selection from a drop down list
If I want to change
=IF(S4="","",VLOOKUP(S4,MFData!$D$2:$S$4000,16,FAL SE))
to the same range as is filtered.
In one scenario the data range is filtered to rows 2008 to 2243 (rather than
2 to 4000), how can I change the formula to
=IF(S4="","",VLOOKUP(S4,MFData!$D$2008:$S$2243,16, FALSE))
Presumably by defining a named range each time the filter is applied.

Thanks in anticipation


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Defining a range for a look up

If the range will Always be contiguous

Dim rng as range, rng1 as Range
Range("D2:S4000").Name = "Table1"
if Activesheet.AutoFilterMode then
set rng = Activesheet.AutoFilterRange
rng.offset(1,0).Resize(rng.rows.count-1)
On Error Resume Next
set rng1 = rng.SpecialCells(xlVisible)
On Error goto 0
if rng1 is nothing then
msgbox "No visible rows"
exit sub
elseif rng1.areas.count 1 then
msgbox "Filtered area is not contiguous"
exit sub
end if
rng1.Name = "Table1"



=IF(S4="","",VLOOKUP(S4,Table1,16,FALSE))

--
Regards,
Tom Ogilvy


"MH UK" wrote in message
...
I have a vlookup formula, but want to programatically change the range the
look up looks at.
The data set is filtered by vba by user selection from a drop down list
If I want to change
=IF(S4="","",VLOOKUP(S4,MFData!$D$2:$S$4000,16,FAL SE))
to the same range as is filtered.
In one scenario the data range is filtered to rows 2008 to 2243 (rather

than
2 to 4000), how can I change the formula to
=IF(S4="","",VLOOKUP(S4,MFData!$D$2008:$S$2243,16, FALSE))
Presumably by defining a named range each time the filter is applied.

Thanks in anticipation




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Defining a range for a look up

Tom,

Thank you.
I am getting a runtime error "Invalid Use of property" at
rng.Offset(1, 0).Resize (rng.Rows.Count - 1)

I shall have a play around to try and fix, unless you beat me to the fix!

Martin.


"Tom Ogilvy" wrote:

If the range will Always be contiguous

Dim rng as range, rng1 as Range
Range("D2:S4000").Name = "Table1"
if Activesheet.AutoFilterMode then
set rng = Activesheet.AutoFilterRange
rng.offset(1,0).Resize(rng.rows.count-1)
On Error Resume Next
set rng1 = rng.SpecialCells(xlVisible)
On Error goto 0
if rng1 is nothing then
msgbox "No visible rows"
exit sub
elseif rng1.areas.count 1 then
msgbox "Filtered area is not contiguous"
exit sub
end if
rng1.Name = "Table1"



=IF(S4="","",VLOOKUP(S4,Table1,16,FALSE))

--
Regards,
Tom Ogilvy


"MH UK" wrote in message
...
I have a vlookup formula, but want to programatically change the range the
look up looks at.
The data set is filtered by vba by user selection from a drop down list
If I want to change
=IF(S4="","",VLOOKUP(S4,MFData!$D$2:$S$4000,16,FAL SE))
to the same range as is filtered.
In one scenario the data range is filtered to rows 2008 to 2243 (rather

than
2 to 4000), how can I change the formula to
=IF(S4="","",VLOOKUP(S4,MFData!$D$2008:$S$2243,16, FALSE))
Presumably by defining a named range each time the filter is applied.

Thanks in anticipation





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Defining a range for a look up

Tom,

What if the range is not contiguous?

"Tom Ogilvy" wrote:

If the range will Always be contiguous

Dim rng as range, rng1 as Range
Range("D2:S4000").Name = "Table1"
if Activesheet.AutoFilterMode then
set rng = Activesheet.AutoFilterRange
rng.offset(1,0).Resize(rng.rows.count-1)
On Error Resume Next
set rng1 = rng.SpecialCells(xlVisible)
On Error goto 0
if rng1 is nothing then
msgbox "No visible rows"
exit sub
elseif rng1.areas.count 1 then
msgbox "Filtered area is not contiguous"
exit sub
end if
rng1.Name = "Table1"



=IF(S4="","",VLOOKUP(S4,Table1,16,FALSE))

--
Regards,
Tom Ogilvy


"MH UK" wrote in message
...
I have a vlookup formula, but want to programatically change the range the
look up looks at.
The data set is filtered by vba by user selection from a drop down list
If I want to change
=IF(S4="","",VLOOKUP(S4,MFData!$D$2:$S$4000,16,FAL SE))
to the same range as is filtered.
In one scenario the data range is filtered to rows 2008 to 2243 (rather

than
2 to 4000), how can I change the formula to
=IF(S4="","",VLOOKUP(S4,MFData!$D$2008:$S$2243,16, FALSE))
Presumably by defining a named range each time the filter is applied.

Thanks in anticipation





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Defining a range for a look up

Here is the revised code:

Sub abc()
Dim rng As Range, rng1 As Range
Range("D2:S4000").Name = "Table1"
If ActiveSheet.AutoFilterMode Then
Set rng = ActiveSheet.AutoFilter.Range
Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
On Error Resume Next
Set rng1 = rng.SpecialCells(xlVisible)
On Error GoTo 0
If rng1 Is Nothing Then
MsgBox "No visible rows"
Exit Sub
ElseIf rng1.Areas.Count 1 Then
Debug.Print rng1.Address
MsgBox "Filtered area is not contiguous"
Exit Sub
End If
rng1.Name = "Table1"
End If
End Sub


You can't look up a none contiguous range.


--
Regards,
Tom Ogilvy


"MH UK" wrote in message
...
Tom,

What if the range is not contiguous?

"Tom Ogilvy" wrote:

If the range will Always be contiguous

Dim rng as range, rng1 as Range
Range("D2:S4000").Name = "Table1"
if Activesheet.AutoFilterMode then
set rng = Activesheet.AutoFilterRange
rng.offset(1,0).Resize(rng.rows.count-1)
On Error Resume Next
set rng1 = rng.SpecialCells(xlVisible)
On Error goto 0
if rng1 is nothing then
msgbox "No visible rows"
exit sub
elseif rng1.areas.count 1 then
msgbox "Filtered area is not contiguous"
exit sub
end if
rng1.Name = "Table1"



=IF(S4="","",VLOOKUP(S4,Table1,16,FALSE))

--
Regards,
Tom Ogilvy


"MH UK" wrote in message
...
I have a vlookup formula, but want to programatically change the range

the
look up looks at.
The data set is filtered by vba by user selection from a drop down

list
If I want to change
=IF(S4="","",VLOOKUP(S4,MFData!$D$2:$S$4000,16,FAL SE))
to the same range as is filtered.
In one scenario the data range is filtered to rows 2008 to 2243

(rather
than
2 to 4000), how can I change the formula to
=IF(S4="","",VLOOKUP(S4,MFData!$D$2008:$S$2243,16, FALSE))
Presumably by defining a named range each time the filter is applied.

Thanks in anticipation









  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Defining a range for a look up

Tom,

Thanks - you pointed me in the right direction and is now sorted.

"Tom Ogilvy" wrote:

If the range will Always be contiguous

Dim rng as range, rng1 as Range
Range("D2:S4000").Name = "Table1"
if Activesheet.AutoFilterMode then
set rng = Activesheet.AutoFilterRange
rng.offset(1,0).Resize(rng.rows.count-1)
On Error Resume Next
set rng1 = rng.SpecialCells(xlVisible)
On Error goto 0
if rng1 is nothing then
msgbox "No visible rows"
exit sub
elseif rng1.areas.count 1 then
msgbox "Filtered area is not contiguous"
exit sub
end if
rng1.Name = "Table1"



=IF(S4="","",VLOOKUP(S4,Table1,16,FALSE))

--
Regards,
Tom Ogilvy


"MH UK" wrote in message
...
I have a vlookup formula, but want to programatically change the range the
look up looks at.
The data set is filtered by vba by user selection from a drop down list
If I want to change
=IF(S4="","",VLOOKUP(S4,MFData!$D$2:$S$4000,16,FAL SE))
to the same range as is filtered.
In one scenario the data range is filtered to rows 2008 to 2243 (rather

than
2 to 4000), how can I change the formula to
=IF(S4="","",VLOOKUP(S4,MFData!$D$2008:$S$2243,16, FALSE))
Presumably by defining a named range each time the filter is applied.

Thanks in anticipation





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
Defining a range and use it's value Petitboeuf[_3_] Excel Programming 1 December 15th 05 06:34 PM
Help with defining a Range drhalter Excel Programming 2 August 11th 05 11:56 PM
Defining a range Don Excel Worksheet Functions 1 February 25th 05 03:54 PM
Defining a Range Ric[_5_] Excel Programming 4 April 26th 04 07:34 PM
Defining a range Matt Excel Programming 3 January 23rd 04 03:21 PM


All times are GMT +1. The time now is 11:03 AM.

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"