VBA Help? If statement compare sum ranges two columns

andrewdp3

New Member
Joined
Jun 25, 2011
Messages
10
Hello,

I've been trying different pieces of code to figure this one out but to no avail. I have a listbox on a form that populates when users select an option button. When they select something from the listbox and click ok the code makes a copy of a template sheet and places in workbook. It also renames the sheet with the name of the selected item in the listbox. Last it takes the same name selected in the listbox and adds it to the next empty spot on a sheet called "Main" (Column B).

All of this works fine except I need one final piece to the code for everything to work. The idea would be an "if" statement but I'm unsure exactly how?

If the sum of D4:D75 is greater than the sum of E4:E75 of the same sheet that is created and named. I would like to subtract the sum of E4:E75 from the sum of D4:D75 and place that answer on the current row of main sheet in the D column.

Else subtract the sum of E4:E75 from the sum of D4:D75 and place that answer on the current row of the main sheet in the E column? Is this possible??

Sheets("Main").Cells(curRow, 4).Formula = Sum(Sheets(Me.lbxAccount.Value).Range("E4:E75"))
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Code:
With Sheets(Me.lbxAccount.Value)
    Result = WorksheetFunction.Sum(.Range("D4:D75")) - WorksheetFunction.Sum(.Range("E4:E75"))
End With
If Result >= 0 Then
    Sheets("Main").Cells(curRow, "D") = Result
Else
    Sheets("Main").Cells(curRow, "E") = Result
End If
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,683
Members
452,937
Latest member
Bhg1984

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