VBA - Multiple AND within IF statements

Ananthak275

Board Regular
Joined
Aug 22, 2020
Messages
128
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
This is part of a code that i keep getting an error on

Its going into each column, looking if the previous value in the column is the same as the current. All three have to be true
If (source.Sheets(1).Cells(j, ColumnA).Value = source.Sheets(1).Cells(j - 1, ColumnA).Value)
AND source.Sheets(1).Cells(j, ColumnB).Value = source.Sheets(1).Cells(j - 1, ColumnB).Value
AND (source.Sheets(1).Cells(j, ColumnC).Value = source.Sheets(1).Cells(j - 1, ColumnC).Value) THEN a = 2

Error: Expected: line number or label or statement or end of statement
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Missing brackets in green:

If (source.Sheets(1).Cells(j, ColumnA).Value = source.Sheets(1).Cells(j - 1, ColumnA).Value)
(AND source.Sheets(1).Cells(j, ColumnB).Value = source.Sheets(1).Cells(j - 1, ColumnB).Value)
AND (source.Sheets(1).Cells(j, ColumnC).Value = source.Sheets(1).Cells(j - 1, ColumnC).Value) THEN a = 2


Try replacing all of that with:
VBA Code:
With source.Sheets(1)
        If .Cells(j, columnA).Value = .Cells(j - 1, columnA).Value And _
            .Cells(j, ColumnB).Value = .Cells(j - 1, ColumnB).Value And _
            .Cells(j, ColumnC).Value = .Cells(j - 1, ColumnC).Value Then a = 2
        End If
    End With
 
Upvote 0
Solution

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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