+ Reply to Thread
Results 1 to 3 of 3

VBA - How to rename an Excel file based on the value in a Column

Hybrid View

  1. #1
    Registered User
    Join Date
    05-24-2010
    Location
    Rockville, MD
    MS-Off Ver
    Excel 2007
    Posts
    57

    VBA - How to rename an Excel file based on the value in a Column

    Hi,

    I'm trying to rename a file based on two values in column A (Cell A2) and in column B (Cell B2). In my Excel spreadsheet, it has 3 columns:

    Column A - Customer Name,
    Column B - Giving Date (11/30/2020)
    Column C - Giving Amount ($)

    Column A has ONLY 1 customer (John Doe)
    Column B has multiple dates (1/31/2020, 2/28/2020, 3/31/2020 etc.)

    I need the VBA to create a new file with a different name. It should be in this format "2020.11.30_John Doe_Giving Statement"

    The date must be the most recent date. Maybe sort it (descending) first?

    Big thanks for your help!

    -Dan

  2. #2
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,465

    Re: VBA - How to rename an Excel file based on the value in a Column

    Dhoang,

    Not tested, but try this:

    Option Explicit
    Dim LastRow As Long
    Dim max_dat As Date
    Dim ws As Worksheet
    
    Sub test()
    
    Set ws = ThisWorkbook.Worksheets("Sheet3") ' Change to your actual sheet:
    
    Find last row with date in Col B
    
        With ws
            f = .Cells(.Rows.Count, "B").End(xlUp).Row
    
    'Find latest date in the range
    
    max_dat = Application.WorksheetFunction.Max(ws.Range("B2:B" & f))
    
    'Use that in File Name:
    
    ActiveWorkbook.SaveAs Filename:=max_dat & "_" & range("A2").value & "_Giving Statement"
    
    End with
    
    End Sub
    Hope this helps

    Ochimus
    Last edited by Ochimus; 12-24-2020 at 02:14 PM.

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: VBA - How to rename an Excel file based on the value in a Column

    Hi there,

    See if the following code does what you need:

    
    
    
    Option Explicit
    
    
    Sub CreateWorkbook()
    
        Const sCOLUMN_DATES As String = "B"
        Const sCOLUMN_NAME  As String = "A"
        Const sFILE_SUFFIX  As String = ".xlsx"
        Const sSHEET_NAME   As String = "Sheet1"
    
        Dim dteLatestDate   As Date
        Dim sLatestDate     As String
        Dim rDateCells      As Range
        Dim sCustomer       As String
        Dim sFileName       As String
        Dim wks             As Worksheet
    
        Set wks = ThisWorkbook.Worksheets(sSHEET_NAME)
    
        With wks
    
            sCustomer = .Columns(sCOLUMN_NAME).Cells(1, 1).Value
    
            Set rDateCells = Intersect(.UsedRange, _
                                       .Columns(sCOLUMN_DATES))
    
            dteLatestDate = WorksheetFunction.Max(rDateCells)
    
            sLatestDate = Format(dteLatestDate, "yyyy.mm.dd")
    
            sFileName = sLatestDate & "_" & sCustomer & "_Giving Statement" & sFILE_SUFFIX
    
            wks.Copy
    
            ActiveWorkbook.SaveAs Filename:=sFileName
    
        End With
    
    End Sub
    The highlighted values may be altered to suit your requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Last edited by Greg M; 12-24-2020 at 02:37 PM.

+ 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. Copy a single file and rename based on cell value
    By zidinho in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-29-2019, 08:18 PM
  2. Split master file by column values/ rename PQ output file?
    By njs27 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-05-2019, 11:37 PM
  3. Split excel file having multipl sheets into multiple excel file based on column
    By Shaharyarwatto in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-22-2014, 05:02 AM
  4. Replies: 0
    Last Post: 01-21-2014, 10:52 AM
  5. Excel VBA find and replace string in non text file and rename file
    By razzack in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-01-2013, 02:43 PM
  6. [SOLVED] Macro to rename file based on tab name
    By punter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-14-2013, 03:37 PM
  7. Rename file based on current date
    By wira in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2005, 06:06 AM

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