Unable to select a cell in a worksheet

DBrian7

New Member
Joined
Feb 15, 2024
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
I need help troubleshooting some VBA code. The following lines of code are part of a larger procedure in an Excel workbook. Before running the procedure, the workbook is open and it contains four worksheets – “Chase8919,” “T_Export,” “D_Export,” and “BFCU&Chase.” The first step of the code clears the entire contents of worksheet “Chase8919,” then cell A1 is selected. The next two steps repeat this process for worksheets “T_Export” and “D_Export,” respectively. The fourth step goes to worksheet “BFCU&Chase” and selects the cell directly below the last non-empty cell in column A. Then the code goes to End Sub.

When I run the procedure there is no error message. The procedure successfully selects the cell directly below the last non-empty cell in column A of worksheet “BFCU&Chase.” It also successfully deletes the contents of worksheets “Chase8919,” “T_Export,” and “D_Export.” It also successfully selects cell A1 in worksheets “Chase8919” and “T_Export.” But it does not select cell A1 in worksheet “D_Export.” It’s as if it ignores the command, .Range("A1").Value = "" in Step 3. I have attempted to solve the problem by trying both the .Activate and .Select methods in steps 1, 2, and 3 instead of .Range("A1").Value = "" but those result in the error messages, “Run-time error ‘1004’: Activate method of Range class failed” and “Run-time error ‘1004’: Select method of Range class failed.”

Here are the lines code:

' Clear contents and select "A1" in worksheets Chase8919, T_Export, and D_Export, then go back to BFCU&Chase.
' Step 1: Delete all data in worksheet Chase8919
With ThisWorkbook.Worksheets("Chase8919")
.Cells.Clear
.Range("A1").Value = ""
End With

' Step 2: Delete all data in worksheet T_Export
With ThisWorkbook.Worksheets("T_Export")
.Cells.Clear
.Range("A1").Value = ""
End With

' Step 3: Delete all data in worksheet D_Export
With ThisWorkbook.Worksheets("D_Export")
.Cells.Clear
.Range("A1").Value = ""
End With

' Step 4: Select the cell directly below the last non-empty cell in column A of worksheet BFCU&Chase
With ThisWorkbook.Worksheets("BFCU&Chase")
Dim lastRowBFCU As Long
lastRowBFCU = .Cells(.Rows.Count, "A").End(xlUp).Row
.Cells(lastRowBFCU + 1, "A").Select
End With
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Ok, I think I see. In order for .Range("A1").Select to work, the worksheet must first be made the activesheet, e.g.:


VBA Code:
    ' Step 3: Delete all data in worksheet D_Export
    With ThisWorkbook.Worksheets("D_Export")
        .Activate
        .Cells.Clear
        .Range("A1").Value = ""
        .Range("A1").Select
    End With
    
    ' Step 4: Select the cell directly below the last non-empty cell in column A of worksheet BFCU&Chase
    With ThisWorkbook.Worksheets("BFCU&Chase")
        .Activate
    Dim lastRowBFCU As Long
        lastRowBFCU = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Cells(lastRowBFCU + 1, "A").Select
    End With
 
Upvote 1
Solution
Also, do you really need .Range("A1").Value = ""? You are already clearing all of the cells just before that line of code.
 
Upvote 1
Ok, I think I see. In order for .Range("A1").Select to work, the worksheet must first be made the activesheet, e.g.:


VBA Code:
    ' Step 3: Delete all data in worksheet D_Export
    With ThisWorkbook.Worksheets("D_Export")
        .Activate
        .Cells.Clear
        .Range("A1").Value = ""
        .Range("A1").Select
    End With
   
    ' Step 4: Select the cell directly below the last non-empty cell in column A of worksheet BFCU&Chase
    With ThisWorkbook.Worksheets("BFCU&Chase")
        .Activate
    Dim lastRowBFCU As Long
        lastRowBFCU = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Cells(lastRowBFCU + 1, "A").Select
    End With
This worked perfectly. Thank you!
 
Upvote 0
Your other option is to use Application.Goto (No better just different)

VBA Code:
    ' Step 3: Delete all data in worksheet D_Export
    With ThisWorkbook.Worksheets("D_Export")
        .Cells.Clear
        Application.Goto .Range("A1"), True
    End With
   
    ' Step 4: Select the cell directly below the last non-empty cell in column A of worksheet BFCU&Chase
    With ThisWorkbook.Worksheets("BFCU&Chase")
        Dim lastRowBFCU As Long
        lastRowBFCU = .Cells(.Rows.Count, "A").End(xlUp).Row
        Application.Goto .Cells(lastRowBFCU + 1, "A"), True
    End With
 
Upvote 1
Ok, I think I see. In order for .Range("A1").Select to work, the worksheet must first be made the activesheet, e.g.:


VBA Code:
    ' Step 3: Delete all data in worksheet D_Export
    With ThisWorkbook.Worksheets("D_Export")
        .Activate
        .Cells.Clear
        .Range("A1").Value = ""
        .Range("A1").Select
    End With
  
    ' Step 4: Select the cell directly below the last non-empty cell in column A of worksheet BFCU&Chase
    With ThisWorkbook.Worksheets("BFCU&Chase")
        .Activate
    Dim lastRowBFCU As Long
        lastRowBFCU = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Cells(lastRowBFCU + 1, "A").Select
    End With

Also, do you really need .Range("A1").Value = ""? You are already clearing all of the cells just before that line of code.
You’re right, I don’t need .Range("A1").Value = "". But please explain why the first two steps worked fine without the .Activate command. Cell A1 was selected in each of those worksheets.
 
Upvote 0
But please explain why the first two steps worked fine without the .Activate command. Cell A1 was selected in each of those worksheets.

Because they aren't using Select, you can only Select a cell on an Active sheet, the same as you can't select a cell manually without having the sheet active.
 
Upvote 1
Same answer as @MARK858

FWIW, there is a lot of commonality in steps 1-3. You could tighten up the code a bit there using a For loop.
VBA Code:
    'Clear contents and select "A1" in worksheets Chase8919, T_Export, and D_Export,
    'then go back to BFCU&Chase.

    Dim WS As Worksheet
    Dim SheetToActivate As String
    With ThisWorkbook
        SheetToActivate = ActiveSheet.Name
        Application.ScreenUpdating = False
        For Each WS In .Worksheets
            Select Case WS.Name
                Case "Chase8919", "T_Export", "D_Export"
                    With WS
                        .Cells.Clear
                        .Activate
                        .Range("A1").Select
                    End With
                Case "BFCU&Chase"
                    With WS
                        .Activate
                        SheetToActivate = .Name     'to make sure sheet BFCU&Chase is active after everything is done
                        .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Select
                    End With
            End Select
        Next WS
        .Worksheets(SheetToActivate).Activate
        Application.ScreenUpdating = True
    End With
 
Upvote 1

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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