Copy range to new sheet

JAVEDR

Board Regular
Joined
Sep 17, 2019
Messages
79
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
  3. Web
Greetings of day, I have macro which work for single condition but all time I have to change condition as multiple condition which is not feasible any help to modify same current macro as follow
VBA Code:
Sub CopyDataToNewSheet()
    Dim sourceSheet As Worksheet
    Dim destinationSheet As Worksheet
    Dim lastRow As Long
    Dim startRow As Long
    Dim i As Long
    Dim destRow As Long
    Dim copyRange As Range
    Dim condition As String
    
    ' Set the source sheet (adjust the sheet name as needed)
    Set sourceSheet = ThisWorkbook.Sheets("Sheet1")
    
    ' Add a new sheet for the destination
    Set destinationSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    destinationSheet.Name = "NewSheet"
    
    lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "C").End(xlUp).row
    destRow = 2 ' Start from row 2 on the destination sheet (to leave room for headers)
    startRow = 2 ' Assuming your data starts in row 2
    
    ' Get the condition from cells C468 and C469
    condition = sourceSheet.Cells(468, "C").Value & sourceSheet.Cells(469, "C").Value
    
    ' Copy header row to the destination sheet
    sourceSheet.Range("A1:V1").Copy destinationSheet.Range("A1")
    
    ' Adjust column widths on the destination sheet
    destinationSheet.Columns.AutoFit
    
    For i = startRow To lastRow
        ' Check if the condition is met for each set of rows
        If sourceSheet.Cells(i, "C").Value & sourceSheet.Cells(i + 1, "C").Value = condition Then
            If copyRange Is Nothing Then
                Set copyRange = sourceSheet.Range("A" & i & ":V" & i + 1)
            Else
                Set copyRange = Union(copyRange, sourceSheet.Range("A" & i & ":V" & i + 1))
            End If
        ElseIf Not copyRange Is Nothing Then
            ' If the condition is not met, but we have a copyRange, then copy and reset it
            copyRange.Copy destinationSheet.Range("A" & destRow)
            destRow = destRow + copyRange.Rows.Count
            
            ' Insert a blank row after each set of results
            destRow = destRow + 1
            
            Set copyRange = Nothing
        End If
    Next i
    
    ' Copy the last range if there's any remaining
    If Not copyRange Is Nothing Then
        copyRange.Copy destinationSheet.Range("A" & destRow)
    End If
    
    MsgBox "Data copied successfully!", vbInformation
End Sub

result below =
DateOPJODCPOPJODCPOPJODCPOPJODCPOPJODCPOPJODCPOPJODCP
17-Nov-14457-14-180359-12-556479-40-270477-23-570459-13-259123-10-550189-13-790
24-Nov-14158-40-267458-12-600580-34-469126-40-145478-24-246379-34-170345-24-199
25-Jul-16290-14-360240-11-268128-13-459167-34-468138-24-680559-34-166330-14-248
1-Aug-16379-40-578118-50-339478-24-179223-20-249158-34-558114-13-599478-34-256
13-Aug-18236-14-180234-34-567******150-10-668499-12-335370-50-780490-13-457
20-Aug-18130-40-889238-33-445146-14-248268-12-156399-14-234470-11-128******
17-Jul-23360-14-358790-11-470268-13-440224-33-125478-44-450600-12-124460-40-469
24-Jul-23460-40-469456-20-499456-20-499567-23-679667-24-160130-24-14700


modification needed
1. I'm looking for combination of second last and last row of range, want to filter rows where the values in cells C468 and C469 match a specific condition (e.g., both cells contain a specific number), and then copy the entire set of rows that match this condition to a new sheet.
2. Conditions are multiple like b468 and b469 , b468 and c469, b468 and d469, b468 and e469, b468 and f469, b468 and g469, b468 and h469, b468 and i469
, b468 and j469, b468 and k469, b468 and l469,b468 and m469, b468 and n469, b468 and o469,b468 and p469, b468 and q469, b468 and r469, b468 and s469. repeat the same for cell c , d , e , f , g , h till s and result will be collected in new sheet and highlight that condition

I know above is bit complicated to understand , trying best to make it easy. Thank you for your valuable time and suggestions
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Can you submit some data that these combinations are represented in?
 
Upvote 0
Please find data below highlight in green there might b more as per condition match
search.xlsm
ABCDEFGHIJKLMNOPQRSTUV
1DateOPJODCPOPJODCPOPJODCPOPJODCPOPJODCPOPJODCPOPJODCP
219-May-14580-34-126236-13-490467-23-779134-34-400457-10-456123-12-340147-22-377
326-May-14135-34-440467-22-160240-12-246126-40-488668-30-350259-14-126180-14-556
42-Jun-14157-13-678124-23-120189-34-900588-10-578137-10-136239-24-160338-40-780
59-Jun-14569-10-380248-14-579378-30-159250-23-134566-12-349168-30-116350-34-379
616-Jun-14134-34-356370-10-128460-30-116129-23-260890-20-226459-34-199557-24-450
723-Jun-14115-22-138668-10-114340-20-889224-30-460257-34-355668-10-227160-20-168
830-Jun-14358-14-590247-13-240690-50-159227-11-240778-12-290289-40-389134-23-246
97-Jul-14667-24-480778-23-490457-13-256290-13-260128-13-689117-40-267459-13-169
1014-Jul-14112-34-134120-13-277299-50-370268-11-556280-40-900349-10-500468-13-470
1121-Jul-14380-11-899456-50-159188-20-249588-11-899140-10-128568-14-268134-34-478
1228-Jul-14170-34-388******347-40-479788-33-490130-14-380238-33-580559-14-344
134-Aug-14470-14-455770-44-333229-23-110300-34-144478-14-290229-30-159******
1411-Aug-14900-44-270127-30-490146-12-458277-11-155******378-33-279679-23-157
1518-Aug-14250-12-240550-20-467789-14-245123-11-466248-34-134567-34-167120-33-139
1625-Aug-14470-13-229388-14-457457-13-350267-50-488680-44-356236-11-100389-20-129
171-Sep-14148-23-449400-40-127668-20-179156-23-355124-23-580577-44-117460-40-379
188-Sep-14690-30-580129-20-889789-40-677234-40-569688-12-277678-12-250678-12-250
1915-Sep-14168-50-569499-23-157378-34-130224-23-480233-13-367127-40-347590-14-600
2022-Sep-14233-34-446257-44-900112-44-568178-11-128890-23-788790-11-128689-34-266
2129-Sep-14679-12-137168-30-279890-24-360377-23-279188-22-480660-20-370346-30-230
226-Oct-14115-20-889260-33-440377-12-146568-44-144126-44-559148-23-345238-30-357
2313-Oct-14123-14-559579-12-566900-14-560157-33-459250-23-477599-23-124399-13-689
2427-Oct-14******379-24-237248-14-489348-50-370356-34-166455-24-246346-13-678
253-Nov-14458-20-555790-14-590347-34-378238-34-770390-24-347138-23-157578-50-488
2610-Nov-14228-12-899678-10-168890-22-188799-10-579120-13-290280-40-577699-14-344
2717-Nov-14457-14-180359-12-556479-40-270477-23-570459-13-259123-10-550189-13-790
2824-Nov-14158-40-267458-12-600580-34-469126-40-145478-24-246379-34-170345-24-199
291-Dec-14257-40-136139-23-589880-10-113129-12-268245-11-579390-24-478347-34-670
308-Dec-14450-40-258789-24-589338-40-190346-23-589479-10-150378-30-479188-22-557
3115-Dec-14467-23-170489-12-458114-10-370270-34-120136-50-140134-13-380178-14-356
3222-Dec-14600-11-367689-34-270167-34-229246-24-900290-14-379480-23-256335-12-237
3329-Dec-14490-23-138240-11-358568-14-259135-44-117334-10-290890-24-126560-14-347
345-Jan-15459-30-299670-23-566440-23-449247-33-689489-10-140279-30-190480-22-340
3512-Jan-15144-24-179788-13-259******248-44-126257-44-180270-24-138160-12-358
3619-Jan-15166-33-468126-14-245139-33-580380-11-123170-33-260127-40-446358-14-149
3726-Jan-15******138-20-280780-20-480145-50-299360-24-660369-33-477569-30-238
382-Feb-15280-50-136349-14-257778-20-244158-44-126145-40-446680-14-245247-30-177
399-Feb-15469-14-114479-20-566667-24-138470-14-220257-34-670134-13-290579-12-269
4016-Feb-15567-34-360128-11-349367-13-260229-30-488349-10-258348-30-157140-20-188
4123-Feb-15568-34-580889-10-358235-50-460226-30-346566-12-399360-14-367459-23-223
422-Mar-15577-44-130679-20-230140-40-112139-13-259******340-12-470800-33-238
439-Mar-15245-11-380459-23-449569-30-459170-33-125348-30-157230-10-457478-44-789
4416-Mar-15578-20-147568-44-680130-14-600133-22-250380-10-357160-20-389589-22-179
4523-Mar-15789-14-457179-23-558239-14-137144-40-235168-50-127258-50-136479-20-480
4630-Mar-15790-10-500169-10-136370-20-570456-40-360239-34-189359-12-367457-10-159
476-Apr-15239-44-117144-14-358126-24-110770-24-589112-34-229223-22-679138-23-788
Sheet3
Cells with Conditional Formatting
CellConditionCell FormatStop If True
L2Expression=NOT(ISERROR(MATCH(L2,$BG$1:$BG$50,0)))textNO
O2Expression=NOT(ISERROR(MATCH(O2,$BG$1:$BG$50,0)))textNO
R2Expression=NOT(ISERROR(MATCH(R2,$BG$1:$BG$50,0)))textNO
F2:F47Expression=NOT(ISERROR(MATCH(F2,$BG$1:$BG$50,0)))textNO
I2:I47Expression=NOT(ISERROR(MATCH(I2,$BG$1:$BG$50,0)))textNO
C2Expression=NOT(ISERROR(MATCH(C2,$BG$1:$BG$50,0)))textNO
C1,F1,I1,L1,O1,R1,C3:C47,L3:L47,O3:O47,R3:R47,U1:U47Expression=NOT(ISERROR(MATCH(C1,$BG$1:$BG$50,0)))textNO
D32Cell Value=$N$2textYES
D1:E47,G1:H47,J1:K47,M1:N47,P1:Q47,S1:T47,V1:V47,A1:B47,W6,W46:W47Cell Value=#REF!textYES

Can you submit some data that these combinations are represented in?
 
Upvote 0

Forum statistics

Threads
1,215,078
Messages
6,122,996
Members
449,093
Latest member
masterms

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top