Cell selection based on a criterion

pt3

New Member
Joined
Oct 5, 2011
Messages
18
Hi,

I am new to VBA and struggling with a piece of code; any help would be much appreciated!

I am attempting to write some code which instructs the selection of a cell, within a range, whereby the cell to select is one row lower than the column header (row 1 of the column), if the column header matches a date in a fixed cell of another sheet (in the code attempt below, the fixed cell, which contains the date, is cell N1 in "Sheet1").

My attempt:

Sub test()
Dim Found As Range, LookFor As String
LookFor = Sheets("Sheet1").Range("N1").Value
Set Found = Sheets("Sheet1").Range("A1:H1").Find(what:=LookFor)
If Found Is Nothing Then
MsgBox "Not found"
Else
Range(Found.Offset(1, 0)).Select
End If
End Sub

Thanks in advance.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
If I understood your description, and we are dealing with two different sheets, then I think you want:
Rich (BB code):
Range(Found.Offset(1, 0).Address).Select
 
Upvote 0
Hello there

If you are talking about 2 sheets, why do you have twice Sheets("Sheet1") in the code?
 
Upvote 0
Thanks for the reply. You are correct- in my example code the reference cell and range are on the same sheet ("Sheet1"). I think the issue may be the line 'LookFor = Sheets("Sheet1").Range("N1").Value' - the macro does not seem to pick up the date in cell N1 from this command.
 
Upvote 0
Try changing this:
Code:
Dim Found As Range, LookFor As String
to this:
Code:
Dim Found As Range, LookFor As Date
 
Upvote 0
This is superb Rory- it now works. Can I ask what the 'Address' addition instructs, as per you first response to my thread.

Many thanks.
 
Upvote 0
It returns the address of the found cell as text - e.g. "A1"
It's only necessary if the found cell is on a different sheet to the one that is active. If not, you can just use:
Code:
Found.Offset(1, 0).Select
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,280
Members
452,902
Latest member
Knuddeluff

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