Need VBA script to extend Excel table every week - vba

The problem is as follows: I have several tables in Excel with given number of raws and dynamic number of columns (each week a new column should be added, currently I'm doing it manually). I want to automate it and create a script that will extend every raw range to the next column (namely the range from A2 to C2 should become from A2 to D2), and so on (such that running script N times will result in extending a table to N columns further). By "extending" I mean extending formulas, since each cell in my tables contains any formula. Is there any way to do it via VBA?
I can't just record the corresponding macro because I have now idea how to specify that I don't want to link it with any specific range, but instead always extend just to one column right.
Any help and examples will be very appreciative.

You dont need VBA to do this. Use dynamic defined names and reference them in your formulas. For example, if you add a named range and add this in the refersTo dialog
=OFFSET($A$1,0,0,COUNTA($A:$A),COUNTA($1:$1)
your range will automatically expand from cell A1 (as long as there are no blank cells in column A or Row 1). You can then use that named range in your formulae.
More here http://www.excel-easy.com/examples/dynamic-named-range.html

Related

Conditional formatting 1 cell based on cell input on another sheet, apply to entire row

For work I need to create a resource management excel file. My goal is to create an overview as is seen here:
If John would have taken a few hours off, or if he has a fey hours of sick leave, I would like to turn this cell only to change colour so I know that John will be absent for whatever reason on this day.
However, since I'm creating this for an entire row (for a whole year), I do not want to create conditional formatting per cell because that would be plain madness.
Here is an example per employee (in this example John):
enter image description here
So what I need is a formula to check if a cell in a row (for example sick leave) on the employee worksheet is grater than 0 and then change the colour of only the corresponding cell on the recourse planning worksheet, not the entire row.
Does any of you guys know if this is possible in excel 2016? Preferably without VBA scripting since I have to transfer this excel file to a co-worker who is not into VBA programming.
Thank in advance.
Nuntius transmittendus!
This is definitely possible without any code. Use CountIf() as tinus087 commented. The tricky part is how to create the address range used by the CountIf() so that you can put one conditional format on all the cells. On the the one hand you need the column of the cells being checked to match the column of your total cell. On the other hand the worksheet tab name needs to change depending on which row you are on. So use INDIRECT() to point to the correct worksheet and OFFSET() to look at the correct cells.
So, for cell C4, the formula you want to use as the range in your CountIf() is OFFSET(INDIRECT($A4&"!A1"),3,COLUMN(C1)-1,4). When applied to cell C4 this creates address John!C4:C7. When used for cell D4, it results in John!D4:D7.
You'll probably want some error checking to deal with the #REF! error happens when the formula is applied to a row without a first name or where the name listed doesn't match any worksheet tabs.
Something else to think about, if you haven't already, what happens if there are two John's? Is the 2nd John going to be named John2?

Trying to use excel formula or VBA

I have two different workbooks. Book 1 and Book 2 both have the same number of columns A through M.
I want to do match the records between two workbook, for example: I have a column A name Birthday, Column B City, Column C Passport Number......., in worksheet 1 & 2. I want to match worksheet 1 Cell A1 from the Range A:A worksheet 2, If the record in column A cell 13 not matching it shows Birthdate not match in N13 Workbook 2, If it does not match with worksheet 1 Cell 13 from the Range B:B worksheet 2 it shows city not MATCH in Column N 13 in workbook 2, and so on till column M.
I am using the formula below but it's not working properly, I don't know what I am missing and what formula should I add in. I have no idea about VBA. But I want to see is it easier to do by using excel formula or vba?
IF(COUNTIF(Target!$A$2:$A$5964,Source!A8)=0,"Birthday",IF(COUNTIF(Target!$B$2:$B$5964,Source!B8)=0,"City",IF(COUNTIF(Target!$C$2:$C$5964,Source!C8)=0,"Country",IF(COUNTIF(Target!$E$2:$E$5964,Source!D8)=0,"Passport Number Mismatch in Target",IF(COUNTIF(Target!$F$2:$F$5964,Source!E8)=0," Travel Date Mismatch in Target",IF(COUNTIF(Target!$G$2:$G$5964,Source!F8)=0,"First Name Mistmatch in Target",IF(COUNTIF(Target!$H$2:$H$5964,Source!G8)=0,"Full Name Mismatch in Target","Match in Target")))))))
Thanks in Advance.
VBA has access to these same worksheet formula functions (e.g. COUNTIF): there really aren't column or matrix functions that VBA has that formulas don't have.
However, VBA lets you write loops (e.g. while, for), it allows if-statements, procedure calls, and many lines of code so your calculations can have more steps and hence be more complex. VBA also lets you have temporary space in the form of arrays (and strings and objects, too) (so you don't necessarily need to use columns for temporary space as one might do with formulas). VBA also allows recursion, which makes some calculations easier (to some definition).
VBA provides an imperative programming model. VBA procedures can read and write any cell of the spreadsheet. Imperative programming, on the other hand, needs to be triggered somehow such as by using a button.
By contrast, the data-flow programming model with formulas will automatically recalculate whenever their input sources change, which is good. But there are some cases it doesn't handle naturally (e.g. recursion).
Another option is to combine VBA with formulas, by writing new formulas that are then implemented in VBA. If you are doing that, the VBA can only return information thru function return values; it cannot otherwise modify the spreadsheet.
So, if you can think of how to do this easier using loops (and arrays) or recursion and maybe with a button to trigger the computation (or by using custom formulas) then VBA might be interesting.

#REF! result when using INDEX function in Excel

I am trying to create a couple of reports from data on another Excel worksheet based on the value in a drop down list. I am using the MATCH and INDEX functions and have created Named Ranges of the columns of data. I am able to get the first value I want in the report but none of the others, even though when I debug by evaluating the formula it points to the right cell but still displays #REF! instead of the actual value from the referenced cell.
I'll do my best to make this clear:
In "POST_Data" worksheet I have 4 columns titled Course Name, Course Length, Attendee and Date Attended. Currently I have 33 rows of data (plus the header row) but I need the reports to be dynamic since new data will be added from time to time.
I have created Dynamic Named Ranges of the data using the OFFSET function (e.g. for the Course Name data I have a NameRange called CourseNamesData = OFFSET(POST_Data!$A$2,0,0,COUNTA(POST_Data!$A:$A),1)
In the "DashBoard_and_Data Entry" worksheet I have a two report areas: one to report the Course Name and Date Attended for a specified Attendee (specified by a drop down list in cell C7) and the other report to provide the Attendee Name and Date Attended for a specified Course Name (specified by a drop down list in I7).
In row 8 I report the column that the data belongs to in the POST_Data worksheet
What IS working: When I choose an Attendee from the drop down list in C7, I correctly report the first of the Course Names for this attendee from the data in the POST_Data worksheet. I used the following formula to do so: =INDEX(CourseNamesData,MATCH(C7,AttendeeNamesData,0),B$8)
What is NOT working: The corresponding "Date Attended" data when I use a similar formula as the one that is working. I have: =INDEX(DateAttendedData,MATCH(C7,AttendeeNamesData,0),C$8) but this gives me the #REF! error.
Again, when I try to follow the data that this formula points to, it looks like it is pointing to the correct cell but not showing the result.
Another issue is how to get all the data corresponding to the choice in the drop down and not just the first row. So for example, if I choose Richards, K. from the attendee list and he has attended 4 training courses, I need all 4 to show up, not just the first one.
I appreciate any help or insights on this. If you know of a better way to display the workbook contents, please let me know.
Thanks!
Can you provide a screenshot so that I can better understand the issue?
With regard to your post_data worksheet, you might find it easier to turn the data into a table. You can then point your named range to the column within the table without needing to use the offset function, and your range will update automatically as you add to the table.
I have a entire playlist on my youtube channel devoted to using ranges and tables if you are interested. https://www.youtube.com/playlist?list=PL1nLTDk2QLL9415OPSjIICJs1EeV-HeK3

Getpivotdata Pivot location based on a value in another cell.

GETPIVOTDATA("Data",{Dynamic reference to another sheet},"Field","Item")
I'm trying to get my formula to be super dynamic. I have 5 - 10 different worksheets with pivot tables on them. I have the rest of the formulas setup to get their "FIELD" and "ITEM" off of a small table of data that can change. I have tried named ranges but again cannot get it to work. The only thing in the getpivot i cannot make dynamic is the location of the pivot table. I was shown the CHOOSE(1,"STRINGS") function, but I do not know how many pivots there will be. I've used the INDIRECT() with vlookups. I'm fresh out of ideas any help welcome.
Using a named range to refer to the pivot table should work. However, make sure the named range only refers to a single cell within the pivot table. If it refers to a range, or the whole table it will not work.
EDIT
If you want to pull the pivottable from another cell, you'll need to use the INDIRECT function
=GETPIVOTDATA("Amount",INDIRECT($Q$2),"Name","Jack","Month","Feb")
Cell $Q$2 can contain either a named range referring to the pivot table or a cell reference. In either event, you will also need to specify the worksheet if you're working across multiple sheets. eg, $Q$2 could contain
Pivots!$A$1

Setup an Excel template so calculations are not dependant on a specific number of columns / rows

Problem Statement:
I'm creating a template for multi tiered complicated calculations in MS Excel that depend on a few input "n x 3" matrices.
It is really difficult to redesign the 15 sheets or so (200 ~ 300 lines each) every time I have a different "n" where "n" ranges from 3 to 900.
Goals:
I'd like to input the number of "n" in a cell subsquently changing all the other sheets in the workbook accordingly.
Avoid VBA as much as possible
How can I achieve these goals?
Note: I'm willing to answer any questions or comments concerning my issue
EDIT
"n" represents the number of columns / rows, if n = 3, all calculations will be for a 3 x 3 matrix. If n = 500, all calculations will be for a 500 x 3 matrix.
I want Excel to do the expansions / contractions of the rows automatically, so i do't have to do them myself accross hundreds of tables
In Excel 2007 turn your data matrices into tables.
This can be done by clicking on a matrix and then on the Insert tab select Table. The keyboard shortcut for this functionality is Ctrl-L or Ctrl-T. Multiple tables can exist on the same worksheet.
Once your data is marked as a table, the table will dynamically expand when new data is added.
Each table is automatically given a name, starting with Table1. The name can be change via the Table tools - Design tab.
In formulas each table can be referenced by it's name.
=SUM(Table1)
Each column heading in the table is also usable in formulas.
=SUM(Table1[Column1])
In versions of Excel prior to 2007, 'Dynamic named ranges' can be used.
These can be created via the Insert - Name - Define menu.
Give the 'Dynamic named range' a name (e.g. Table1) and enter a formula similar to the following assuming your matirx starts in cell A1:
=OFFSET(Sheet1!$A$1,1,0,COUNTA(Sheet1!A:A)-1,3)
If your matrix starts in cell D10 the formula would look like this
=OFFSET(Sheet1!$D$10,1,0,COUNTA(Sheet1!D:D)-1,3)
This formula excludes any column heading in the matrix. It selects data on a 'n x 3' basis.
In formulas each 'Dynamic named range' can be referenced by it's name.
=SUM(Table1)
You will need to review the layout of your worksheet as the dynamic named range works out it's number of rows by counting all items that appear in the first column of data.
If you have cells populated above and/or below your matrix they will be included in the calculation and the 'Dynamic named range' will include rows below your data matrix.
To see which cells are included in a 'Dynamic named range' or table press F5 and type in it's name, then click on OK.
Create defined names (Insert - Name - Define) that use OFFSET and COUNTA to make dynamic ranges. Instead of
=SUM(A1:A300)
use
=SUM(MyRange)
where MyRange is
=OFFSET($A$1,0,0,COUNTA($A:$A),1)
See also
Instead of calculating against A1:A300, you can calculate against A:A, which is the entire column.
You may have to bear in mind that you don't want other stuff in that column when you design your layout.
In more recent version of Excel, you can select sets of data and format them as a table, in which case you can use the table column, for example Table1[Column2].