Cannot Activate Cell

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
591
Office Version
  1. 2019
Platform
  1. Windows
I am running the code below, the cell that should be activating is A2, somehow R1 keeps getting activated and the next steps are happening in the wrong spot.

what am i doing wrong here?

Code:
Private Sub cmdOk_Click()    
    If chkdefective.Value = True And optPlastic.Value = True Then
    With Worksheets("SealLog").Range("A:A")
            Worksheets("SealLog").Activate
            Cells.Find(txtSealnumberPlastic, LookIn:=xlValues).Activate
                ActiveCell.Offset(0, 5) = "Defective"
                ActiveCell.Offset(0, 6) = "y"
    End With
    End If
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You don't have to activate worksheets or cells to access their data. In fact doing so just slows things down.

Try the following without the "activates". It should work even if "SealLog" is not the active sheet.

Code:
Private Sub cmdOk_Click()

    Dim oFound As Range
    
    If chkdefective.Value = True And optPlastic.Value = True Then
        Set oFound = Worksheets("SealLog").Range("A:A").Find(txtSealnumberPlastic, LookIn:=xlValues)
        If Not oFound Is Nothing Then
            oFound.Offset(0, 5) = "Defective"
            oFound.Offset(0, 6) = "y"
        End If
    End If
    
End Sub
 
Upvote 0
Adding to what Gary has stated

Code:
Cells.Find(txtSealnumberPlastic, LookIn:=xlValues).Activate

Cells is looking at the entire worksheet not a single column as looks like your intention.
You are not using any of the parameters of the Find function and so you are taking a chance that they are set up to look in the right direction and from the right starting point.

Code:
With Worksheets("SealLog").Range("A:A")
You are using a With statement but then not committing any action to it and so this line isn't doing anything.

Possibly the below is something like you want.

Rich (BB code):
Private Sub cmdOk_Click()
    Dim x As Range
    If chkdefective.Value = True And optPlastic.Value = True Then
        With Worksheets("SealLog").Range("A:A")
            Set x = .Find(What:=txtSealnumberPlastic, After:=.Cells(.Rows.Count, 1), LookIn:=xlValues, LookAt _
                                                                                                      :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
            x.Offset(0, 5) = "Defective"
            x.Offset(0, 6) = "y"
        End With
    End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,559
Members
448,970
Latest member
kennimack

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