Improve VBA

Kazdima

Board Regular
Joined
Oct 15, 2010
Messages
226
Hello,

would you please improve the macro? The issue arises when I have numbers in both columns (A and B) in Debit and Credit.
For example In Debit 1,000 and in Credit 1,200. So, in column A should be (200) after running a Macro.

Sub MoveCredits_v3()
With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row)
.Value = Evaluate(Replace(Replace("if(#="""","""",if(#=0,-^,#))", "#", .Address), "^", .Offset(, 1).Address))
.Offset(, 1).Clear '<- Optional to clear Credit column
End With
End Sub
 
I haven't supplied a formula, I have only stated what you code is doing!
If you simply want to deduct col A from col B then try
Code:
Sub Kazdima()
   With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate("if({1}," & .Offset(, 1).Address & "-" & .Address & ")")
   End With
End Sub
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Not deduct, but add. Column A is always "+", column "B" is always "-". So whichever number is higher that sign prevails. Exactly, like I explained in my first posting
 
Upvote 0
Exactly, like I explained in my first posting
Whilst you may think that this is true, please bear in mind that I cannot see your worksheet, not can I read your mind. ;)
It was not clear to me what you were asking for & the code you supplied was doing nothing like what you wanted. That is why I tried to get clarification from you, before wasting my time doing something you did not want.
So, simply put, you want to add column A & column B without any criteria, with the result in column A. Is that correct?
 
Last edited:
Upvote 0
Please see an example, may it is more clear-:)
[TABLE="width: 321"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Column A
[/TD]
[TD]Column B[/TD]
[TD] Should be in Column A [/TD]
[/TR]
[TR]
[TD]500.00
[/TD]
[TD]200.00[/TD]
[TD] 300.00[/TD]
[/TR]
[TR]
[TD]20,000.00[/TD]
[TD]25,313.93[/TD]
[TD]- 5,313.93[/TD]
[/TR]
[TR]
[TD]300.00[/TD]
[TD]800.00[/TD]
[TD]- 500.00[/TD]
[/TR]
[TR]
[TD]0.00[/TD]
[TD]90,000.00[/TD]
[TD]- 90,000.00[/TD]
[/TR]
[TR]
[TD]0.00
[/TD]
[TD]28,865.00[/TD]
[TD]- 28,865.00[/TD]
[/TR]
[TR]
[TD]50,000.00[/TD]
[TD]0.00[/TD]
[TD] 50,000.00
[/TD]
[/TR]
[TR]
[TD]150.00[/TD]
[TD]180.00[/TD]
[TD]- 30.00[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Try this:

Code:
Sub Results()
'Modified 4/30/2019 2:15:32 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To Lastrow
Cells(i, 1).Value = Cells(i, 1).Value - Cells(i, 2).Value
Next
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
That example shows that you want to subtract col B from col A, not add.
In which case try
Code:
Sub Kazdima()
   With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate("if({1}," & .Address & "-" & .Offset(, 1).Address & ")")
   End With
End Sub
However if col B numbers are all negative try
Code:
Sub Kazdima()
   With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate("if({1}," & .Address & "+" & .Offset(, 1).Address & ")")
   End With
End Sub
 
Upvote 0
In Post 12 you said:
Column A is always "+"

But in Post 14 your showing the results in column A will be a negative.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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