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:
[TABLE="class: grid"]
<tbody>[TR]
[TD][/TD]
[TD]V[/TD]
[TD]W[/TD]
[TD]X[/TD]
[TD]Y[/TD]
[TD]Z[/TD]
[TD]AA[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]2[/TD]
[TD][/TD]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]9[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]8[/TD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Zoli
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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