What's the problem exactly? That code is perfectly valid as long as x is a valid value and both cells are numeric.
What's the problem exactly? That code is perfectly valid as long as x is a valid value and both cells are numeric.
You may think that, but it is valid, subject to the items I mentioned. What is the full code, what is the error, and what is in the cells in question when the error occurs?
If x = 1 then it will subtract B1 from A1. If x has another value it will subtract Bx from Ax. You still have not explained what your problem is.
Cells(x,5) = Cells(x,1) -
Dim numbers(), arrItem, maxItem, result, searchNum
Dim x As Integer
x = 1
searchNum = Cells(x, 1) - Cells(x, 2) 'search number
numbers = Array(1, 3125, 3542, 4375, 6042, 8958, 13542, 23958)
maxItem = Application.Max(numbers) 'get max of array item
For Each arrItem In numbers 'loop through each array item
If Abs(searchNum - arrItem) < maxItem Then
maxItem = Abs(searchNum - arrItem)
result = arrItem
End If
Next arrItem
Cells(x, 7) = searchNum - Results