VBA With an if statement

ENTERTAIN

New Member
Joined
Sep 6, 2017
Messages
29
I have a code which works fine . It takes changing values from cell "AB10" together with the small value of .0001 and lists them in column "AJ"
The additional small value is required for conditional formatting elsewhere but does not effect the final result which is in a cell that is formatted to ignore decimal places.
The code I use is

Sub_Tally_Values
Range("AB10"). CopyRange("AB10").Value=Range("AB10")+0.0001
Range("AJ" & Rows.Count). End (xlUp). Offset(1,0).PasteSpecial xlPasteValues
End Sub

My problem comes when I try to add an if statement as a condition of it to run. The code I want to place in front of it would only let it run if "AB6" or AB13 is less than 0.0003
I tried If Range("AB6").Value < 0.0003 Or Range("AB13").Value < 0.0003 Then - - - I do not know where to place the End if What ever I try does not work.
Any help with this would be appreciated
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try
VBA Code:
Sub_Tally_Values
If  Range("AB6").Value < 0.0003  Or Range("AB13").Value < 0.0003 then
   Range("AB10").Value=Range("AB10")+0.0001
   Range("AB10"). Copy 
   Range("AJ" & Rows.Count). End (xlUp). Offset(1,0).PasteSpecial xlPasteValues
end if
End Sub
 
Upvote 0
It takes changing values from cell "AB10"
If AB10 has changing values, do you really need to add the 0.0001 to it or would it be just as good to add that amount in column AJ?

If so, try

VBA Code:
Sub Tally_Values()
  If Range("AB6").Value < 0.0003 Or Range("AB13").Value < 0.0003 Then
    Range("AJ" & Rows.Count).End(xlUp).Offset(1, 0).Value = Range("AB10").Value + 0.0001
  End If
End Sub
 
Upvote 0
If Range("AB6").Value < 0.0003 Or Range("AB13").Value < 0.0003 Then Range("AJ" & Rows.Count).End(xlUp).Offset(1, 0).Value = Range("AB10").Value + 0.0001 End If End Sub
Thank You Peter for your reply. That code has done the job for me
 
Upvote 0
Here a simplified way of the code.

VBA Code:
Sub Tally_Values_2()
  If [AB6] < 0.0003 Or [AB1] < 0.0003 Then Range("AJ" & Rows.Count).End(3)(2).Value = [AB10] + 0.0001
End Sub
 
Upvote 0
Here a simplified way of the code.
I don't know how often the OPs value changes or how many times it changes altogether so it might not make a significant difference, but I do note the shorter way to write the code is the longer way to execute it.
 
Upvote 0
Thanks Peter for your invaluable advice they are always welcome.
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,167
Members
448,870
Latest member
max_pedreira

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