"SUM" by headings

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
882
Hi all, kindly advise me so that to create a VBA code which should “SUM” the headings and rows down in columns “SALARY” & “ACTUAL SALARY”. Therefore, that the above headings have no stably column, but the row “2” is constantly. Below I present the original data and the expected result. Thanking you in advance

Original data

PQRST
1COMPANY
2EMPL. NAMEEMPL. CODESALARYACTUAL SALARYDIFFERENCE
3EMPLOYEE NAME14500101,570.001,480.00
4EMPLOYEE NAME24501461,850.001,588.00

<tbody>
</tbody>


Expected result

PQRST
1COMPANY
2EMPL. NAMEEMPL. CODESALARYACTUAL SALARYDIFFERENCE
3EMPLOYEE NAME14500101,570.001,480.0090.00
4EMPLOYEE NAME24501461,850.001,588.00262.00

<tbody>
</tbody>
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
See if this does what you want.
Code:
Sub Insert_Formula()
  Dim rSalary As Range, rActualSalary As Range, rDifference As Range
  
  Set rSalary = Rows(2).Find(What:="salary", LookAt:=xlWhole, MatchCase:=False)
  Set rActualSalary = Rows(2).Find(What:="actual salary", LookAt:=xlWhole, MatchCase:=False)
  Set rDifference = Rows(2).Find(What:="difference", LookAt:=xlWhole, MatchCase:=False)
  If Not rSalary Is Nothing And Not rActualSalary Is Nothing And Not rDifference Is Nothing Then
    With rDifference.Offset(1).Resize(Cells(Rows.Count, rSalary.Column).End(xlUp).Row - 2)
      .FormulaR1C1 = "=RC[" & rSalary.Column - .Column & "]-RC[" & rActualSalary.Column - .Column & "]"
    End With
  End If
End Sub
 
Upvote 0
Thank you so much Peter! Is perfect work. Many thanks also for your continued support. Hv a great, lovely day!
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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