trouble with VBA code, how do i activate a cell?

bigdan

Well-known Member
Joined
Oct 5, 2009
Messages
846
Office Version
  1. 2013
Platform
  1. Windows
I'm trying to find a value in a list. I've written some code that is able to find the value just find, ie a msgbox will say "found the value at E8", but I'd like to be able to bring the active cell to where the value is found. ie I'd like Excel to bring the cursor to E8. can anyone advise?

Here's the code. I've commented out the one line where I tried to activate the relevant cell. That command gave a compile error.

Btw I haven't posted much code here before, what is the etiquette when doing so? I'm guessing it's not just to paste the code in here?


For Each cell In Range("E1:E30")
If cell.Value = search Then
'cell.Row.Activate (this command gave the error)
MsgBox "found the value at " & "Cell E" & (i + 1)
Exit For
End If
i = i + 1
Next cell
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
The .Row part is what's messing you up.
VBA Code:
cell.Activate
or also
VBA Code:
cell.Select

Pasting in the code is good. What's even better is to put code tags around your code to preserve formatting and show color-coding. After you paste your code, select it, then click the VBA button on the edit controls to add the code tags.

You also don't need to keep track with i when you already know which cell it is.

VBA Code:
For Each cell In Range("E1:E30")
   If cell.Value = search Then
      cell.Activate
      MsgBox "found the value at " & cell.Address(False, False)
      Exit For
   End If
Next cell
 
Upvote 0
Jeff got you going already but this is a different approach.
Code:
Sub No_Loop_Required()
Dim search As String
search = Application.InputBox("Please Enter The Search Value", "Text String Required", , , , , 2)
MsgBox "Found the value at " & Range("E1:E30").Find(search, , , 1).Address(0, 0)
End Sub
FYI
 

Attachments

  • Use Code Tags MrExcel.JPG
    Use Code Tags MrExcel.JPG
    50.2 KB · Views: 3
Upvote 1

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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