VBA for search -> add -> delete

keresztesi

Board Regular
Joined
Aug 14, 2017
Messages
64
Hi,

I'd like to have a vba code for the following:

1. the code should search for numeric value in column "V" (there is either only one cell with number or all cells are blank). If there's a nr. then:
2. in the row where the number is (for example it's the 20th) it searches again for a number in range W20:AA20
3. then it searches again for numeric values in range W2:AA1000 and if it finds a value that is greater then the one found in range W20:AA20 then it adds the value from column "V"
4. finally it deletes the numeric value from column "V"

For example:

BEFORE:
VWXYZAA
1110
225
36
44
5
63
7
89
98
107

<tbody>
</tbody>

AFTER:
VWXYZAA
1115
2210
311
459
5
63
7
814
913
1012

<tbody>
</tbody>

Thx in advance!

Zoli
 
Ok, so let's make it so:

Col 'V' := column where it should search for a number
Range : X:AB
In case there are more values in range then it should select the value given in column 'W'

Is it ok so?

If you put a value in column AB for example then its ok yes.
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Lets try this. Try on a copy workbook first! There are no sheets here so make sure the sheet you want it to work on is the active sheet when you run it.

Code:
Dim lr As Long, rw As Long, myVal1 As Double, myVal2 As Double, arr

'no value in column V exit
If Application.Count(Columns("V")) = 0 Then Exit Sub

'get various values
lr = Columns("V:AB").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
rw = Evaluate("MATCH(TRUE,INDEX(ISNUMBER(V1:V" & lr & "),0),0)")
myVal1 = Evaluate("INDEX(V1:V" & lr & "," & rw & ")")
cnt = Application.Count(Range(Cells(rw, 24), Cells(rw, 28)))

If cnt <> 1 Then
    myVal2 = Cells(rw, 23)
Else
    myVal2 = Evaluate("INDEX(X" & rw & ":AB" & rw & ",MATCH(TRUE,INDEX(ISNUMBER(X" & rw & ":AB" & rw & "),0),0))")
End If

'add range into array
arr = Range("X1:AB" & lr)

'alter array as appropriate
For i = LBound(arr, 1) To UBound(arr, 1)
    For j = LBound(arr, 2) To UBound(arr, 2)
        If arr(i, j) > myVal2 Then arr(i, j) = arr(i, j) + myVal1
    Next
Next

'put array back on worksheet
Range("X1:AB" & lr) = arr

'clear column V
Columns("V").ClearContents
 
Upvote 0
Try this instead:

Code:
If IsNumeric(arr(i, j)) And arr(i, j) > myVal2 Then arr(i, j) = arr(i, j) + myVal1
 
Upvote 0
Ok, it runs but please, correct 2 things:

1. it doesn't add the number to the first number in the range (to the value in the where the founded number is)
2. after run delete column 'W' as well

Thx


Try this instead:

Code:
If IsNumeric(arr(i, j)) And arr(i, j) > myVal2 Then arr(i, j) = arr(i, j) + myVal1
 
Upvote 0
I think it does the trick:

If IsNumeric(arr(i, j)) And arr(i, j) >= myVal2 Then arr(i, j) = arr(i, j) + myVal1

Sorry for my english :)

Ok, I explain it by example:

The code finds number '5' in column "V" and in row nr. 10.
In row 10 in range X10:AB10 there is a number 20.

The code should add 5 to 20 :)
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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