Define row or cell variables

matthew230

Board Regular
Joined
Jan 2, 2006
Messages
152
Hi Guys,

I am completely self taught, and need a few pointers on learning a new technique.

I need to be able to look at an entire column and search for a value. Once the value is found, then I need to make that cell a variable.

I have got so far

Dim currentcell As Range

Sub test()
For Each cell In Range("a1:a10")
If cell.Value = "b" Then
MsgBox "found it"
cell.Select
currentcell = Range(cell)

End If
Next
MsgBox currentcell

End Sub

The currentcell = range(cell) is the line that doesn't work.

Can someone help with this ??

Many thanks,

Matt
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Code:
Sub test()
Dim currentcell As Range
Set currentcell = [A:A].Find("b")
If currentcell Is Nothing Then
    MsgBox "Not found"
Else
    MsgBox "Found it"
    currentcell.Select
End If
End Sub
 
Upvote 0
You may want to look at Find:

Code:
Sub test()
Dim c As Range
Set c = Range("A1:A10").Find("b")
If c Is Nothing Then Exit Sub
MsgBox "Found it in cell " & c.Address
End Sub

If it finds it, it sets c to be the cell that it found it at. c contains a range object and is treated as such.

But using your loop, you can use a range object the same way:

Code:
Sub test()
Dim c As Range, d As Range
For Each d In Range("A1:A10")
    If d = "b" Then
        Set c = d
        Exit For
    End If
Next
If Not c Is Nothing Then MsgBox "Found it in cell " & c.Address(0, 0)
End Sub
 
Upvote 0
You may want to look at Find:

Code:
Sub test()
Dim c As Range
Set c = Range("A1:A10").Find("b")
If c Is Nothing Then Exit Sub
MsgBox "Found it in cell " & c.Address
End Sub

If it finds it, it sets c to be the cell that it found it at. c contains a range object and is treated as such.

But using your loop, you can use a range object the same way:

Code:
Sub test()
Dim c As Range, d As Range
For Each d In Range("A1:A10")
    If d = "b" Then
        Set c = d
        Exit For
    End If
Next
If Not c Is Nothing Then MsgBox "Found it in cell " & c.Address(0, 0)
End Sub

Hi,

That is great, it was the .address part I needed to work out.

One more question, how do I now go about selecting another cell relative to the one found. In this example, I then need to move the cell focus one row down and one column across.

Thank you very much for your invaluable help !!

matt
 
Upvote 0
Code:
Sub test()
Dim c As Range
Set c = Range("A1:A10").Find("b")
If c Is Nothing Then Exit Sub
c.Offset(1, 1).Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,517
Members
448,968
Latest member
Ajax40

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