Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default Copy Values Between Worksheets in same Workbook


Please help me create a macro that will have the source Worksheet as
Sheet1 and the destination worksheet as discount that corresponds to the
data listed below. Each row from Sheet1 corresponds to two rows in Discount
worksheet.

Every row that corresponds to Cost column (E) has a Cost Code = 0007346
and Discount column (K) has a Discount Code = 0007346 and this value place
in the Column G of the Discount worksheet.

The Sheet1 usually has 60 days of data.

Thanks,



Category
Cost Code = 0007234
Discount Code = 0007346


Worksheet(Sheet1)
Date(B06) Cost(E06) Discount(K06)
01/01/2003 5.67 1.32
01/02/2003 15.24 3.34
01/03/2003 12.13 2.09
....
03/01/2003 8.48 1.68

Worksheet(Discount)

Col(C) Date Col(F) Cost Col(G) Category
01/01/2003 5.67 0007234
01/01/2003 1.32 0007346
01/02/2003 15.24 0007234
01/02/2003 3.34 0007346
01/03/2003 12.13 0007234
01/03/2003 2.09 0007346
....
03/01/2003 8.48 0007234
03/01/2003 1.68 0007346


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Copy Values Between Worksheets in same Workbook

Sub move_to_discount()

Const CostCode = 7234
Const DiscountCode = 7346

With Sheets("Discount")
DisLastRow = .Cells(Rows.Count, "C").End(xlUp).Row
End With

DisRowCount = DisLastRow + 1

With Sheets("Sheet1")
Sh1LastRow = .Cells(Rows.Count, "B").End(xlUp).Row

For Sh1RowCount = 2 To Sh1LastRow
ItemDate = .Cells(Sh1RowCount, "B")
Cost = .Cells(Sh1RowCount, "E")
Discount = .Cells(Sh1RowCount, "K")

With Sheets("Discount")
.Cells(DisRowCount, "C") = ItemDate
.Cells(DisRowCount, "F") = Cost
.Cells(DisRowCount, "G") = CostCode
DisRowCount = DisRowCount + 1
.Cells(DisRowCount, "C") = ItemDate
.Cells(DisRowCount, "F") = Discount
.Cells(DisRowCount, "G") = DiscountCode
DisRowCount = DisRowCount + 1
End With

Next Sh1RowCount
End With

End Sub


"Joe K." wrote:


Please help me create a macro that will have the source Worksheet as
Sheet1 and the destination worksheet as discount that corresponds to the
data listed below. Each row from Sheet1 corresponds to two rows in Discount
worksheet.

Every row that corresponds to Cost column (E) has a Cost Code = 0007346
and Discount column (K) has a Discount Code = 0007346 and this value place
in the Column G of the Discount worksheet.

The Sheet1 usually has 60 days of data.

Thanks,



Category
Cost Code = 0007234
Discount Code = 0007346


Worksheet(Sheet1)
Date(B06) Cost(E06) Discount(K06)
01/01/2003 5.67 1.32
01/02/2003 15.24 3.34
01/03/2003 12.13 2.09
...
03/01/2003 8.48 1.68

Worksheet(Discount)

Col(C) Date Col(F) Cost Col(G) Category
01/01/2003 5.67 0007234
01/01/2003 1.32 0007346
01/02/2003 15.24 0007234
01/02/2003 3.34 0007346
01/03/2003 12.13 0007234
01/03/2003 2.09 0007346
...
03/01/2003 8.48 0007234
03/01/2003 1.68 0007346


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Copy Values Between Worksheets in same Workbook

Hi Joe,

I see that Joel has posted a reply while I was dragging my feet getting one
ready. From your previous post I assume that you are on a learning curve so
I'll post the code I came up with. (That does not mean that I am critical of
Joel's code because that is not the case. It is simply to show you another
option.)

Sub Copy_Data()

Dim rngSht1 As Range
Dim wsDisc As Worksheet
Dim strCostCode As String
Dim strDiscount As String
Dim c As Range

strCostCode = "0007234"
strDiscount = "0007346"

With Sheets("Sheet1")
Set rngSht1 = Range(.Cells(6, 2), _
.Cells(Rows.Count, 2).End(xlUp))
End With

Set wsDisc = Sheets("Discount")

With wsDisc
.Cells(1, 3) = "Date"
.Cells(1, 6) = "Cost"
.Cells(1, 7) = "Category"
'Format col G as text otherwise
'leading zeros will be dropped.
.Columns("G:G").NumberFormat = "@"
End With

For Each c In rngSht1
'First row of data
'Copy paste Date
c.Copy _
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)

'Copy paste Cost
c.Offset(0, 3).Copy _
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 3)

'Insert Cost Code
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 4) _
= strCostCode
'Alternative if Discount code is in a cell
'wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 4) _
= wsDisc.Range("I2")


'Second row of data
'Copy paste Date
c.Copy _
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)

'Copy paste Discount
c.Offset(0, 9).Copy _
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 3)

'Insert Discount Code
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 4) _
= strDiscount
'Alternative if Discount code is in a cell
'wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 4) _
= wsDisc.Range("J2")

Next c

End Sub


Regards,

OssieMac



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Copy Values Between Worksheets in same Workbook

I triy to make code easily readable so it can be maintained and changed in
the future. the Computer science courses I took in college the teachers
insisted on clear documentation.

My prefedrence is to avoid using OFFSET in functions unless it is necessary.
In my code I selected the column letters (cells(RowCount,"C") when I did the
copy instruction.

I believe the offset should only be used when you need tthe code to be
flexible that the selected cell can vary. In this case the columns and rows
are known so I don't think the offset is necessary.

Additionally, offsets are sometimes the better way of going because the code
runs quicker (in some cases).

"OssieMac" wrote:

Hi Joe,

I see that Joel has posted a reply while I was dragging my feet getting one
ready. From your previous post I assume that you are on a learning curve so
I'll post the code I came up with. (That does not mean that I am critical of
Joel's code because that is not the case. It is simply to show you another
option.)

Sub Copy_Data()

Dim rngSht1 As Range
Dim wsDisc As Worksheet
Dim strCostCode As String
Dim strDiscount As String
Dim c As Range

strCostCode = "0007234"
strDiscount = "0007346"

With Sheets("Sheet1")
Set rngSht1 = Range(.Cells(6, 2), _
.Cells(Rows.Count, 2).End(xlUp))
End With

Set wsDisc = Sheets("Discount")

With wsDisc
.Cells(1, 3) = "Date"
.Cells(1, 6) = "Cost"
.Cells(1, 7) = "Category"
'Format col G as text otherwise
'leading zeros will be dropped.
.Columns("G:G").NumberFormat = "@"
End With

For Each c In rngSht1
'First row of data
'Copy paste Date
c.Copy _
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)

'Copy paste Cost
c.Offset(0, 3).Copy _
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 3)

'Insert Cost Code
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 4) _
= strCostCode
'Alternative if Discount code is in a cell
'wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 4) _
= wsDisc.Range("I2")


'Second row of data
'Copy paste Date
c.Copy _
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)

'Copy paste Discount
c.Offset(0, 9).Copy _
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 3)

'Insert Discount Code
wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 4) _
= strDiscount
'Alternative if Discount code is in a cell
'wsDisc.Cells(Rows.Count, 3).End(xlUp).Offset(0, 4) _
= wsDisc.Range("J2")

Next c

End Sub


Regards,

OssieMac



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
Copy worksheets within a workbook robert morris Excel Discussion (Misc queries) 0 February 24th 08 01:25 PM
how to copy only values and formats of worksheets to new workbook rvd Excel Worksheet Functions 3 January 31st 07 12:43 PM
copy between worksheets does not copy formulae just values Chris@1000 Oaks Excel Discussion (Misc queries) 0 March 19th 06 11:44 AM
Copy four worksheets from one workbook into a new workbook.e-mail Francis Brown Excel Programming 1 October 3rd 05 12:24 AM
How do I sum values from different worksheets within one workbook. master gardener Excel Worksheet Functions 1 January 28th 05 07:19 PM


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

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"