VBA-Replace Page with a Page in same Workbook

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
821
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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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,215,124
Messages
6,123,187
Members
449,090
Latest member
bes000

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