VBA Help

Donai

Well-known Member
Joined
Mar 28, 2009
Messages
543
I am after a macro that will return either "OK" or "ERROR" in D6:D if there is a difference between Previous and Current Count. The data does not have a fixed end range, it will change from day to day.

Excel Workbook
ABCD
5StockPreviousCurrentCheck
6TLS1212OK
7BHP31ERROR
8NAB4545OK
9CBA77OK
10MQB2323OK
11WES88OK
Sheet1
 

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
ok i have never done looping before, so can someone point me to the right direction, i get an error on ELSE

Code:
Sub Test()
Dim LastRow, i As Long
 
LastRow = Range("D" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
    If Cells(i, 3) <> Cells(i, 2) Then Cells(i, 4) = "ERROR"
 
    Else
        Cells(i, 4) = "OK"
    End If
 
 Next i
 
End Sub
 
Upvote 0
Why use VBA when a simple Excel IF function will suffice?

In D6 enter the formula =IF(B6<>C6,"Error","OK").

Now, move the cursor over the bottom-right corner of D6 and double-click. Excel will fill D as far down as you have data in C.
I am after a macro that will return either "OK" or "ERROR" in D6:D if there is a difference between Previous and Current Count. The data does not have a fixed end range, it will change from day to day.

Excel Workbook
ABCD
5StockPreviousCurrentCheck
6TLS1212OK
7BHP31ERROR
8NAB4545OK
9CBA77OK
10MQB2323OK
11WES88OK
Sheet1
 
Upvote 0
try
Code:
Sub Test()
Dim LastRow, i As Long
 
LastRow = Range("D" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
    If Cells(i, 3) <> Cells(i, 2) Then
         Cells(i, 4) = "ERROR"
    Else
        Cells(i, 4) = "OK"
    End If
 
 Next i
 
End Sub
 
Upvote 0
Why use VBA when a simple Excel IF function will suffice?

In D6 enter the formula =IF(B6<>C6,"Error","OK").

Now, move the cursor over the bottom-right corner of D6 and double-click. Excel will fill D as far down as you have data in C.

My data is not fixed therefore i dont want to use formula
 
Upvote 0
Awesome, i was 99% there, just had to press enter on the "ERROR" part.

Thanks


try
Code:
Sub Test()
Dim LastRow, i As Long
 
LastRow = Range("D" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
    If Cells(i, 3) <> Cells(i, 2) Then
         Cells(i, 4) = "ERROR"
    Else
        Cells(i, 4) = "OK"
    End If
 
 Next i
 
End Sub
 
Upvote 0
You could also do it like
Code:
Sub Test()
Dim LastRow, i As Long
LastRow = Range("D" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
If Cells(i, 3) <> Cells(i, 2) Then Cells(i, 4) = "ERROR" Else Cells(i, 4) = "OK"
Next i
End Sub
 
Upvote 0
You could also do it like
Code:
Sub Test()
Dim LastRow, i As Long
LastRow = Range("D" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
If Cells(i, 3) <> Cells(i, 2) Then Cells(i, 4) = "ERROR" Else Cells(i, 4) = "OK"
Next i
End Sub

Great, i have learnt something new today. Thanks
 
Upvote 0

Forum statistics

Threads
1,224,558
Messages
6,179,512
Members
452,920
Latest member
jaspers

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