How To Copy Cut Row of Data Based on TRUE FALSE Condition Excel VBA

0 votes

I want to programmatically copy and paste all the rows from the "Product Price List" page that satisfy the TRUE criterion into the "Customer List" sheet.

Only columns A,B,C, and D need to be copied, not all other columns.

In order to maintain the consistency of Product Numbers, I ask that the "Customer List" sheet be cleared and re-pasted if more TRUE conditions are later satisfied.

'Product Price List Screenshot'

'Customer List' Screenshot

I am a coding novice and have tried various attempts, to no avail.

Feb 4, 2023 in Others by Kithuzzz
• 38,000 points
912 views

1 answer to this question.

0 votes

Solution

  • Loop through the rows on the Price List
  • When Column H of the current row in the loop is TRUE then:
    • Add values from Column A - Column D to a Union which is referred to as true_collection in code
    • A Union is just a collection of cells. In this use case, it's used to create a non-continuous range to be copied/pasted

A more efficient way to do this would be to just filter your data set (Column H = TRUE) and then copy/paste the resultant (visible) cells of the filter. This is more or less how you would do this manually.


Sub free_code_come_get_your_free_code_free_code()

Dim pl As Worksheet: Set pl = ThisWorkbook.Sheets("Price List")
Dim cl As Worksheet: Set cl = ThisWorkbook.Sheets("Customer List")

Dim lr As Long, i As Long
Dim true_collection As Range

lr = pl.Range("H" & pl.Rows.Count).End(xlUp).Row


For i = 5 To lr
    If pl.Range("H" & i) Then
        If Not true_collection Is Nothing Then
            Set true_collection = Union(true_collection, pl.Range("A" & i).Resize(1, 4))
        Else
            Set true_collection = pl.Range("A" & i).Resize(1, 4)
        End If
    End If
Next i
            

If Not true_collection Is Nothing Then
    lr = cl.Range("A" & cl.Rows.Count).End(xlUp).Offset(1).Row
    cl.Range("A5:D" & lr).Clear
    
    true_collection.Copy
    cl.Range("A5").PasteSpecial xlPasteValues
End If

End Sub
answered Feb 4, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

How to automatically get a specified range of data from Excel to XML in VBA

Range method works, always identify the sheet ...READ MORE

answered Mar 18, 2023 in Others by narikkadan
• 63,600 points
832 views
0 votes
1 answer

VBA Loop to select then copy a range of cells based on value in column B

Try this: Sub Macro2() Dim ...READ MORE

answered Mar 23, 2023 in Others by narikkadan
• 63,600 points
2,310 views
0 votes
1 answer

How to import data from a HTML table on a website to excel?

Hello  To import any HTML file in excel there ...READ MORE

answered Feb 10, 2022 in Others by gaurav
• 23,260 points
7,886 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,053 views
0 votes
1 answer
0 votes
1 answer

Confused on VBA copy destination

Try this: Sub so75496418AddChart() Dim rng As Range Dim strTypeName ...READ MORE

answered Feb 20, 2023 in Others by Kithuzzz
• 38,000 points
873 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,212 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,662 views
0 votes
1 answer

In a excel formula I need to create a list of names on one sheet based upon criteria/data of another sheet

The final formula is: =IF(ROWS($H$3:H3)<=$I$1,INDEX(Personnel! ...READ MORE

answered Nov 25, 2022 in Others by narikkadan
• 63,600 points
1,103 views
0 votes
1 answer

Excel VBA search based on cell values into folders and sub-folders to get the file path and data

This will create a listing of all ...READ MORE

answered Jan 19, 2023 in Others by narikkadan
• 63,600 points
2,076 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP