Hi all!
I have a problem which is really troubling me! If someone could please help, I would really appreciate it!
Overview:
I have a range of different workbooks which I need to insert a new column to and include a specific ID which is based on the specific workbook name.3
Objective:
- Insert new column in B:B with name ID_Name in B1 (I have this code already)
- Paste the name of the workbook from cell B2:Last cell in that column (But I want to paste a shortened name of the workbook). (The challenge for me)
My workbooks all have the name structure: ABCD_File_Date_Version or ABCDE_File_Date_Version or ABCDEF_File_Date_Version
So I just want to paste everything before the underscore '_' for that specific workbook when I run the macro.
i.e. I just need ABCD or ABCDE or ABCDEF depending on the file.
My code is in my personal macro workbook, and it works very well so far, but I can't get it to paste the name of the workbook according to my requirement.
Here is my code so far:
If anyone has some ideas, please let me know.
Thank you Excel Community!
I have a problem which is really troubling me! If someone could please help, I would really appreciate it!
Overview:
I have a range of different workbooks which I need to insert a new column to and include a specific ID which is based on the specific workbook name.3
Objective:
- Insert new column in B:B with name ID_Name in B1 (I have this code already)
- Paste the name of the workbook from cell B2:Last cell in that column (But I want to paste a shortened name of the workbook). (The challenge for me)
My workbooks all have the name structure: ABCD_File_Date_Version or ABCDE_File_Date_Version or ABCDEF_File_Date_Version
So I just want to paste everything before the underscore '_' for that specific workbook when I run the macro.
i.e. I just need ABCD or ABCDE or ABCDEF depending on the file.
My code is in my personal macro workbook, and it works very well so far, but I can't get it to paste the name of the workbook according to my requirement.
Here is my code so far:
VBA Code:
Sub AddCol()
'Add a new column in B:B with ID_Name
Dim ws As Worksheet
Dim lastRow As Long
lastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
If Range("B1") = "ID_Name" Then
Else
For Each ws In ActiveWorkbook.Sheets
ws.Range("B:B").EntireColumn.Insert
ws.Range("B1").Value = "ID_Name"
With ws
.Range(Cells(1, 2), Cells(lastRow, 1)).Value = ActiveWorkbook.Name
End With
With ThisWorkbook.Worksheets
ws.Range("B:B").NumberFormat = "Text"
End With
Next ws
End If
End Sub
If anyone has some ideas, please let me know.
Thank you Excel Community!