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
>
>
>
|