reference tab value on worksheet

J

jnewl

have a workbook with several sheets. want to insert a column in each
worksheet and populate that column with the value indicated on the tab. how
do i code in vb? thanks
 
D

Dave Peterson

Maybe something like:

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim myCol As Long
Dim LastRow As Long

myCol = 5
For Each wks In ActiveWorkbook.Worksheets
With wks
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Columns(myCol).Insert
.Range(.Cells(1, myCol), .Cells(LastRow, myCol)).Value = "'" & wks.Name
End With
Next wks

End Sub
 
J

jnewl

using office 2007. getting an error message on the .range statement.
getting run time error 1004 - method 'range' of 'object' _ worksheet' failed
 
J

jnewl

tried mycol = 1 and mycol = 5. same error each time.
none of the sheets were protected
want to insert one column in each worksheet.
 
D

Dave Peterson

Did you change any thing else in the code?

It worked fine for me.
tried mycol = 1 and mycol = 5. same error each time.
none of the sheets were protected
want to insert one column in each worksheet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top