PC Review


Reply
Thread Tools Rate Thread

Copy 3 columns become one column with macro

 
 
Lyn
Guest
Posts: n/a
 
      6th Sep 2008
Dear Expert,

I have 3 columns data (for example column A, B and C) and I would like copy
to become one column data only, the example as below:

A B C D
Tomato 1 a Tomato
Sugar 2 b Sugar
3 c 1
d 2
3
a
b
c
d

Could you please advice me

Regards
Lyn

 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      6th Sep 2008
Sub CombineColumns()

'Copy column A to Column d
Columns("A").Copy Destination:=Columns("D")

NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
LastRowB = Range("B" & Rows.Count).End(xlUp).Row
Set CopyRange = Range("B1:B" & LastRowB)
CopyRange.Copy Destination:=Range("D" & NewRowD)

NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
LastRowC = Range("C" & Rows.Count).End(xlUp).Row
Set CopyRange = Range("C1:C" & LastRowC)
CopyRange.Copy Destination:=Range("D" & NewRowD)


End Sub

"Lyn" wrote:

> Dear Expert,
>
> I have 3 columns data (for example column A, B and C) and I would like copy
> to become one column data only, the example as below:
>
> A B C D
> Tomato 1 a Tomato
> Sugar 2 b Sugar
> 3 c 1
> d 2
> 3
> a
> b
> c
> d
>
> Could you please advice me
>
> Regards
> Lyn
>

 
Reply With Quote
 
Lyn
Guest
Posts: n/a
 
      6th Sep 2008
HI Joel,

This is perfect formula, and thanks a lot.
If you not mind, I have another question. I would like put data for example
in column A and in the column D will be automatically update also. Cause with
this formula I have to close my excel file and open again to update data in
column D
Could you please advice this one.

Regards
lyn


"Joel" wrote:

> Sub CombineColumns()
>
> 'Copy column A to Column d
> Columns("A").Copy Destination:=Columns("D")
>
> NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> LastRowB = Range("B" & Rows.Count).End(xlUp).Row
> Set CopyRange = Range("B1:B" & LastRowB)
> CopyRange.Copy Destination:=Range("D" & NewRowD)
>
> NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> LastRowC = Range("C" & Rows.Count).End(xlUp).Row
> Set CopyRange = Range("C1:C" & LastRowC)
> CopyRange.Copy Destination:=Range("D" & NewRowD)
>
>
> End Sub
>
> "Lyn" wrote:
>
> > Dear Expert,
> >
> > I have 3 columns data (for example column A, B and C) and I would like copy
> > to become one column data only, the example as below:
> >
> > A B C D
> > Tomato 1 a Tomato
> > Sugar 2 b Sugar
> > 3 c 1
> > d 2
> > 3
> > a
> > b
> > c
> > d
> >
> > Could you please advice me
> >
> > Regards
> > Lyn
> >

 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      6th Sep 2008
You could make the code a worksheet change and clear column D before the code
is run

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Columns("D").ClearContents

'Copy column A to Column d
Columns("A").Copy Destination:=Columns("D")

NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
LastRowB = Range("B" & Rows.Count).End(xlUp).Row
Set CopyRange = Range("B1:B" & LastRowB)
CopyRange.Copy Destination:=Range("D" & NewRowD)

NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
LastRowC = Range("C" & Rows.Count).End(xlUp).Row
Set CopyRange = Range("C1:C" & LastRowC)
CopyRange.Copy Destination:=Range("D" & NewRowD)

Application.EnableEvents = True
End Sub


"Lyn" wrote:

> HI Joel,
>
> This is perfect formula, and thanks a lot.
> If you not mind, I have another question. I would like put data for example
> in column A and in the column D will be automatically update also. Cause with
> this formula I have to close my excel file and open again to update data in
> column D
> Could you please advice this one.
>
> Regards
> lyn
>
>
> "Joel" wrote:
>
> > Sub CombineColumns()
> >
> > 'Copy column A to Column d
> > Columns("A").Copy Destination:=Columns("D")
> >
> > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > LastRowB = Range("B" & Rows.Count).End(xlUp).Row
> > Set CopyRange = Range("B1:B" & LastRowB)
> > CopyRange.Copy Destination:=Range("D" & NewRowD)
> >
> > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > LastRowC = Range("C" & Rows.Count).End(xlUp).Row
> > Set CopyRange = Range("C1:C" & LastRowC)
> > CopyRange.Copy Destination:=Range("D" & NewRowD)
> >
> >
> > End Sub
> >
> > "Lyn" wrote:
> >
> > > Dear Expert,
> > >
> > > I have 3 columns data (for example column A, B and C) and I would like copy
> > > to become one column data only, the example as below:
> > >
> > > A B C D
> > > Tomato 1 a Tomato
> > > Sugar 2 b Sugar
> > > 3 c 1
> > > d 2
> > > 3
> > > a
> > > b
> > > c
> > > d
> > >
> > > Could you please advice me
> > >
> > > Regards
> > > Lyn
> > >

 
Reply With Quote
 
Lyn
Guest
Posts: n/a
 
      6th Sep 2008
HI Joel,

This greats...
when i put data in column B or C, data from column A on column D disappear.
I have to save and close from xls sheet to update my data.
Please advice again.

Regards
lyn

"Joel" wrote:

> You could make the code a worksheet change and clear column D before the code
> is run
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> Application.EnableEvents = False
> Columns("D").ClearContents
>
> 'Copy column A to Column d
> Columns("A").Copy Destination:=Columns("D")
>
> NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> LastRowB = Range("B" & Rows.Count).End(xlUp).Row
> Set CopyRange = Range("B1:B" & LastRowB)
> CopyRange.Copy Destination:=Range("D" & NewRowD)
>
> NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> LastRowC = Range("C" & Rows.Count).End(xlUp).Row
> Set CopyRange = Range("C1:C" & LastRowC)
> CopyRange.Copy Destination:=Range("D" & NewRowD)
>
> Application.EnableEvents = True
> End Sub
>
>
> "Lyn" wrote:
>
> > HI Joel,
> >
> > This is perfect formula, and thanks a lot.
> > If you not mind, I have another question. I would like put data for example
> > in column A and in the column D will be automatically update also. Cause with
> > this formula I have to close my excel file and open again to update data in
> > column D
> > Could you please advice this one.
> >
> > Regards
> > lyn
> >
> >
> > "Joel" wrote:
> >
> > > Sub CombineColumns()
> > >
> > > 'Copy column A to Column d
> > > Columns("A").Copy Destination:=Columns("D")
> > >
> > > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > > LastRowB = Range("B" & Rows.Count).End(xlUp).Row
> > > Set CopyRange = Range("B1:B" & LastRowB)
> > > CopyRange.Copy Destination:=Range("D" & NewRowD)
> > >
> > > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > > LastRowC = Range("C" & Rows.Count).End(xlUp).Row
> > > Set CopyRange = Range("C1:C" & LastRowC)
> > > CopyRange.Copy Destination:=Range("D" & NewRowD)
> > >
> > >
> > > End Sub
> > >
> > > "Lyn" wrote:
> > >
> > > > Dear Expert,
> > > >
> > > > I have 3 columns data (for example column A, B and C) and I would like copy
> > > > to become one column data only, the example as below:
> > > >
> > > > A B C D
> > > > Tomato 1 a Tomato
> > > > Sugar 2 b Sugar
> > > > 3 c 1
> > > > d 2
> > > > 3
> > > > a
> > > > b
> > > > c
> > > > d
> > > >
> > > > Could you please advice me
> > > >
> > > > Regards
> > > > Lyn
> > > >

 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      6th Sep 2008
Events are somehow getting disabled. Try the code below to re-enable Events
without closing the book. Are there any other macros that can be failing?

Cechk your settings in VBA

Tools - OPtions - General - Error Trapping. Set to break on All Errors.

Sub EnableEvent()
Application.EnableEvents = True

End Sub


"Lyn" wrote:

> HI Joel,
>
> This greats...
> when i put data in column B or C, data from column A on column D disappear.
> I have to save and close from xls sheet to update my data.
> Please advice again.
>
> Regards
> lyn
>
> "Joel" wrote:
>
> > You could make the code a worksheet change and clear column D before the code
> > is run
> >
> > Private Sub Worksheet_Change(ByVal Target As Range)
> > Application.EnableEvents = False
> > Columns("D").ClearContents
> >
> > 'Copy column A to Column d
> > Columns("A").Copy Destination:=Columns("D")
> >
> > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > LastRowB = Range("B" & Rows.Count).End(xlUp).Row
> > Set CopyRange = Range("B1:B" & LastRowB)
> > CopyRange.Copy Destination:=Range("D" & NewRowD)
> >
> > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > LastRowC = Range("C" & Rows.Count).End(xlUp).Row
> > Set CopyRange = Range("C1:C" & LastRowC)
> > CopyRange.Copy Destination:=Range("D" & NewRowD)
> >
> > Application.EnableEvents = True
> > End Sub
> >
> >
> > "Lyn" wrote:
> >
> > > HI Joel,
> > >
> > > This is perfect formula, and thanks a lot.
> > > If you not mind, I have another question. I would like put data for example
> > > in column A and in the column D will be automatically update also. Cause with
> > > this formula I have to close my excel file and open again to update data in
> > > column D
> > > Could you please advice this one.
> > >
> > > Regards
> > > lyn
> > >
> > >
> > > "Joel" wrote:
> > >
> > > > Sub CombineColumns()
> > > >
> > > > 'Copy column A to Column d
> > > > Columns("A").Copy Destination:=Columns("D")
> > > >
> > > > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > > > LastRowB = Range("B" & Rows.Count).End(xlUp).Row
> > > > Set CopyRange = Range("B1:B" & LastRowB)
> > > > CopyRange.Copy Destination:=Range("D" & NewRowD)
> > > >
> > > > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > > > LastRowC = Range("C" & Rows.Count).End(xlUp).Row
> > > > Set CopyRange = Range("C1:C" & LastRowC)
> > > > CopyRange.Copy Destination:=Range("D" & NewRowD)
> > > >
> > > >
> > > > End Sub
> > > >
> > > > "Lyn" wrote:
> > > >
> > > > > Dear Expert,
> > > > >
> > > > > I have 3 columns data (for example column A, B and C) and I would like copy
> > > > > to become one column data only, the example as below:
> > > > >
> > > > > A B C D
> > > > > Tomato 1 a Tomato
> > > > > Sugar 2 b Sugar
> > > > > 3 c 1
> > > > > d 2
> > > > > 3
> > > > > a
> > > > > b
> > > > > c
> > > > > d
> > > > >
> > > > > Could you please advice me
> > > > >
> > > > > Regards
> > > > > Lyn
> > > > >

 
Reply With Quote
 
Lyn
Guest
Posts: n/a
 
      7th Sep 2008
Hi Joel,

Perfect, thanks a lot

regards
lyn

"Joel" wrote:

> Events are somehow getting disabled. Try the code below to re-enable Events
> without closing the book. Are there any other macros that can be failing?
>
> Cechk your settings in VBA
>
> Tools - OPtions - General - Error Trapping. Set to break on All Errors.
>
> Sub EnableEvent()
> Application.EnableEvents = True
>
> End Sub
>
>
> "Lyn" wrote:
>
> > HI Joel,
> >
> > This greats...
> > when i put data in column B or C, data from column A on column D disappear.
> > I have to save and close from xls sheet to update my data.
> > Please advice again.
> >
> > Regards
> > lyn
> >
> > "Joel" wrote:
> >
> > > You could make the code a worksheet change and clear column D before the code
> > > is run
> > >
> > > Private Sub Worksheet_Change(ByVal Target As Range)
> > > Application.EnableEvents = False
> > > Columns("D").ClearContents
> > >
> > > 'Copy column A to Column d
> > > Columns("A").Copy Destination:=Columns("D")
> > >
> > > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > > LastRowB = Range("B" & Rows.Count).End(xlUp).Row
> > > Set CopyRange = Range("B1:B" & LastRowB)
> > > CopyRange.Copy Destination:=Range("D" & NewRowD)
> > >
> > > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > > LastRowC = Range("C" & Rows.Count).End(xlUp).Row
> > > Set CopyRange = Range("C1:C" & LastRowC)
> > > CopyRange.Copy Destination:=Range("D" & NewRowD)
> > >
> > > Application.EnableEvents = True
> > > End Sub
> > >
> > >
> > > "Lyn" wrote:
> > >
> > > > HI Joel,
> > > >
> > > > This is perfect formula, and thanks a lot.
> > > > If you not mind, I have another question. I would like put data for example
> > > > in column A and in the column D will be automatically update also. Cause with
> > > > this formula I have to close my excel file and open again to update data in
> > > > column D
> > > > Could you please advice this one.
> > > >
> > > > Regards
> > > > lyn
> > > >
> > > >
> > > > "Joel" wrote:
> > > >
> > > > > Sub CombineColumns()
> > > > >
> > > > > 'Copy column A to Column d
> > > > > Columns("A").Copy Destination:=Columns("D")
> > > > >
> > > > > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > > > > LastRowB = Range("B" & Rows.Count).End(xlUp).Row
> > > > > Set CopyRange = Range("B1:B" & LastRowB)
> > > > > CopyRange.Copy Destination:=Range("D" & NewRowD)
> > > > >
> > > > > NewRowD = Range("D" & Rows.Count).End(xlUp).Row + 1
> > > > > LastRowC = Range("C" & Rows.Count).End(xlUp).Row
> > > > > Set CopyRange = Range("C1:C" & LastRowC)
> > > > > CopyRange.Copy Destination:=Range("D" & NewRowD)
> > > > >
> > > > >
> > > > > End Sub
> > > > >
> > > > > "Lyn" wrote:
> > > > >
> > > > > > Dear Expert,
> > > > > >
> > > > > > I have 3 columns data (for example column A, B and C) and I would like copy
> > > > > > to become one column data only, the example as below:
> > > > > >
> > > > > > A B C D
> > > > > > Tomato 1 a Tomato
> > > > > > Sugar 2 b Sugar
> > > > > > 3 c 1
> > > > > > d 2
> > > > > > 3
> > > > > > a
> > > > > > b
> > > > > > c
> > > > > > d
> > > > > >
> > > > > > Could you please advice me
> > > > > >
> > > > > > Regards
> > > > > > Lyn
> > > > > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to create a new workbook/ macro to copy columns TG Microsoft Excel Programming 0 11th Sep 2009 06:55 PM
Change a Macro - Copy in Columns instead of copy in Rows ytayta555 Microsoft Excel Programming 7 9th May 2009 06:32 PM
Want a macro for multiple columns on current macro for column Valerie Microsoft Excel Programming 4 23rd Feb 2009 07:30 PM
Macro to copy and paste values (columns)I have a macro file built C02C04 Microsoft Excel Programming 2 2nd May 2008 01:51 PM
copy two columns without blanks in the first column jeffm Microsoft Excel Programming 2 26th May 2006 03:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:05 AM.