• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Automatically Update Infomation In Cells When New Rows or Columns Are Added

Charles T

New Member
I've been having a lot of trouble on this and would really appreciate some help on the matter.

The following image shows an estimation of how many employee hours are going to be used per month:

upload_2013-11-22_16-6-34.png


The problem I am having is that a user can insert any number of "functional resources" or months. When the user adds new columns and/or rows to do this, I need to have the information contained in the formulas on the right hand side automatically updated.

For example, if the user were to add an additional 4 months (insert 4 columns) in the yellow highlighted section, the same amount of columns would be inserted in the white section above.

I've been trying various commands such as Offset and Index, however I cannot get it to work. Please help!!!

I have also uploaded an example of the excel file to make things more clear.
 

Attachments

  • Employee Hours.xlsx
    12.8 KB · Views: 11
Hi, Charles T!

As a new user you might want (I'd say should and must) read this:
http://chandoo.org/forum/forums/new-users-please-start-here.14/

And regarding your issue...

For the 1st question, I don't see any constraint regarding formulas in the worksheet. For adding a new row just copy row 24 on row 25 (the borders can be adjusted by conditional formatting but I think that this didn't raise your issue), and for adding a new month just copy column K inserting copied cells before column L.

For the 2nd question, try this in U16:
=SUMA(DESREF($M16;0;0;1;COLUMNA()-COLUMNA($M16))) -----> in english: =SUM(OFFSET($M16,0,0,1,COLUMN()-COLUMN($M16)))
and copy down as required.

Just advise if any issue.

Regards

PS: Hope that the T at your nick doesn't stand for Tévez :p
https://en.wikipedia.org/wiki/Carlos_Tévez
 
SirJB7,

Haha nope the last name isn't for Tévez...

The formula you provided worked perfectly!:D The table updates automatically when new columns are added.

One other question...Is there any function in excel or by VBA that would automatically insert a column instead of the user having to insert 2 columns every time they want to add a month?

For example, If I copy column K inserting copied cells before column L like you mentioned...I would also copy column U inserting copied cells before column V to update the formulas.

Is is possible to have excel automatically insert a column before V if i manually insert a column before L?

Thank you,

Charles
 
Hi, Charles T (aka El Apache)!

About your 1st question, glad you solved it. Thanks for your feedback and for your kind words too. And welcome back whenever needed or wanted.

About your new question...

Functions placed in Excel formulas are constrained to modify the value within that cells, they don't affect directly (indirectly of course, because of dependance references) other cells, and even less insert or delete ranges. A formula just displays something.

So your way is thru VBA. And yes it's possible thru worksheet events, but it'd be easier by assigning a procedure (macro) to a command button, that operates on active cell, for example.

You can give it a try by yourself using the built-in macro recorder: go to Programmer tab (if not enabled, go to File, Options, Customize Ribbon, and enable it), Code group, Record Macro icon), or click on the little button just at the right of the "Ready" message at the status bar.

Of course that the code won't be bullet proof but it's a kick-off to start getting your hands dirty with VBA. You can come back with the recorded code to get further help. Or you can just ask for the code directly, surely someone less lazy than me would help you... now. :p

Regards!
 
Hey SirJB7,

Haha the nicknames are great...

Yes! You are precisely correct and a macros is perfect for the application at hand. I created another excel file to add a macros. The ideas behind the file are the following:

(1) The user will enter the starting month in C8, the number of months the project is expected to take in C9, and the number of employees working on the project in C10.

(2) The user will then press the button "Calculate" and the excel file will automatically populate the proper amount of rows and columns based on the inputs in step 1.

(3)The starting month will be entered as the first column and continue until the duration of months is up. For example, If I specify the the starting month is September and the number of months is 3, the month columns will begin with September and end with November.

(3) If at some point the user decided that less employees or months are needed, they should be able to re-enter the inputs in step (1) above, press "calculate", and the approrate amount of rows or columns will be removed.

I don't have too much experience with macros and am having trouble carrying out the necessary commands above. Please find a copy of the excel file attached. As always your help is greatly appreciated.



Thank you,

-Charles
 

Attachments

  • Employee_Hours.xlsm
    27.1 KB · Views: 15
Hi, Charles T!

Give a look at this file:
https://dl.dropboxusercontent.com/u/60558749/Automatically Update Infomation In Cells When New Rows or Columns Are Added - Employee_Hours (for Charles T at chandoo.org).xlsm

It uses many dynamic named ranges for make formulas and ranges handling easy, basically this:
Single ranges:
MonthEmployeeHourCell: C5
StartDateCell: C8
MonthNoCell: C9
EmployeeNoCell: C10
TitleHeaderCell: B14 (previous row cell of EmployeeList)
TitleFooterCell: B27 (next row cell of EmployeeList)
TitleSeparator1Cell: C14 (previous column cell of MonthPercentageList)
TitleSeparator2Cell: L14 (next column cell of MonthPercentageList)
List ranges:
EmployeeList: B15:B26
=DESREF(Sheet1!$B$15;;;FILA(TotalFooterCell)-FILA(TitleHeaderCell)-1;1) -----> in english: =OFFSET(Sheet1!$B$15,,,ROW(TotalFooterCell)-ROW(TitleHeaderCell)-1,1)
MonthPercdentageList: D14:K14
=DESREF(Sheet1!$D$14;;;1;COLUMNA(TitleSeparator2Cell)-COLUMNA(TitleSeparator1Cell)-1) -----> in english: =OFFSET(Sheet1!$D$14,,,1,COLUMN(TitleSeparator2Cell)-COLUMN(TitleSeparator1Cell)-1)
MonthHourList: M14:T14
=DESREF(MonthPercentageList;0;COLUMNAS(MonthPercentageList)+1;; ) -----> in english: =OFFSET(MonthPercentageList,0,COLUMNS(MonthPercentageList)+1,,)

Changed the format of starting date from free text (string) to a valid date, formatted as "mmmm yyyy" (unquoted), so as to handle the formulas for MonthPercentageList (from where the yellow background was removed as it isn't anymore input but calculated) as:
D14: =FIN.MES(StartDateCell;COLUMNA()-COLUMNA(MonthPercentageList)) -----> in english:
=EOMONTH(StartDateCell,COLUMN()-COLUMN(MonthPercentageList))
and MonthHourlist as:
M14: =D14

This is the code for the worksheet Sheet1 class module:
Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    ' constants
    Const ksEmployeeNo = "EmployeeNoCell"
    Const ksEmployee = "EmployeeList"
    Const ksMonthNo = "MonthNoCell"
    Const ksMonthPercentage = "MonthPercentageList"
    Const ksMonthHours = "MonthHourList"
    ' declarations
    Dim rngENo As Range, rngE As Range, rngMNo As Range, rngMP As Range, rngMH As Range
    Dim iNumber As Integer
    ' start
    With Application
        .EnableEvents = False
'        .ScreenUpdating = False
    End With
    Set rngENo = Range(ksEmployeeNo)
    Set rngMNo = Range(ksMonthNo)
    If Application.Intersect(Target, rngENo) Is Nothing And _
      Application.Intersect(Target, rngMNo) Is Nothing Then GoTo Worksheet_Change_Exit
    ' process
    Set rngE = Range(ksEmployee)
    Set rngMP = Range(ksMonthPercentage)
    Set rngMH = Range(ksMonthHours)
    '  employee
    iNumber = rngENo.Cells(1, 1).Value - rngE.Rows.Count
    AddDeleteRowsColumns ActiveSheet, rngE, iNumber, 0
    '  month percentage
    iNumber = rngMNo.Cells(1, 1).Value - rngMP.Columns.Count
    AddDeleteRowsColumns ActiveSheet, rngMP, 0, iNumber
    '  month hour
    iNumber = rngMNo.Cells(1, 1).Value - rngMH.Columns.Count
    AddDeleteRowsColumns ActiveSheet, rngMH, 0, iNumber
    'end
    Set rngMH = Nothing
    Set rngMP = Nothing
    Set rngE = Nothing
Worksheet_Change_Exit:
    Set rngMNo = Nothing
    Set rngENo = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    Beep
End Sub

Private Sub AddDeleteRowsColumns(poWorksheet As Worksheet, poRange As Range, _
        piRows As Integer, piColumns As Integer)
    ' constants
    ' declarations
    ' start
    ' process
    With poWorksheet
        If piRows <> 0 Then
            Select Case piRows
                Case Is < 0
                    Range( _
                        .Rows(poRange.Row + poRange.Rows.Count + piRows), _
                        .Rows(poRange.Row + poRange.Rows.Count - 1)).EntireRow.Delete _
                            xlShiftUp
                Case Is > 0
                    poRange.Rows(poRange.Rows.Count).EntireRow.Copy
                    .Range( _
                        .Rows(poRange.Row + poRange.Rows.Count), _
                        .Rows(poRange.Row + poRange.Rows.Count + piRows - 1)).EntireRow.Insert _
                            xlShiftDown, xlFormatFromLeftOrAbove
            End Select
        ElseIf piColumns <> 0 Then
            Select Case piColumns
                Case Is < 0
                    Range( _
                        .Columns(poRange.Column + poRange.Columns.Count + piColumns), _
                        .Columns(poRange.Column + poRange.Columns.Count - 1)).EntireColumn.Delete _
                            xlShiftToLeft
                Case Is > 0
                    poRange.Columns(poRange.Columns.Count).EntireColumn.Copy
                    .Range( _
                        .Columns(poRange.Column + poRange.Columns.Count), _
                        .Columns(poRange.Column + poRange.Columns.Count + piColumns - 1)).EntireColumn.Insert _
                            xlShiftToRight, xlFormatFromLeftOrAbove
            End Select
            Application.CutCopyMode = False
        End If
    End With
    ' end
End Sub

The core is the procedure AddDeleteRowsColumns which does the dirty job.

Just advise if any issue.

Regards!

PS: I left the shape of Calculate button since it's not necessary as the code is triggered by the worksheet change event for cells C9:C10 (MonthNoCell & EmployeeNoCell). I also removed module Module1 as it was empty, leaving module Module2 as it was (even if not used) as it was your recorded code.
 
Last edited:
SirJB7,

Your answer is perfect and better than I ever expected any response to be:D:D. You have taught me so much and your ideas are helping me take my excel knowledge to the next level. I am grateful for this.

The file you have posted in your response is stunning. Great idea on keeping a neat track by using dynamic named ranges. Its going to take me time to go through the code step by step and learn the commands you have used and what you have done.

One thing...If I try and change anything in worksheet like the name of the employee or the % of time devoted to project, the program will stop working. Is there something I am doing wrong or is this how the file is meant to be? If it is this is not a big deal and is something I can work on.

Thank you so much for your help!!!


P.S. I apologize for posting this topic again in the forum
 
Last edited:
Hi, Charles T!
I don't get any error or unwanted stops. Get sure to download the file and save to your local disk, then open from there, write down any cell change that you make before it stops working and post them here. Cell and values. I'll check it tomorrow.
Regards!
 
SirJB7-

If I enter any date in the "Startdatecell" C8 for example, such as 1/1/14, the program becomes "dead" and won't update if I put in different numbers in cells C9:C10.

If I download the file and do not try to change anything, and all I do is enter in numbers in cells C9:C10, the program works fine. It is only when I try and change something in the worksheet that the program will stop working.

Let me know if you are confused and I will explain it more clearly.


Thank you,

Charles
 
I also found a bug that causes the file to act up...

If I input the number 4 into cell C9, press enter, and then input the number 12, 13, 14, or 15 into cell C9 and press enter, I get a runtime error and a pop up window that says the following:

"Run-time error '-2147417848 (80010108)': Method 'Insert' of obect 'Range' failed."

If I click the debug button, its opens the VBA editor and points me to the following section in the code:

.Range( _
.Columns(poRange.Column + poRange.Columns.Count), _
.Columns(poRange.Column + poRange.Columns.Count + piColumns - 1)).EntireColumn.Insert _
xlShiftToRight, xlFormatFromLeftOrAbove

This runtime errors seems to freeze excel as I have to start the task manager and force quit excel when it happens.

If I do the same thing in cell C10 as I mentioned above I do not get this runtime error.
 
Last edited:
Hi, Charles T!

Give a look at this file:
https://dl.dropboxusercontent.com/u...oyee_Hours (for Charles T at chandoo.org).avi

As you could see I entered valid dates at StartDateCell and the MonthPercentageList / MonthHourList ranges got correctly updated, and when entered invalid dates (adfsasdfasdf) only the displayed values returned value errors but never crashed nor hung.

Similarly when trying to enter invalid data at MonttNoCell / EmployeeNoCell the data validation clauses didn't let me go on. It's in Spanish but I think you'll get it.

If I were you I'd look more towards Excel options not properly configured, for example those regarding macros and access to VBA projects (File tab, Options entry, Trust Center option, Trust Center Setup button, Macro Configuration option (check that at least is Disabled All Macros With Notification -recommended- or higher and check on Trust on VBA Projects Object Model Access).

Otherwise let us start with the technical support check outs:
- Is there electrical supply at the computer location?
- Does the computer require a permanent connection to it?
- Is it turned on?
- Does it has a Windows OS installed?
- Is it updated?
- Does it have a M$ Office Suite installed?
- Does it include Excel application?
- Is it updated?
- Are you enabled to run applications?
- Have you closed all running applications except those in background?
- Have you started a new instance of Excel application?
- Have you opened the related workbook?
- Can you reproduce your issue?
- Described in detail the steps made in order to reproduce it, indicating every change in the displayed data.
- Are you still there?
...

Regards! :p
 
Hello SirJB7,

Please take a look at the following file:

https://dl.dropboxusercontent.com/s...oyee_Hours (for Charles T at chandoo.org).avi

The file begins to work fine, but when I change the date in C9 to 1/1/14, it won't update anymore when I change the values in C10 and C11.

I close the file and re-open it and it works fine again. Then I change an employee name to "Robert", and it won't update when I change the values in C10 and C11.

To answer your questions:

- Is there electrical supply at the computer location? Yes
- Does the computer require a permanent connection to it? YEs
- Is it turned on? Yes
- Does it has a Windows OS installed? Yes
- Is it updated? Yes
- Does it have a M$ Office Suite installed? Yes
- Does it include Excel application? Yes
- Is it updated? Yes
- Are you enabled to run applications? Yes
- Have you closed all running applications except those in background? Yes
- Have you started a new instance of Excel application? Yes
- Have you opened the related workbook? Yes
- Can you reproduce your issue? Yes
- Described in detail the steps made in order to reproduce it, indicating every change in the displayed data. Took video
- Are you still there? Yup


What are your thoughts?

Thank you,

Charles
 
Last edited:
Hi, Charles T!

My mistake, in this instruction:
If Application.Intersect(Target, rngENo) Is Nothing And _
Application.Intersect(Target, rngMNo) Is Nothing Then Exit Sub
replace this:
Then Exit Sub
by this:
Then GoTo Worksheet_Change_Exit

It quit without setting the application property EnableEvents to true again.

File uploaded with the update and posted code fixed too.

Regards!
 
Back
Top