DThompson89
New Member
- Joined
- Dec 15, 2023
- Messages
- 3
- Office Version
- 2021
- Platform
- Windows
I'm trying to move a worksheet from one workbook to another and rename it, but I need it at the end of the workbook. I'm calling a user form to select the workbook to move the sheet from and an input box to rename the sheet. Everything is functioning properly except it won't move the sheet to the end. See the code below. It always puts it as the 2nd sheet in the work book. It seems to be counting the sheets in wb1 instead of wb because wb1 is a csv file that always only has one sheet, but if I add a 2nd sheet to the csv file before running the code, it will move the sheet after the 2nd sheet of wb. Is there a way to move to the end other than Sheets.Count?
Sub Move_and_Rename_Sheets()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim wb1 As Workbook
Call Open_Files
Set wb1 = ActiveWorkbook
Dim ws As Worksheet
Dim iName As String
iName = InputBox("Enter the New Name for the Sheet")
If iName <> "" Then
wb1.ActiveSheet.Move After:=wb.Worksheets(Worksheets.Count)
ActiveSheet.Name = iName
End If
End Sub
Sub Move_and_Rename_Sheets()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim wb1 As Workbook
Call Open_Files
Set wb1 = ActiveWorkbook
Dim ws As Worksheet
Dim iName As String
iName = InputBox("Enter the New Name for the Sheet")
If iName <> "" Then
wb1.ActiveSheet.Move After:=wb.Worksheets(Worksheets.Count)
ActiveSheet.Name = iName
End If
End Sub