Modify the code to change positive numbers to negative numbers in arrays

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
172
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone. I need to correct the code below, when outputting the number to column E, change the positive number to Negative (Like the image described below). Hope everyone can help, thank you very much!

1678164983859.png


I tried to fix it like this but the code is not working properly
arr(ik, 2) = (arr(ik, 2) + CDbl(s(1))) * -1

VBA Code:
Sub totalsum()
On Error Resume Next
  Dim Dic As Object, dArr As Variant, arr As Variant, Tmp As Variant, s As Variant
  Dim i As Long, J As Integer, K As Long, ik As Long, key As String, b As Long, C As Long
  Set Dic = CreateObject("Scripting.dictionary")
  dArr = Range("B3:B11").Value
  ReDim arr(1 To UBound(dArr), 1 To 3)
  For i = 1 To UBound(dArr)
      Tmp = Split(dArr(i, 1), ";")
      For J = LBound(Tmp) To UBound(Tmp)
          s = Split(Tmp(J), "*")
          If UBound(s) = 2 Then
              key = s(0)
              If Not Dic.Exists(key) Then
                  K = K + 1
                  Dic.Add key, K
                  arr(K, 1) = key
              End If
              ik = Dic.Item(key)
              arr(ik, 2) = (arr(ik, 2) + CDbl(s(1)))
              arr(ik, 3) = arr(ik, 3) + CDbl(s(2))
              
          End If
      Next J
 
  Next i
Range("D3:E11").ClearContents
Range("D3:E11").Resize(K) = arr

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi Excelpromax123,

Try changing the plus signs in these two lines...

VBA Code:
arr(ik, 2) = (arr(ik, 2) + CDbl(s(1)))
arr(ik, 3) = arr(ik, 3) + CDbl(s(2))

...to minuses:

VBA Code:
arr(ik, 2) = (arr(ik, 2) - CDbl(s(1)))
arr(ik, 3) = arr(ik, 3) - CDbl(s(2))

Not sure why for b I'm getting 40 not 43.

Regards,

Robert
 
Upvote 1
Hi Excelpromax123,

Try changing the plus signs in these two lines...

VBA Code:
arr(ik, 2) = (arr(ik, 2) + CDbl(s(1)))
arr(ik, 3) = arr(ik, 3) + CDbl(s(2))

...to minuses:

VBA Code:
arr(ik, 2) = (arr(ik, 2) - CDbl(s(1)))
arr(ik, 3) = arr(ik, 3) - CDbl(s(2))

Not sure why for b I'm getting 40 not 43.

Regards,

Robert
Thank You !
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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