Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2008

Tip: Looking for answers? Try searching our database.

How to concatenate in macro across sheets

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gemz - 17 Jan 2008 11:29 GMT
I would like columns C & D to concatenate with a space and a comma seperating
the two words in each of my many sheets. the columns remain the same all the
time.

How do i do this? i tried using the concatenate formula in my macro code but
it didnt work as i dont really know how to refer to all sheets. Ideally i
would like a click of a button to concatenate C & D (with comma and space) in
each of my sheets.

thanks.
Mike H - 17 Jan 2008 11:41 GMT
Maybe this,

Alt + F11 to open vb editor. Double click 'This Workbook' and paste this in

Sub renameall()
'Scroll through worksheets
   Dim ws As Worksheet
   For Each ws In ThisWorkbook.Worksheets
   ws.Select
      Lastrow = Range("C65536").End(xlUp).Row
       Set myrange = Range("C1:C" & Lastrow)
           For Each c In myrange
               c.Offset(0, 2).Value = c.Value & "," & c.Offset(0, 1).Value
           Next
     
   Next ws
End Sub

Mike

> I would like columns C & D to concatenate with a space and a comma seperating
> the two words in each of my many sheets. the columns remain the same all the
[quoted text clipped - 6 lines]
>
> thanks.
Mike H - 17 Jan 2008 11:42 GMT
Missed you wanted a space so substitute this line

c.Offset(0, 2).Value = c.Value & " , " & c.Offset(0, 1).Value

Mike

> I would like columns C & D to concatenate with a space and a comma seperating
> the two words in each of my many sheets. the columns remain the same all the
[quoted text clipped - 6 lines]
>
> thanks.
Gemz - 17 Jan 2008 11:53 GMT
I tried that but get a big X and '400' error!

i selected 'this workbook' from the left hand side somwhere and pasted the
code there.

please advise..

> Missed you wanted a space so substitute this line
>
[quoted text clipped - 12 lines]
> >
> > thanks.
Mike H - 17 Jan 2008 12:00 GMT
Sorry that was my fault, I should have said
right click 'This workbook'
Insert module and paste the code into the new module.

Mike

> I tried that but get a big X and '400' error!
>
[quoted text clipped - 19 lines]
> > >
> > > thanks.
Gemz - 17 Jan 2008 12:06 GMT
I now get a run time error 1004 and it highlights "ws.Select".

i have the workbook open, is there something im doing wrong? does it matter
if each worksheet is named and not called sheet..?

sorry about this!

thanks for help

> Sorry that was my fault, I should have said
> right click 'This workbook'
[quoted text clipped - 25 lines]
> > > >
> > > > thanks.
Mike H - 17 Jan 2008 12:18 GMT
Gemz,

I can't replicate that error and no it doesn't matter what your sheets are
called. Looking at the code again it doesn't really matter if its in  a
module or a worksheet it should still work. I am confused by your comment
that 'I have the workbook open'. You have pasted the code into the workbook
you want to concatenate haven't you

Mike

> I now get a run time error 1004 and it highlights "ws.Select".
>
[quoted text clipped - 34 lines]
> > > > >
> > > > > thanks.
Gemz - 17 Jan 2008 12:25 GMT
thanks it works now but it takes a while to run.. there are like 20 sheets
but isnt there a way to speed up?

thanks.

> Gemz,
>
[quoted text clipped - 44 lines]
> > > > > >
> > > > > > thanks.
Mike H - 17 Jan 2008 12:29 GMT
Try

Application.screenupdating=false

the code

Application.screenupdating=true

> thanks it works now but it takes a while to run.. there are like 20 sheets
> but isnt there a way to speed up?
[quoted text clipped - 49 lines]
> > > > > > >
> > > > > > > thanks.
Gemz - 17 Jan 2008 12:41 GMT
This is how my code looks like now, it still took 2-3mins to run and froze
all other excel applications..

Sub concatenate1()
Application.ScreenUpdating = False
   Dim ws As Worksheet
   For Each ws In ThisWorkbook.Worksheets
   ws.Select
      Lastrow = Range("C65536").End(xlUp).Row
       Set myrange = Range("C1:C" & Lastrow)
           For Each c In myrange
               c.Offset(0, 2).Value = c.Value & " , " & c.Offset(0, 1).Value
           Application.ScreenUpdating = True

           Next
     
   Next ws
End Sub

thanks.

> Try
>
[quoted text clipped - 57 lines]
> > > > > > > >
> > > > > > > > thanks.
Mike H - 17 Jan 2008 12:58 GMT
Gemz,

I have a run of the mill laptop and my machine concatenated 100k records
across 3 worksheets in 12 seconds so it's one of a few things:-

You have a very large amount of records
Your PC processor time is being divided between this and other tasks
Your PC is slow

I don't see how the code can be significantly improved.

Mike

> This is how my code looks like now, it still took 2-3mins to run and froze
> all other excel applications..
[quoted text clipped - 78 lines]
> > > > > > > > >
> > > > > > > > > thanks.
Gemz - 17 Jan 2008 13:02 GMT
yes there is quite a large amount of data.

ok thanks for helping.

> Gemz,
>
[quoted text clipped - 91 lines]
> > > > > > > > > >
> > > > > > > > > > thanks.
Gemz - 17 Jan 2008 15:54 GMT
Sorry to bother you again but i have a quick question about the below, would
the macro work fine if the workbook is emailed across to someone else for
them to use the macro? they would probably assign the macro to a toolbar
button.. obviously the workbook would need to be opened for the macro to work?

thanks.

> This is how my code looks like now, it still took 2-3mins to run and froze
> all other excel applications..
[quoted text clipped - 78 lines]
> > > > > > > > >
> > > > > > > > > thanks.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.