Public Function XLfilename() As String
XLfilename = ThisWorkbook.Name
End Function
Cell Formulas | ||
---|---|---|
Range | Formula | |
H1:H13 | H1 | = XLfilename() |
Sub InsertFileNameInColumnH()
Dim ws As Worksheet
Dim lastRow As Long
Dim fileName As String
Set ws = ActiveSheet
fileName = ThisWorkbook.Name
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'Change "A" to your desired column to find last row
If lastRow > 0 Then
ws.Range("H1:H" & lastRow).Value = fileName
End If
End Sub
It depends where you put your code.Hi Iggy thanks for replying but it's copying the Workbook name (Personal.xlsm) instead of the individual File name (Prd.xlsx).
Can you please adapt that it use the File name
Sub InsertFileNameInColumnH()
Dim ws As Worksheet
Dim lastRow As Long
Dim fileName As String
Set ws = ActiveSheet
fileName = ActiveWorkbook.Name
lastRow = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row 'Change "H" to your desired column to find last row
If lastRow > 0 Then
ws.Range("H1:H" & lastRow).Value = fileName
End If
End Sub