VBA code problem

Yaron Yehezkel

New Member
Joined
Jul 11, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hey guys,

I have some problem with my code and i hope to fine here the solution.

I have a data of employees and their monthly salary. At the end of each table, in each of the 5 specific sheets, is the "Monthly Salary in NIS" column.
I need that the total amount of all employees monthly salary (the sum) will be placed under the column "Monthly Salary in NIS" and one cell after the last data line.

The problem is that in each sheet, where the "Monthly..." columns is, there are different number of rows and columns (because not all the employees worked all those 5 months). so the thing is that i need that mt code will sum dynamically those columns (if i will add more rows, the calculation still will be placed one cell after the last data line in the specific columns) - at my code i set a particular cell, which is not my intention but i couldn't find the solution:


VBA Code:
Sub SumIf()

    ActiveCell.FormulaR1C1 = "=SUM([Monthly Salary in NIS])"
       Range("F18").Select
       Sheets("employees 1").Select
      
    ActiveCell.FormulaR1C1 = "=SUM([Monthly Salary in NIS])"
       Range("G20").Select
       Sheets("employees 2").Select
      
    ActiveCell.FormulaR1C1 = "=SUM([Monthly Salary in NIS])"
       Range("H9").Select
       Sheets("employees 3").Select
      
    ActiveCell.FormulaR1C1 = "=SUM([Monthly Salary in NIS])"
       Range("I40").Select
        Sheets("employees 4").Select
       
    ActiveCell.FormulaR1C1 = "=SUM([Monthly Salary in NIS])"
       Range("J202").Select
       Sheets("employees 5").Select

End Sub


Thank you!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of one or two of your sheets. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
Ok,
So, I have a workbook full of sheets. five of them are called: Employees (1-5).
In each sheet, there is a table of data. In the end of the table there is a column called "Monthly salary in NIS".
The tables aren't equally with the columns and the rows.

I need to write a code that calculate the last column in those five sheets, the "Monthly salary in NIS" and present the calculation one cell after the last cell of data in that last column.

The problem is that i need it to be dynamically. It's means that if i'll add some more rows in the future, the calculation will still be presented one cell after the last cell of data in that column (the code that i sent in that post is referring to specific and permanent cell (for example - "Range("J202").Select").

* I've marked the sum spot in yellow. which need to be dynamically (but always in the last column!)

attached XL2BB of two of my sheets for the visual explanation.

Appreciate!


Excel Employees.xlsb
ABCDEF
1Employee IDFirst NameLast NameBranchDepartmentMonthly Salary in NIS
210001Accounting6210
310002Computing4095
410003Planning6066
510004Warehouse12060
610005Accounting7636
710006Sales6246
810007Transport8851
910008Development7704
1010009Sales6255
1110010Warehouse10350
1210011Sales7326
1310012Warehouse6817
1410013Planning4365
1510014Human Resources10071
1610015Development7470
17
Employees1



Excel Employees.xlsb
ABCDEFG
1Employee IDFirst NameLast NameBranchDepartmentPositionMonthly Salary in NIS
210001AccountingAssistant Director6210
310002ComputingSupporter4095
410003PlanningAssistant Director6066
510004WarehouseAssistant12060
610005AccountingHead of7636
710006SalesAssistant6246
810007TransportHead of8851
910008DevelopmentAssistant7704
1010009SalesDepartment Director6255
1110010WarehouseTeam Leader10350
1210011SalesEmployee7326
1310012WarehouseEmployee6817
1410013PlanningDepartment Director4365
1510014Human ResourcesTeam Leader10071
1610015DevelopmentHead of7470
1710016ComputingAssistant Director11448
1810017PlanningTeam Leader8505
1910018AccountingBranch Director12672
20
Employees2
 
Upvote 0
Try:
VBA Code:
Sub GetSum()
    Application.ScreenUpdating = False
    Dim LastRow As Long, lCol As Long, ws As Worksheet, ColLetter As String
    For Each ws In Sheets(Array("Employees1", "Employees2", "Employees3", "Employees4", "Employees5"))
        With ws
            lCol = .Cells(1, Columns.Count).End(xlToLeft).Column
            ColLetter = Replace(.Cells(1, lCol).Address(False, False), "1", "")
            LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
            .Cells(LastRow, lCol).Formula = "=sum(" & ColLetter & "2:" & ColLetter & LastRow - 1 & ")"
        End With
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Why not just add a total row to your table?
On the table design tab select Total row & then you can add the sum in there.
 
Upvote 0
Try:
VBA Code:
Sub GetSum()
    Application.ScreenUpdating = False
    Dim LastRow As Long, lCol As Long, ws As Worksheet, ColLetter As String
    For Each ws In Sheets(Array("Employees1", "Employees2", "Employees3", "Employees4", "Employees5"))
        With ws
            lCol = .Cells(1, Columns.Count).End(xlToLeft).Column
            ColLetter = Replace(.Cells(1, lCol).Address(False, False), "1", "")
            LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
            .Cells(LastRow, lCol).Formula = "=sum(" & ColLetter & "2:" & ColLetter & LastRow - 1 & ")"
        End With
    Next ws
    Application.ScreenUpdating = True
End Sub

That code is good for me but the thing is that it's continuing calculating the column each time i press the wired button.
Instead of writing a code, i tried to record Fluff idea and it actually worked perfect!

Thanks for your time and effort! Appreciate!
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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