the_schuetz
New Member
- Joined
- Apr 13, 2022
- Messages
- 5
- Office Version
- 365
- Platform
- Windows
Hi everyone!
If have the following two workbooks:
(1) wrkData: which contains the first of the month in Column C and daily precipitation data starting from Column N until AR (depending on the month it goes to either AR for 31 days, AQ for 30 days, AP for 29 days and AO 28 days).
(2) wrkPrecip: which contains in Column A daily dates and in Column B I want to paste & transpose from Workbook wrkData N:AR/AQ/AP/AO in a loop.
This turns out to be more difficult than anticipated because the range that I want to copy changes from month to month. Also, sometimes I have blank cells at the end of a month in Workbook wrkData that need to stay blank in the Workbook wrkPrecip which is why I can't use the last row with data for the loop.
This is what I got so far. I do not receive an error, but as soon as I run the macro Excel stops responding. Any ideas? Thanks in advance!
If have the following two workbooks:
(1) wrkData: which contains the first of the month in Column C and daily precipitation data starting from Column N until AR (depending on the month it goes to either AR for 31 days, AQ for 30 days, AP for 29 days and AO 28 days).
(2) wrkPrecip: which contains in Column A daily dates and in Column B I want to paste & transpose from Workbook wrkData N:AR/AQ/AP/AO in a loop.
This turns out to be more difficult than anticipated because the range that I want to copy changes from month to month. Also, sometimes I have blank cells at the end of a month in Workbook wrkData that need to stay blank in the Workbook wrkPrecip which is why I can't use the last row with data for the loop.
This is what I got so far. I do not receive an error, but as soon as I run the macro Excel stops responding. Any ideas? Thanks in advance!
VBA Code:
'Loop copy daily data to new precipitation workbook
Dim d As Long 'for loop through wrkData
Dim p As Long 'for cell to paste into in wrkPrecip
Dim c As Long 'defines cell step for paste in wrkPrecip
wrkData.Activate
LastRow = Cells(Rows.Count, 3).End(xlDown).Row
p = 2
For d = 2 To LastRow
If Month(Cells(d, "C")) = 1 Or Month(Cells(d, "C")) = 3 Or Month(Cells(d, "C")) = 5 Or Month(Cells(d, "C")) = 7 Or Month(Cells(d, "C")) = 8 Or Month(Cells(d, "C")) = 10 Or Month(Cells(d, "C")) = 12 Then
Range(Cells(d, "N"), Cells(d, "AR")).Copy
c = 30
ElseIf Month(Cells(d, "C")) = 4 Or Month(Cells(d, "C")) = 6 Or Month(Cells(d, "C")) = 9 Or Month(Cells(d, "C")) = 11 Then
Range(Cells(d, "N"), Cells(d, "AQ")).Copy
c = 29
ElseIf Month(DateSerial(Year(Cells(d, "C")), 2, 28)) = 2 Then
Range(Cells(d, "N"), Cells(d, "AO")).Copy
c = 27
ElseIf Month(DateSerial(Year(Cells(d, "C")), 2, 29)) = 2 Then
Range(Cells(d, "N"), Cells(d, "AP")).Copy
c = 28
End If
wrkPrecip.Activate
Cells(p, "B").PasteSpecial Transpose:=True
p = p + c
wrkData.Activate
Next d