Skip to main content

Conas ainm comhaid Excel a shábháil le stampa ama?

An ndearna tú iarracht riamh comhad Excel a shábháil leis an stampa ama reatha? Taispeánfaidh an t-alt seo modh duit chun é a bhaint amach.

Sábháil ainm comhaid Excel le stampa ama le cód VBA


mboilgeog cheart gorm saighead Sábháil ainm comhaid Excel le stampa ama le cód VBA

Is féidir leat an cód VBA thíos a rith chun ainm comhaid Excel a shábháil le stampa ama. Déan mar a leanas le do thoil.

1. Sa leabhar oibre is gá duit é a ainmniú de réir an stampa ama reatha, brúigh an Eile + F11 eochracha ag an am céanna chun an Fuinneog Microsoft Visual Basic for Applications.

2. Sa Microsoft Visual Basic d’Fheidhmchláir fuinneog, cliceáil le do thoil Ionsáigh > Modúil. Ansin cóipeáil an cód VBA thíos sa cód fuinneog. Féach an pictiúr:

Cód VBA: Sábháil ainm comhaid Excel le stampa ama (cuir stampa ama in ionad ainm an chomhaid)

Sub SaveAsFilenameWithTimestamp()
'Updated by Extendoffice 20191223
Dim xWb As Workbook
Dim xStrDate As String
Dim xFileName As Variant
Dim xFileDlg As FileDialog
Dim i As Variant
Application.DisplayAlerts = False
Set xWb = ActiveWorkbook
xStrDate = Format(Now, "yyyy-mm-dd hh-mm-ss")
If Right(xWb.Name, 4) = "xlsm" Then
  xFileName = Application.GetSaveAsFilename(xStrDate, "Excel Macro-Enabled Workbook (*.xlsm),*.xlsm")
Else
  xFileName = Application.GetSaveAsFilename(xStrDate, "Excel Workbook (*.xlsx),*.xlsx")
End If
If xFileName = False Then
Else
  xWb.SaveAs (xFileName)
End If
Application.DisplayAlerts = True
End Sub

3. Brúigh an F5 eochair chun an cód a rith. Ansin a Sábháil Mar tagann bosca dialóige aníos, is féidir leat an stampa ama a thaispeáint sa bhosca ainm comhaid. Sonraigh suíomh le do thoil chun an comhad seo a shábháil, agus ansin cliceáil ar an Sábháil cnaipe. Féach an pictiúr:

nótaí:

1. Sábhálfar an comhad nua Excel cruthaithe le hainm an stampa ama reatha go díreach.

2. Dóibh siúd a bhfuil comhad Excel acu cheana féin, cuirfear an stampa ama in ionad ainm bunaidh an chomhaid.

Mura dteastaíonn uait ach an stampa ama a chur leis agus an bunainm comhaid ina dhiaidh sin seachas é a athsholáthar, cuir an cód VBA thíos i bhfeidhm le do thoil.

Cód VBA: Sábháil ainm comhaid Excel le stampa ama (Cuir isteach stampa ama agus ainm an chomhaid bunaidh ina dhiaidh)

Sub AddTimestampToFileName()
'Updated by Extendoffice 20191223
Dim xWb As Workbook
Dim xStr As String
Dim xStrOldName As String
Dim xStrDate As String
Dim xFileName As Variant
Dim xFileDlg As FileDialog
Dim i As Variant
Application.DisplayAlerts = False
Set xWb = ActiveWorkbook
xStrOldName = xWb.Name
xStr = Left(xStrOldName, Len(xStrOldName) - 5)
xStrDate = Format(Now, "yyyy-mm-dd hh-mm-ss")
If Right(xStrOldName, 4) = "xlsm" Then
  xFileName = Application.GetSaveAsFilename(xStr & " " & xStrDate, "Excel Macro-Enabled Workbook (*.xlsm),*.xlsm")
Else
  xFileName = Application.GetSaveAsFilename(xStr & " " & xStrDate, "Excel Workbook (*.xlsx),*.xlsx")
End If
If xFileName = False Then
Else
  xWb.SaveAs (xFileName)
End If
Application.DisplayAlerts = True
End Sub

mboilgeog cheart gorm saigheadEarraí gaolmhara:

Uirlisí Táirgiúlachta Oifige is Fearr

🤖 Kutools AI Aide: anailís sonraí a réabhlóidiú bunaithe ar: Forghníomhú Chliste   |  Gin Cód  |  Cruthaigh Foirmlí Saincheaptha  |  Anailís a dhéanamh ar Sonraí agus Cairteacha a Ghin  |  Feidhmeanna Kutools a agairt...
Gnéithe Coitianta: Faigh, Aibhsigh nó Aithnigh Dúblaigh   |  Scrios Sraitheanna Bána   |  Comhcheangail Colúin nó Cealla gan Sonraí a Chailleadh   |   Babhta gan Foirmle ...
Cuardaigh Super: Ilchritéir VLookup    VLookup Illuachanna  |   VLookup Trasna Ilbhileoga   |   Amharc doiléir ....
Liosta anuas Casta: Go tapa Cruthaigh Liosta Anuas   |  Liosta anuas Cleithiúnach   |  Liosta Buail Isteach Ilroghnacha ....
Bainisteoir Colún: Cuir Líon Sonrach Colún leis  |  Colúin Bog  |  Scoránaigh Stádas Infheictheachta na gColún Ceilte  |  Déan comparáid idir Raonta & Colúin ...
Gnéithe Réadmhaoin: Fócas Eangaí   |  Amharc Dearaidh   |   Barra Mór na Foirmle    Leabhar Oibre & Bainisteoir Bileog   |  Leabharlann Acmhainní (Uaththéacs)   |  Piocálaí Dáta   |  Comhcheangail Bileoga Oibre   |  Criptigh/Díchriptigh Cealla    Seol Ríomhphost trí Liosta   |  Scagaire Super   |   Scagaire Speisialta (scagaire trom/iodálach/stailc tríd...) ...
Barr 15 Uirlisí12 Téacs uirlisí (Cuir Téacs, Bain Carachtair,...)   |   50 + Cairt cineálacha (Cairt Gantt,...)   |   40+ Praiticiúil Foirmlí (Ríomh aois bunaithe ar lá breithe,...)   |   19 Insertion uirlisí (Cuir isteach Cód QR, Ionsáigh Pictiúr ón gCosán,...)   |   12 Tiontú uirlisí (Uimhreacha le Focail, Comhshó Airgeadra,...)   |   7 Cumaisc & Scoilt uirlisí (Sraitheanna Comhcheangail Casta, Cealla Scoilt,...)   |   ... agus eile

Supercharge Do Scileanna Excel le Kutools le haghaidh Excel, agus Éifeachtúlacht Taithí Cosúil Ná Roimhe. Kutools le haghaidh Excel Tairiscintí Níos mó ná 300 Ardghnéithe chun Táirgiúlacht a Treisiú agus Sábháil Am.  Cliceáil anseo chun an ghné is mó a theastaíonn uait a fháil ...

Tuairisc


Tugann Tab Oifige comhéadan Tabbed chuig Office, agus Déan Do Obair i bhfad Níos Éasca

  • Cumasaigh eagarthóireacht agus léamh tabbed i Word, Excel, PowerPoint, Foilsitheoir, Rochtain, Visio agus Tionscadal.
  • Oscail agus cruthaigh cáipéisí iolracha i gcluaisíní nua den fhuinneog chéanna, seachas i bhfuinneoga nua.
  • Méadaíonn do tháirgiúlacht 50%, agus laghdaíonn sé na céadta cad a tharlaíonn nuair luch duit gach lá!
Comments (12)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
To whom it may concern,
I try to implied your code "VBA code: Save an Excel filename with timestamp (Insert timestamp within original file name)" at https://www.extendoffice.com/documents/excel/4413-excel-timestamp-filename.html. BUT it stop at new file name with timestamp Save As dialogue box. If I want to auto save continuous after Save As shown dialogue box, Close all workbook and close application. What VBA code you recommend to end this job? Please help.

Please answer me at misterxyz123@gmail
This comment was minimized by the moderator on the site
Hi Mr.X,
After running the code, all opened workbooks (except for the currently using workbook) are saved as new files with timestamp followed by the original file name then closed automatically. Please give it a try. Thank you.
Please specify a folder to save the files in this line: xPath = "C:\Users\Win10x64Test\Desktop\file with timestamp\"
Sub AddTimestampToFileName()
    'Updated by Extendoffice 20220826
    Dim xWb As Workbook
    Dim xStr As String
    Dim xPath As String
    Dim xStrDate As String
    Dim xFileName As String
    Application.DisplayAlerts = False
    
    
    xPath = "C:\Users\Win10x64Test\Desktop\file with timestamp\"
    xFileName = ActiveWorkbook.Name
    xStrDate = " " & Format(Now, "yyyy-mm-dd hh-mm-ss")
    For Each xWb In Workbooks
        If xWb.Name <> xFileName Then
            If Dir(xWb.FullName) <> "" Then
                xStr = Left(xWb.Name, InStrRev(xWb.Name, ".") - 1)
                xWb.SaveAs Filename:=xPath & Replace(xWb.Name, xStr, xStr & xStrDate)
            Else
                xWb.SaveAs Filename:=xPath & xWb.Name & xStrDate & ".xlsx"
            End If
        xWb.Close
        End If
    Next
    
    xStr = Left(xFileName, InStrRev(xFileName, ".") - 1)
    ActiveWorkbook.SaveAs Filename:=xPath & Replace(xFileName, xStr, xStr & xStrDate)
    Application.DisplayAlerts = True
    
End Sub
This comment was minimized by the moderator on the site
Is it possible to embed the code and tell it where to save the file rather than individually select the save path each time?
This comment was minimized by the moderator on the site
This works except when the user presses the cancel button, it still creates a file called "false".
This comment was minimized by the moderator on the site
Hi Jason,
Thank you for reminding me. The codes are updated.
This comment was minimized by the moderator on the site
Would it be possible to modify it to remove the previous timestamp and save it as in the same directory?
This comment was minimized by the moderator on the site
Good day,
Sorry can't help you with that.
This comment was minimized by the moderator on the site
You can't save files with : within the filename, if you change this to nbre = Format(Now, "yyyy-mm-dd hh-mm")
This comment was minimized by the moderator on the site
Dear Nathan,
The below VBA code can help to add the timestamp within the filename.

Sub test()
Dim xWb As Workbook
Dim xStr As String
Dim xStrOldName As String
Dim xStrDate As String
Dim xFileName As String
Dim xFileDlg As FileDialog
Dim i As Variant
Application.DisplayAlerts = False
Set xWb = ActiveWorkbook
xStrOldName = xWb.Name
xStr = Left(xStrOldName, Len(xStrOldName) - 5)
xStrDate = Format(Now, "yyyy-mm-dd hh-mm-ss")
If Right(xStrOldName, 4) = "xlsm" Then
xFileName = Application.GetSaveAsFilename(xStr & " " & xStrDate, "Excel Macro-Enabled Workbook (*.xlsm),*.xlsm")
Else
xFileName = Application.GetSaveAsFilename(xStr & " " & xStrDate, "Excel Workbook (*.xlsx),*.xlsx")
End If
xWb.SaveAs (xFileName)
Application.DisplayAlerts = True
End Sub
This comment was minimized by the moderator on the site
I have this below, which works perfect to save file with date stamp, BUT would not work if change to this to add time-> nbre = Format(Now, "yyyy-mm-dd hh:mm")


nbre = Format(Now, "yyyy-mm-dd")


Application.DisplayAlerts = False


ChDir "T:\Distribution Center"
ActiveWorkbook.SaveAs Filename:="T:\Distribution Center\2. BACKLOG\DC BACKLOG " + nbre + ".xlsx", FileFormat _
:=xlOpenXMLWorkbook, WriteResPassword:="****", CreateBackup:=False



Can you please help what is missning???....
This comment was minimized by the moderator on the site
You cannot use the character ":" in file names
This comment was minimized by the moderator on the site
Is there a version of this that works for Word documents?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations