VBA to find the first value in a column greater than or equal to 1, then deduct a value from that cell

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

Is there a way to use VBA to find the first value in a column that's greater than or equal to 1, then deduct a value from that cell?

Eg I have a list of Numbers in column D in Sheet 1.

In the simple example below, the title 'Numbers' is in cell D1, and the values 0, 1, 2, and 3 are in cells D2, D3, D4 and D5, respectively.

I'd like to run a Sub which finds the first value greater than or equal to 1 then deducts the value in cell A1 in Sheet 2 from the cell found in column D in Sheet 1.

In this case, the value in cell A1 in Sheet 2 is 1.

I'm sure this is possible, but would be grateful for your thoughts?

TIA




Numbers
0​
1​
2​
3​
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this.
VBA Code:
Sub findone()

Dim lastrow_1 As Integer
Dim varray As Variant
Dim a As Long

lastrow_1 = Sheets("Sheet1").Cells(Rows.Count, "D").End(xlUp).Row
varray = Sheets("Sheet1").Range("D1:D" & lastrow_1).Value

For a = 1 To UBound(varray, 1)
    If varray(a, 1) >= 1 Then
        ActiveSheet.Cells(a, 4).Value = ActiveSheet.Cells(a, 4).Value - Sheets("Sheet2").Range("A1").Value
        Exit Sub
    Else
    End If
Next
End Sub
 
Upvote 0
Hi @tico_ocit

Thanks for the prompt response to this question.

I tried to run the code above, but I got a 'Run time error: 13. Type mis-match'

And the code stopped at this line:

ActiveSheet.Cells(a, 4).Value = ActiveSheet.Cells(a, 4).Value - Sheets("Sheet2").Range("A1").Value

Do you know why that may be?

Thanks in advance.
 
Upvote 0
Do you have a header in D1?
If so try
VBA Code:
For a = 2 To UBound(varray, 1)
 
Upvote 0
Yes, there was already a header in D1.

And yes, that change to the code you suggested above made it work!

Amazing!!!

Thank you both!!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,058
Latest member
oculus

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