File name

poikl

Active Member
Joined
Jun 8, 2002
Messages
490
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hi, can you please provide VBA code that will insert the excel FILE NAME all the way down in ColH?
 
You could paste this code into a separate Module:
VBA Code:
Public Function XLfilename() As String
   XLfilename = ThisWorkbook.Name
End Function

Then just use it as a regular formula anywhere you want, like that:
Cell Formulas
RangeFormula
H1:H13H1= XLfilename()


Would that be okay for you?
 
Upvote 0
VBA Code:
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
 
Upvote 0
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
 
Upvote 0
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
It depends where you put your code.
If, for example, you put your macro in your Personal Workbook it will write the Personal Workbook's name, but if you put it in your individual File it will write that name.

Use this adapted code instead:
VBA Code:
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

Although this method isn't fail-safe* it should provide the correct result in most cases.
*) ActiveWorkbook addresses the currently "active" or focussed Workbook while ThisWorkbook addresses the Workbook containing the VBA code
 
Upvote 0
Solution
Thanks Pete, now it actually showed the correct desired Work sheet name (PRD) but only on ROW1 and didn't extend down the column.
 
Upvote 0
Pete, sorry I didn't change "K" to "A" as you had noted in the macro. Once I changed that it worked perfectly as needed. Thanks again.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top