+ Reply to Thread
Results 1 to 2 of 2

How to rename filename base on filename contains

  1. #1
    Registered User
    Join Date
    01-08-2021
    Location
    India
    MS-Off Ver
    2010
    Posts
    1

    Red face How to rename filename base on filename contains

    I have folder “A”. In this folder I have 12 excel file with only one sheet i.e. “Sheet1”.
    Excel file name is like 01_ABCD_C01 April, 01_ABCD_C01 May ………01_ABCD_C01 December etc.
    Here C01 is vendor ID
    I am extracting “Sheet1” of every excel file in one WorkBook and change sheet1 name base on cell value F6 and save file in C folder.
    I am doing this task for every vendor. I am using following code for this.
    Now my question is : when I save excel file in C folder then the name of extracted file is base (01_ABCD_C01 April) on client id. The every excel file name contains C01 is a client ID.

    Sub ImportFiles()
    Dim wbOpen As Workbook
    Dim wbNew As Workbook
    Dim fName As String
    'Change Path
    Const strPath As String = "C:\Users\s\Desktop\A\"
    Dim strExtension As String
    MsgBox ("Do you Want to Extract Files?")
    'Comment out the 3 lines below to debug
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    On Error Resume Next
    ChDir strPath
    'Change extension
    strExtension = Dir("*.xls")
    Set wbNew = Workbooks.Add
    'Change Path, Name and File Format
    wbNew.SaveAs Filename:="C:\Users\s\Desktop\C\Consolidation", FileFormat:=xlWorkbookNormal
    Do While strExtension <> ""
    Set wbOpen = Workbooks.Open(strPath & strExtension)
    With wbOpen
    .Sheets("Sheet1").Copy After:=wbNew.Sheets(wbNew.Sheets.Count)
    ActiveSheet.Name = ActiveSheet.Range("F6")
    wbNew.Sheets(wbNew.Sheets.Count).Name = wbNew.Sheets(wbNew.Sheets.Count).Cells(1, 1)
    .Close SaveChanges:=False
    End With
    strExtension = Dir
    Loop
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    On Error GoTo 0
    MsgBox ("Files Extracted Succussfully")
    fName = Dir("C:\Users\s\Desktop\A\*.*")
    Do While fName <> ""
    If fName <> "Master1.xls" And fName <> "Master2.xls" Then 'or .txt or .csv or whatever
    Kill "C:\Users\s\Desktop\A\" & fName
    End If
    fName = Dir
    Loop
    End Sub

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166
    Hello Newton01,

    Welcome to Excelforum. Be a part of large Excel community. Enjoy Learning.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Automatically rename worksheet based on filename
    By cyee in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-04-2017, 01:56 AM
  2. [SOLVED] Rename a special filename
    By Jan Lichtenbelt in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 02-28-2016, 01:49 PM
  3. [SOLVED] Runtime 1004 after prompting for filename but not when filename hard coded.
    By cgoodenough in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-04-2013, 03:46 AM
  4. move anyfile base on filename
    By zhaype in forum Excel General
    Replies: 0
    Last Post: 07-16-2013, 02:23 AM
  5. [SOLVED] How to create Pdf filename base on ws Range.Value
    By tuongtu3 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-10-2012, 11:28 PM
  6. [SOLVED] To rename a workbook filename with the column value of a worksheet.
    By Vinutha in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-18-2012, 04:29 AM
  7. Replies: 3
    Last Post: 07-09-2012, 03:09 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1