Search for a cell with a negative value, then execute calculation, repeat in multiple sheets

cepinac

New Member
Joined
Jul 18, 2022
Messages
9
Office Version
  1. 2019
Platform
  1. MacOS
Hi everybody, I would kindly ask for your help regarding the advanced VBA function.
Scenario:
Range of cells E:T contains numbers. I would need a VBA function to search for a cell with a negative value. Once the negative cell is found, a value of the cell should be added to an adjacent cell on the right, then original negative cell content deleted. Repeat procedure until a value is positive or reaches column T.
i.e. Negative cell is E8 with value -100
step 1 - add value -100 to cell F8 with value 50
step 2 - delete value in E8
step 3 - repeat from F8 until final result is positive

There are multiple sheets in a workbook, ideally macro would loop through all of them with the same procedure.
Note: - minus number will show up always in the same column throughout all of the sheets (same column, not row!). Column with minus changes dynamically based other criteria.
Example sheet can be downloaded from link: example_minus_vba.xlsx

Thank you all :)
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try this:
VBA Code:
Sub cepinac()
Dim ws As Worksheet
Dim lclm As Integer
Dim lrow As Long
Dim i As Long
Dim j As Long
Dim k As Long


For i = 1 To Sheets.Count
    Set ws = Sheets(i)
    lclm = 20
    
    For j = 5 To lclm
        lrow = ws.Cells(Rows.Count, j).End(xlUp).Row
        
        For k = 1 To lrow
            If ws.Cells(k, j) <= 0 Then
                ws.Cells(k, j + 1) = ws.Cells(k, j + 1) + ws.Cells(k, j)
                ws.Cells(k, j) = ""
            End If
        Next k
    Next j
Next i

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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