• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Perform If Condition before running code

ThrottleWorks

Excel Ninja
I am performing below mentioned check through my code.
If the value in first condition is blank then replace it with xyz ( or perform the action mentioned in the code).

But before performing this action I want to run an if condition.
If that condition is met then only i want to execute the (inserted) code.

I have target names in 2 different sheets in same file.
Let’s say target 1 range is sheet 1 column A range A1:A50

Now i have target 2 names in sheet 2 column B range A1:A100
The ABCD mentioned in the below code is name of the column.
This name will be present in range target 1.

I want to check if the ABCD which is present in Target 1 is also present in Target 2.

If ABCD is present in both Target 1 and Target 2 then only I want to perform the below mentioned code.

I can only think of doing with control F but that will be very cumbersome.
Can someone please suggest me better option.

Code:
If Result.Cells(i, FindCol("ABCD").Column) = "" Then
   Result.Cells(i, FindCol("ABCD").Column).value  = “xyz”
End If
 
Hi Throttle,

Any chance you could upload your workbook? You mentioned that the names ranges are each a single column, but then talk about finding the "column" ABCD. Additionally, your sample code is useing some function called FindCol, which I'm not sure what it does.
 
Hi Luke Sir,
My apology, could not upload sample file (from office).

Sir I need to perform 2 different tasks.

I have 2 columns with names. File name test.

Range of 1st column, sheet 1 A1:A50 (around 50 names)

Range of 2nd column, sheet 2 B1:B100 (around 100 names)

I am trying find name from the first table, say from range A1 (xyz) in 2nd table.

If I am able to find xyz in 2nd table then I will run remaining code else will stop there.

Trying to do and If Then Else If but not able guess how.
My apologies once again for not uploading sample file.

I am trying somenthing as mentioned below.

If Test File Sheet 1 Range A1 value is present is Test File Sheet 2 Range(B1:B100) Then

Run remaining code

Else

Do Nothing
 
Does this help?
Code:
Sub ExampleSearch()
Dim rng1 As Range
Dim rng2 As Range
Dim fCell As Range

Dim c As Range

'The two ranges to compare. You can set these however makes sense
Set rng1 = Worksheets("Sheet1").Range("A1:A50")
Set rng2 = Worksheets("Sheet2").Range("B1:B100")

Application.ScreenUpdating = False
For Each c In rng1
    'Check for existance
    Set fCell = rng2.Find(c.Value)
  
    If fCell Is Nothing Then
        'do nothing
    Else
        'Put code here
    End If
Next c
Application.ScreenUpdating = True
  
End Sub
 
Back
Top