VBA-Replace Page with a Page in same Workbook

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
831
Office Version
  1. 365
Platform
  1. Windows
Thanks for looking at my post.
I would like to have some code so I can do the following. I would like to have a box pop up with the option to replace a random page in my work book by entering the page number in dialog box. I want the page that will be replacing the old one to be renamed to old page name.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
When you say "Page" I assume you mean "Sheet"

So you want to enter a Sheet number.
And that sheet will be deleted and the a new sheet will be created given the same sheet name as the one we deleted.

Now I hope you know sheet number means:

As you look at the Tab bar. Sheet(1) is the far sheet to the left. and sheet (2) is the next sheet to the right

So if you enter "10" into a InputBox sheet(10) will be deleted not necessarily a sheet name "Sheet10"

Try This:

Code:
Sub Delete_Sheet()
On Error GoTo M
Dim ans As Long
ans = InputBox("Enter sheet nunber you want deleted")
anss = Sheets(ans).Name
Sheets(ans).Delete
Sheets.Add(After:=Sheets(Sheets.Count)).Name = anss
Exit Sub
M:
MsgBox "No sheet numbered " & ans & "  exist" & vbNewLine & " Or this sheet name may already exist"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
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