Run Time Error VBA

anand3dinesh

Board Regular
Joined
Dec 19, 2019
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Hi There,

please can anyone tell me what is wrong with my code.

i need this code to do the job to fix the sheetname in a selected cell and this works fine. but when click cancel it should come out from the sub, but it gives runtime error.
your reply will be much appriciatted


Private Sub FixSheetName_Click()

Dim sh As Worksheet
Dim rng As Range
Set rng = Application.InputBox(prompt:="Select Cell to Fix Name", Title:="Select Cell", Type:=8)

If Not rng Is Nothing Then
For Each sh In Sheets
sh.Unprotect
sh.Range(rng.Address).Value = sh.Name
Next sh
Else
Exit Sub

End If
End Sub

Thanks in Advance.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about
VBA Code:
Private Sub FixSheetName_Click()
   Dim sh As Worksheet
   Dim rng As Range
   
   On Error Resume Next
   Set rng = Application.InputBox("Select Cell to Fix Name", "Select Cell", , , , , , 8)
   On Error GoTo 0
   If Not rng Is Nothing Then
      For Each sh In Sheets
         sh.Unprotect
         sh.Range(rng.Address).Value = sh.Name
      Next sh
   End If
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,039
Latest member
Mbone Mathonsi

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