Consulting

Results 1 to 3 of 3

Thread: Input box to select sheet then rename sheet

  1. #1
    VBAX Regular
    Joined
    Aug 2012
    Posts
    12
    Location

    Input box to select sheet then rename sheet

    Hi,

    I have this code and it does a search of a closed workbook and copy a sheet and then pastes in another workbook.

    But I want to rename the copied sheeet as "Whole" and I have tried many ways but none would work. Can anyone help?

    [vba]Sub fetch()

    ' This macro will import a file into this workbook
    Dim controlfile, pathname, tabname2, filename, tabname As Variant
    Dim inputpathname, inputtabname, newtabname, wb As Variant
    Dim inputfilename As String

    inputpathname = "C:\MEAS\dmd\LDC HV\Intra 2012"
    inputfilename = "WeeklyAveHV2012.xls"
    inputtabname = InputBox("Enter the tab which you want to copy" & "In Format MMMWhole, For Example: JunWhole")

    newtabname = inputtabname


    If Len(newtabname) = 0 Then
    MsgBox "No file name chosen"
    Exit Sub
    End If

    Set controlfile = ActiveWorkbook

    Set wb = Workbooks.Open(filename:=inputpathname & "\" & inputfilename, UpdateLinks:=0)

    wb.Sheets(inputtabname).Copy After:=controlfile.Sheets(1)
    wb.Close savechanges:=False


    'ActiveSheet.Name = newtabname

    controlfile.Sheets(1).Range("B4") = "Completed"

    ThisWorkbook.Close

    End Sub[/vba]
    Last edited by geebra; 09-21-2012 at 03:35 PM.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    insert this line;
    [VBA]ActiveSheet.Name = "Whole"[/VBA]

    after;
    [VBA]wb.Sheets(inputtabname).Copy After:=controlfile.Sheets(1) [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Or alternatively add this line
    [VBA]controlfile.Sheets(2).Name = "Whole"[/VBA]
    before
    [VBA]controlfile.Sheets(1).Range("B4") = "Completed"[/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •