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 616 views

## 1 answer to this question.

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
• 63,420 points

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

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

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

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

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

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

## VBA Code Help - Need to add a line for each missing date and copy data from cells below

All you have to do is copy ...READ MORE

## Confused on VBA copy destination

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

## Retrieve epay.info Balance with VBA and Excel

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

## How to load file to Excel Power query from SFTP site

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