Pasting Sheet Title as first column

khawarameer

Board Regular
Joined
Jun 2, 2009
Messages
152
I have work book with sevaral sheets. The contain identical columns. I want the sheet titles to be inserted as first columns in the respective sheets. Can any one give me the code for this.

Regards
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
In cell A1 enter the formula
Code:
=CELL("filename",D1)
(Reference to D1 could be to any cell on the sheet) - this will give the full file path, workbook and sheet name (once the file has been saved)
In cell B1 enter the formula
Code:
=RIGHT(A1,(LEN(A1)-(FIND("]",A1,1))))
This will return the sheet name only from the formula in cell A1 i.e. all characters after the ] character.
 
Upvote 0
Thanks Stuart it was great.

But I was hoping for a VBA Code. You see i have lot of sheets and i just cant insert a column in every sheet and paste the formula and then apply to all rows (It will take a long time). Instead a VBA Code will be handy to do all that. Can you provide me with a Code.
 
Upvote 0
Hi,

I have tried to make a VBA Code using Macro Recorder and some modifications. Here is the Code;

Sub Test()
Dim Lastrow As Long, SheetNum As Integer, i As Integer

For i = 1 To SheetNum
Sheets(i).Select
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A2").Select
ActiveCell.FormulaR1C1 = _
"=RIGHT(CELL(""filename""),(LEN(CELL(""filename""))-(FIND(""]"",CELL(""filename""),1))))"
Range("A2").Select
Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Selection.AutoFill Destination:=Range("A2:A" & Lastrow)
Next i
End Sub

when i run this code it gives me this error;

Run-time error '1004':
AutoFill method of Range cass failed

What could be the fault.
 
Upvote 0
What are you actually trying to do?

Why have you based LastRow on a column you've inserted, which will presumamby be blank apart from the formula you enter in A2?
 
Upvote 0
My aim is to Obtain Lastrow that contains data in each sheet. actually I have several sheet in my work book. Though columns are same in each sheet but rows are different therefore a constant value for rows will not work. that's why i placed this Lastrow variable to obtain last row containing data.
 
Upvote 0
But you appear to be basing the last row on a column that is inserted via code.:eek:
 
Upvote 0
I have changed the code a little bit. Now it is working (i.e. Copying Sheet Title in the first column). This is the code.

Sub Test()
Dim Lastrow As Long, SheetNum As Integer, i As Integer

SheetNum = Worksheets.Count
For i = 1 To SheetNum
Sheets(i).Select
Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("Sheets(i)!a2").Select
ActiveCell.FormulaR1C1 = _
"=RIGHT(CELL(""filename""),(LEN(CELL(""filename""))-(FIND(""]"",CELL(""filename""),1))))"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A" & Lastrow)
Next i
End Sub

Now i have another problem. Once i run this macro the title of active sheet is copied in all sheets. I think the formula to copy sheet titles need to be changed.

ActiveCell.FormulaR1C1 = _
"=RIGHT(CELL(""filename""),(LEN(CELL(""filename""))-(FIND(""]"",CELL(""filename""),1))))"

But i don't know any other suitable replacement for that formula. Can any one help me now.

 
Upvote 0
Why are you using the formula in the first place?

Why can't you just get the worksheet name using Sheets(i).Name?
Code:
Sub Test()
Dim ws As Worksheet
Dim Lastrow As Long, SheetNum As Long, I As Long
 
    SheetNum = Worksheets.Count
 
    For I = 1 To SheetNum

        Set ws = Sheets(I)
        With ws

            Lastrow = .Cells(Rows.Count, 1).End(xlUp).Row

            .Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

            .Range("A2:A" & Lastrow) = ws.Name
        End With
    Next I
 
End Sub
PS I still don't see how you can use the inserted column to find the last row of data, perhaps I'm missing something.:)
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

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