Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 122
Default Append Worksheet name to each begining of each row with values

Excel 2007

Hello,

I need to create a macro that will go through each worksheet and append the
worksheet name to the begining of each row if there is a value. Here is what
I have so far. This runs but takes my last worksheet name and appends that
name to the rows in my first worksheet instead of appending my first
worksheet name.

Sub Append()

Dim LastRow As Long
Dim cl As Range
Dim ws As Worksheet

For I = 1 To Worksheets.Count
With Worksheets(1)
Set ws = Worksheets(I)
.Cells(I, 1).Value = ws.Name
End With
Next I

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

For Each cl In Range(Cells(1, 1), Cells(LastRow, 1))
If Not IsEmpty(cl) Then
cl.Value = ws.Name & "." & cl.Value
End If
Next cl

End Sub
--
Thanks for the help,

Glenn
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Append Worksheet name to each begining of each row with values

First, I wouldn't use Append as the procedure name. VBA uses it in some file
statements.

Option Explicit
Sub myAppend()

Dim LastRow As Long
Dim i As Long
Dim cl As Range

For i = 1 To Worksheets.Count
With Worksheets(i)
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For Each cl In .Range("A1", .Cells(LastRow, "A")).Cells
If IsEmpty(cl) Then
'skip it
Else
cl.Value = .Name & "." & cl.Value
End If
Next cl
End With
Next i
End Sub

Glenn wrote:

Excel 2007

Hello,

I need to create a macro that will go through each worksheet and append the
worksheet name to the begining of each row if there is a value. Here is what
I have so far. This runs but takes my last worksheet name and appends that
name to the rows in my first worksheet instead of appending my first
worksheet name.

Sub Append()

Dim LastRow As Long
Dim cl As Range
Dim ws As Worksheet

For I = 1 To Worksheets.Count
With Worksheets(1)
Set ws = Worksheets(I)
.Cells(I, 1).Value = ws.Name
End With
Next I

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

For Each cl In Range(Cells(1, 1), Cells(LastRow, 1))
If Not IsEmpty(cl) Then
cl.Value = ws.Name & "." & cl.Value
End If
Next cl

End Sub
--
Thanks for the help,

Glenn


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Append Worksheet name to each begining of each row with values

You were kinda close. This should do what you were asking. Hope this helps!
If so, let me know, click "YES" below.

Option Explicit

Sub Append()

Dim wks As Worksheet
Dim LastRow As Long
Dim cell As Range

For Each wks In Sheets
LastRow = wks.Cells(Rows.Count, "A").End(xlUp).Row

For Each cell In wks.Range("A1:A" & LastRow)
If Not IsEmpty(cell) Then
cell.Value = wks.Name & "." & cell.Value
End If
Next cell
Next wks

End Sub
--
Cheers,
Ryan


"Glenn" wrote:

Excel 2007

Hello,

I need to create a macro that will go through each worksheet and append the
worksheet name to the begining of each row if there is a value. Here is what
I have so far. This runs but takes my last worksheet name and appends that
name to the rows in my first worksheet instead of appending my first
worksheet name.

Sub Append()

Dim LastRow As Long
Dim cl As Range
Dim ws As Worksheet

For I = 1 To Worksheets.Count
With Worksheets(1)
Set ws = Worksheets(I)
.Cells(I, 1).Value = ws.Name
End With
Next I

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

For Each cl In Range(Cells(1, 1), Cells(LastRow, 1))
If Not IsEmpty(cl) Then
cl.Value = ws.Name & "." & cl.Value
End If
Next cl

End Sub
--
Thanks for the help,

Glenn

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Append values and remove text PurpleMilk Excel Worksheet Functions 2 December 14th 09 07:57 PM
append worksheet with data from another Mark B Excel Worksheet Functions 3 May 8th 08 06:19 PM
how can I Append values to an existing excel file. viv New Users to Excel 0 March 21st 07 11:25 AM
Append Text to Cell Values Using Replace Ngan Excel Discussion (Misc queries) 4 June 4th 05 08:30 PM
re-naming worksheet tab adds ".xls]" to the begining of the name I entered Me Excel Worksheet Functions 3 November 15th 04 09:01 PM


All times are GMT +1. The time now is 11:08 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"