Return values from column based on condition

truerip

New Member
Joined
Feb 26, 2015
Messages
19
This is probably very simple but is giving me a problem.
I want to retrieve values from column B of Sheet2 to column C of Sheet1 while excluding cells that contain certain text (up to 4 different text strings).
Any suggestions would be gratefully appreciated.
Best regards,
truerip
 
Results not as expected.
Results:
$C$2 and empty message box
$C$3 and empty message box
$C$4 and empty message box
$C$5 and empty message box
$C$6 and empty message box
$C$7 and empty message box
$C$8 and empty message box
$C$2 and empty message box
$C$9 and "Standard"

Here is the data on Sheet2 that I'm working with (removed values from previous for testing):

Excel 2013/2016
AB
1
2
3
4
5
6
7
8Standard
9
10BBB-001-TRE-1
11BAL-005-0.2b
12CIP-002-5.1a
13CIP-003-6
14CIP-004-6
15CIP-005-5
16
Sheet2


This is what I end up with on Sheet1:

Excel 2013/2016
ABC
1
2
3
4
5
6
7
8
9Standard
10
11
12
13
14
Sheet1
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Sorry True,

That's my error. Needed a second reference to "Sheet2". Replace the For loop with this:

Code:
For Each forCELL In Sheets("Sheet2").Range("B" & varROW & ":B" & Sheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row).Cells
         If Evaluate("=COUNTIF(Sheet3!A:A," & forCELL.Value & ")") = 0 Then
             Sheets("Sheet1").Range("C" & varDestRow) = forCELL.Value
             varROW = varROW + 1
             varDestRow = varDestRow + 1
         End If
Next

That's why knowing how to hunt errors is as key as knowing how to create them!! :biggrin:

Cheers JB
 
Upvote 0
Hello JB,
I am still missing something.
I adjusted the code per you direction in last post leaving the MSgBox's from before.
Results:
$C$2 and "Standard"
$C$3 and empty message box
$C$4 and "BBB-001-TRE-1"
Then I receive a Run-time error '13': Type mismatch.



Data in sheet2 is still as in above post.
Sheet3 is blank (hope to get the exclusions working later)
Sheet1 results before Run-time error:

Excel 2013/2016
ABCD
1
2Standard
3
4BBB-001-TRE-1
5
6
7
Sheet1


Here is the code:


Code:
Sub MoveCertainCells()
    
    varROW = 8 'change this number to whatever row your data starts on.
    varDestRow = 2
   
    For Each forCELL In Sheets("Sheet2").Range("B" & varROW & ":B" & Sheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row).Cells
         If Evaluate("=COUNTIF(Sheet3!A:A," & forCELL.Value & ")") = 0 Then
             Sheets("Sheet1").Range("C" & varDestRow) = forCELL.Value
             MsgBox Sheets("Sheet1").Range("C" & varDestRow).Address
             MsgBox forCELL.Value
             varROW = varROW + 1
             varDestRow = varDestRow + 1
         End If
    Next
End Sub

Thank you for continuing to troubleshoot this.
Best regards,
True
 
Upvote 0
I've just run this exact code in a test book, with some random text in B8, B10, 11, 12, down to about 18. I basically just mashed the keyboard and hit enter a few times. Anyway, it works exactly as it should.

What is in Sheet2 Cell B11?
 
Upvote 0
Try changing the Evaluate to this
Code:
If Evaluate("=COUNTIF(ToDo!A:A," & forcell.[COLOR=#ff0000]Address [/COLOR]& ")") = 0 Then
 
Upvote 0
That got it Fluff! :) It transfers all of the data from Sheet2 B:B to Sheet1 C:C now!
Now, how can I get it to evaluate and exclude values that match values located in Sheet3 A:A?
For example, if I want to exclude all cells that contain an instance of "BAL", entered into Sheet3 A:A along with other values for exclusion, it should not return BAL-005-0.2b to Sheet1 from Sheet2 but skip to the next cell value and evaluate it and so on.
Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

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