Question : How can I copy a range of cells in a worksheet to another area in the same worksheet based off of the value of a cell in Excel 2007?

Hello,

I need to be able to programatically copy a range of cells in an excel workbook to another area in the same workbook based off of the values of the start and end dates in the worksheet.  The worksheet that I have attached shows sample data.  So, if I needed to change the pick start date of an item, I need excel to pick up all of the values in the month columns and shift it according to the new pick start date.  Also, if the expiration end date changes, I need it to adjust accordingly.  Is this possible using vba?
 
Sample data
 

Answer : How can I copy a range of cells in a worksheet to another area in the same worksheet based off of the value of a cell in Excel 2007?

daintysally,

The code below is in the attached file.

Hope it does what you want.

Patrick
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
Sub specialmacro()
Dim ws As Worksheet
Dim rng1 As Range
Dim celle1 As Range
Dim rng2 As Range
Dim celle2 As Range
Dim pickstart As Date
Dim expiration As Date
Dim pickweek As Long
Dim rng_step As Long
Dim step_counter As Long

Set ws = Sheets("Example-before")

Set rng1 = ws.Range(ws.Cells(2, "A"), ws.Cells(ws.Rows.Count, "A").End(xlUp))
pickweek = 3

step_counter = 0
For rng_step = 0 To 26 Step 13
    For Each celle1 In rng1
        pickstart = celle1.Offset(0, 3)
        expiration = celle1.Offset(0, 4)
        Set rng2 = Range(ws.Cells(celle1.Row, "F"), ws.Cells(celle1.Row, "Q")).Offset(0, rng_step)
        rng2(1, 12).Offset(0, 1).FormulaR1C1 = "=SUM(RC[-12]:RC[-1])"
        rng2.ClearContents
        For Each celle2 In rng2
            If ws.Cells(1, celle2.Column) >= pickstart _
                And ws.Cells(1, celle2.Column) <= expiration Then
                celle2 = pickweek
                pickweek = pickweek + 1
            End If
        Next celle2
    Next celle1
    step_counter = step_counter + 1
    pickweek = 3
Next rng_step

End Sub
Random Solutions  
 
programming4us programming4us