Aisling3475
New Member
- Joined
- Mar 21, 2022
- Messages
- 5
- Office Version
- 365
- Platform
- Windows
Hi There, I have reached the limit of my VBA macro knowledge and am in a bit of a pickle! I have a data report that contains data across a range of dates in a worksheet Sheet 1. I need to generate a macro to execute the following commands:
1. Find specific wording in Sheet1 "esWeeklyCal" in Column B
2. Resize this to select data range 14 rows down and 37 columns to the right (column AI).
3. Copy this range
4. Paste this range starting in Cell B2 in Sheet 2.
5. Then I want the code to go back to sheet 1 and find the next "esWeeklyCal" data range in Sheet 1 and copy this to sheet 2 below the first data range copied in Step 4 above and repeat this for all data ranges for 'esweeklyCal'.
The code below is what I have built so far for this. The problem is every time I execute this the first data range copies to Sheet 2 at cell B2, then subsequent data ranges paste over the second data range in cell B16 and therefore I lose these data ranges in the macro. Some extra info for that may or may not help:
- Sheet 1 has multiple 'esWeeklyCal' rows and associated data ranges. Between these data ranges are other data that I do not want.
- The 'esWeeklyCal' data range has 'esweeklyCal' in first column (call this column B) and the data of interest is 33 columns to the right (column AI) and there is 14 rows of data in that column. The rows below the 'esweeklyCal' text in column B are empty. So when I paste to Sheet 2 I have 'esweeklyCal' in Column B and the 13 rows of data in Column AI. I need to keep all of the 'metadata' in between column B and column AI to the row 13.
- The data report in sheet 1 is in merged cells in some places, is this an issue for the code, should I use paste special for values only?
So I cannot seem to get the code to loop back to Sheet 1 after first paste event, select the next data range, copy and paste and offset from the first paste event in Sheet 2 to retain all data. I need it to repeat the text search in sheet 1 and repeat the paste to pull all data ranges for 'esweeklyCal' into Sheet 2.
***MACRO CODE****
Sub ExtractData()
Dim StatusCol As Range
Dim Status As Range
Dim PasteCell As Range
Set StatusCol = Sheet1.Range("A9:AK10000")
For Each Status In StatusCol
If Sheet2.Range("B2") = "" Then
Set PasteCell = Sheet2.Range("B2")
Else
Set PasteCell = Sheet2.Range("B1").End(xlDown).Offset(14, 0)
End If
If Status = "esWeeklyCal" Then Status.Offset(0, 0).Resize(14, 37).Copy PasteCell
Next Status
For Each Status In StatusCol
If Sheet2.Range("B16") = "" Then
Set PasteCell = Sheet2.Range("B16")
Else
Set PasteCell = Sheet2.Range("B15").End(xlDown).Offset(14, 0)
End If
If Status = "esWeeklyCal" Then Status.Offset(0, 0).Resize(14, 37).Copy PasteCell
Next Status
End Sub
1. Find specific wording in Sheet1 "esWeeklyCal" in Column B
2. Resize this to select data range 14 rows down and 37 columns to the right (column AI).
3. Copy this range
4. Paste this range starting in Cell B2 in Sheet 2.
5. Then I want the code to go back to sheet 1 and find the next "esWeeklyCal" data range in Sheet 1 and copy this to sheet 2 below the first data range copied in Step 4 above and repeat this for all data ranges for 'esweeklyCal'.
The code below is what I have built so far for this. The problem is every time I execute this the first data range copies to Sheet 2 at cell B2, then subsequent data ranges paste over the second data range in cell B16 and therefore I lose these data ranges in the macro. Some extra info for that may or may not help:
- Sheet 1 has multiple 'esWeeklyCal' rows and associated data ranges. Between these data ranges are other data that I do not want.
- The 'esWeeklyCal' data range has 'esweeklyCal' in first column (call this column B) and the data of interest is 33 columns to the right (column AI) and there is 14 rows of data in that column. The rows below the 'esweeklyCal' text in column B are empty. So when I paste to Sheet 2 I have 'esweeklyCal' in Column B and the 13 rows of data in Column AI. I need to keep all of the 'metadata' in between column B and column AI to the row 13.
- The data report in sheet 1 is in merged cells in some places, is this an issue for the code, should I use paste special for values only?
So I cannot seem to get the code to loop back to Sheet 1 after first paste event, select the next data range, copy and paste and offset from the first paste event in Sheet 2 to retain all data. I need it to repeat the text search in sheet 1 and repeat the paste to pull all data ranges for 'esweeklyCal' into Sheet 2.
***MACRO CODE****
Sub ExtractData()
Dim StatusCol As Range
Dim Status As Range
Dim PasteCell As Range
Set StatusCol = Sheet1.Range("A9:AK10000")
For Each Status In StatusCol
If Sheet2.Range("B2") = "" Then
Set PasteCell = Sheet2.Range("B2")
Else
Set PasteCell = Sheet2.Range("B1").End(xlDown).Offset(14, 0)
End If
If Status = "esWeeklyCal" Then Status.Offset(0, 0).Resize(14, 37).Copy PasteCell
Next Status
For Each Status In StatusCol
If Sheet2.Range("B16") = "" Then
Set PasteCell = Sheet2.Range("B16")
Else
Set PasteCell = Sheet2.Range("B15").End(xlDown).Offset(14, 0)
End If
If Status = "esWeeklyCal" Then Status.Offset(0, 0).Resize(14, 37).Copy PasteCell
Next Status
End Sub