Run-time error when using Worksheets("Sheet1").Activate

Ottsel

Board Regular
Joined
Jun 4, 2022
Messages
174
Office Version
  1. 365
Platform
  1. Windows
I'm using the following code before running my script to help cover any errors, as people sometimes add spaces at the end of titles causing my macro to crash

so, before running I was attempting to make sure the sheet - which the name never changes - would be selected before executing to avoid it running on the wrong sheet.
VBA Code:
Sub MrExcelExample()
    Dim r As Range: Set r = Rows("8:9")

    Sheets("Sheet1").Select
    ActiveSheet.Unprotect
    r.Value = Application.Trim(r)

End Sub

If I remove the "Sheets("Sheet1").select" code it'll run fine, and if I use that code on its own macro it also runs fine.

Here's the error pop-up I receive when its added before the application.Trim(r)
1664055135971.png
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I cannot reproduce the error. Your posted code when run on a locked sheet does not produce any errors for me.
 
Upvote 0
I cannot reproduce the error. Your posted code when run on a locked sheet does not produce any errors for me.
if i'm already on that sheet, then i encounter no error, but if i happen on sheet 2, then the error occurs. Workbook is not producted, nor is the sheet.
 
Upvote 0
Seems
Excel Formula:
Dim r As Range: Set r = Sheets("Sheet1").Rows("8:9")
was all that I needed to add. As to why I'm not certain, but appears to be the ticket.
 
Upvote 0
I don't see how that is possible. The marked statement bloiw
Seems
Excel Formula:
Dim r As Range: Set r = Sheets("Sheet1").Rows("8:9")
was all that I needed to add. As to why I'm not certain, but appears to be the ticket.

Because if you start on Sheet2, then the range r is for Sheet2 cells, and if Sheet2 is protected you are going to get that error when you try to trim the range r cells.

This should also work.

VBA Code:
Sub MrExcelExample()
    Dim r As Range

    With Sheets("Sheet1")
         Set r = .Rows("8:9")
        .Unprotect
    End With
    r.Value = Application.Trim(r.Value)
End Sub
 
Upvote 0
Solution
I don't see how that is possible. The marked statement bloiw


Because if you start on Sheet2, then the range r is for Sheet2 cells, and if Sheet2 is protected you are going to get that error when you try to trim the range r cells.

This should also work.

VBA Code:
Sub MrExcelExample()
    Dim r As Range

    With Sheets("Sheet1")
         Set r = .Rows("8:9")
        .Unprotect
    End With
    r.Value = Application.Trim(r.Value)
End Sub
Makes since. Didn't think of it like that. Appreciate it!
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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