Copying values using an IF nested inside For Each

MadButter

New Member
Joined
Oct 2, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi folks!

I'm having troubles figuring out a way to move data from multiple columns (that are not text to each other) based on a different column that is being compared to yet another column:

The file that I'm using has over 12k rows and what I'm aiming to do is get the data from columns A and C to another worksheet. The main issue is that I need to copy said values for each unique identifier separately (so for example first I need to copy the values for all Identifiers (column B) with value "ID1" and so on).
I've gathered all the unique Identifiers to another column by using
VBA Code:
Range("B2:B12000").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("X2"), Unique:=True
and this part works fine (around 1200 unique values)

So now the next step would be to use the column with unique identifiers and for each of these search for rows with the same identifier and get the A and C values from those copied to yet another worksheet.

ABCUnique Identifiers
aaaID1eeeID1
sssID5fffID5
zzzID2gggID2
dddID1hhh

My general idea on how this would work was something in the shape of For Each value in column X (Unique Identifiers) with If nested

Code:
For Each c In Range("X:X")
    If c = value in column B
    Copy values A and D using B row number
    Paste them to another worksheet
    Then look further if there are any matching values in column B
    If yes - copy the values again and paste them to another worksheet (below the ones that were previously pasted)
    Else
    End If

Now there are two issues with the above:
1. It's not a proper code
2. Wouldn't this kind of loop take a long time to finish? Since it's going to scan all the Identifiers (12k) every single time.


I'd be grateful for any help with the code part of this issue and with any corrections to my approach!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I cannot promise that I'll be able to assist but someone else may. Might you provide a link to a version of your workbook with only part of the data so we have some actual data to work with? Is the worksheet copied to in the same workbook as the data? How many IDs are there?
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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