Auto-updating cell ranges based on date - vba

I currently have an excel document with details of invoices on each row for nearly 5000 rows. These rows constantly have new invoices added to the bottom of them.
I am looking for a way to be able to assign the invoices that were raised within the previous month to a range prior to analysing them. There could be several invoices raised on the same day and they may not (although they should be) in order
The dates are within the first column of the workbook (A:A).
Having had a look around I thought I had come up with two solutions which created an auto extending named range but neither of them worked:
=OFFSET((DATE(YEAR(TODAY()),MONTH(TODAY())-1,1)),0,0,COUNTA(DATE(YEAR(TODAY()),MONTH(TODAY())-1,1)):(DATE(YEAR(TODAY()),MONTH(TODAY()),1),1))
and
=OFFSET((DATE(YEAR(TODAY()),MONTH(TODAY())-1,1)),0,0,COUNTA(DATE(YEAR(TODAY()),MONTH(TODAY())-1,1)):(DATE(YEAR(TODAY()),MONTH(TODAY())-1,31),1))
The reason that I am trying to get this to work with Name Manager and OFFSET is to use the named range within VBA so if anyone is aware of an easier way to do this within VBA that would also be great.

Related

Complex IF. formula in Excel

I checked various posts on IF formulas but I cannot find a way to receive the correct result in my report. I manage deliveries and I would like to calculate the delay days basing on the data from delivery report. The trick is that the delay will depend on the status of delivery, as in each case I have to consider a different date and column in Excel. These are the data:
Status of delivery:
Confirmed
Unloaded
Unloading
Not confirmed
Started
In route
Pick-up pending
Prepared
This delivery status is updated in C column in my Raw Data report. For each, I will have to calculate the delay in a different way therefore I figured that IF formula could be of use.
Below you can see the columns that contain the relevant dates for the calculations:
Status of delivery and reference date:
Confirmed - D
Unloaded - D
Unloading - D
Not confirmed - S
Started - D
In route - S
Pick-up pending - E
Prepared - S
I made this formula as below, sadly, only the first record is calculated correctly, the rest of the delays is "null".
=IF(C2="Confirmed";(TODAY()-D2);IF(C2="Unloaded";(TODAY()-D2);IF(C2="Unloading";(TODAY()-D2);IF(C2="Not confirmed";(TODAY()-S2);IF(C2="Started";(TODAY()-D2);IF(C2="In route";(TODAY()-S2);IF(C2="Pick-up pending";(TODAY()-E2);IF(C2="Prepared";(TODAY()-S2);"null"))))))))
Do you happen to have any idea where am I making the error which I don´t see? I will be grateful for any help. If it´s also relevant, I am using Excel 2016.
Breaking it down so the long line becomes readable.
=IF(C2="Confirmed";
(TODAY()-D2);
IF(C2="Unloaded";
(TODAY()-D2);
IF(C2="Unloading";
(TODAY()-D2);
IF(C2="Not confirmed";
(TODAY()-S2);
IF(C2="Started";
(TODAY()-D2);
IF(C2="In route";
(TODAY()-S2);
IF(C2="Pick-up pending";
(TODAY()-E2);
IF(C2="Prepared";
(TODAY()-S2);
"null"
)
)
)
)
)
)
)
)
At first glance, what I'm looking at is basically a vertical lookup schedule here.
So, I created these two colums. One is the text status we're looking for.
The other is the calculated date.
Single date VLOOKUp
Assuming D2, S2 and E2 are fixed fields I made the formula =TODAY() - $D$2
Then it's a simple matter of doing the VLOOKUP in the correct field.
Because we're working with a date type field we need to convert the number to text to get a meaning full date. (I used JJ in the screenshot for years because I have a dutch locale)
Then we also need to handle when VLOOKUP can't find anything, for that we use IFERROR.
=IFERROR(TEXT(VLOOKUP(F6;$A$2:$B$9;2);"MM-DD-YY");"NULL")
And now you have an easy to expand lookup table you can put anywhere, hide on a worker tab, etc.. where you can calculate your values, where you can add and remove values.
Many rows, many dates
But, say you have many rows with different statusses and dates, and you wish to know the number of days it'll take. Then this vertical lookup doesn't look so useful because it can only be used for one row.
We can still leverage VLOOKUP to make our life a bit easier
Assuming there are dates in colums D, E and S
=IFERROR(DATEDIF(TODAY();INDIRECT(ADDRESS(ROW();VLOOKUP(F2;$A$2:$B$9;2;FALSE)));"d");"NULL")
We use VLOOKUP to see which column we need to look in. We use a number here for column, not a letter.
We then use ADDRESS to get an excel address reference for the current ROW(), and the column we found via vlookup
We funnel that through INDIRECT so we can get the value from that targeted cell.
Then we get a DATEDIFference in days from offset today.
We wrap it all in an IFERROR to keep things clean.
You could use an INDEX($D2:$S2;0;VLOOKUP($F2;$A$2:$B$9;2;FALSE)) to get the same effect, as pointed out by Dirk Reichel but you have to mindful then that the index used is from the start of the matrix range. So here the matrix starts on row D. So D2 is index 1 instead of 4 it is with my original method, so you'd need to adjust the lookup table accordingly.

Use One Cell to Add Values to a Named Range

We are trying to allow the end-user of this Google spreadsheet to be able to add values to a Named Range without accessing said range. (Sorting it will be a q for a later date)
In other words, let's say that we have a list of Suppliers that we purchase parts from, and that list resides on 'SheetLists' in cells A1 to A119.
To make that list easier for later use, we made it into a Named Range and named it "list1Supplier".
We then use that "Named Range" throughout the Workbook in drop-down lists. Each of the drop-down lists reference the "Named Range" and NOT cells A1 to A119.
We also realized that, should we begin using a new Supplier, the list could grow. So we changed the Named Range reference to cells A1:A, thus including the entire A column.
Now, as expected, one of the end users begins using a new Supplier. The LAST thing we would EVER want is for them to have to figure out how to add said Supplier to the end of that list MANUALLY... right!?!?
I've googled this for hours and have found NOTHING except ONE using VBA. And unless someone has a REALLY easy to follow How-To on VBA basics, I'd like to try this route first...
As always, ANY and ALL help is GREATLY appreciated!
DG
Why you just don't create a live form?
https://www.youtube.com/watch?v=wNMdjVxAEYY
Best

Excel: Increasing/Decreasing number of columns in table based on number of coloured rows from different sheet

I am in the process of creating a costing summary workbook. My main summary sheet lists a job category and then 4 rows of cost breakdown before the next category is listed. On another sheet, I have a template that the user will make a copy of for each month, which breaks down weekly work hours and then allocates hours from each employee/plant to against a job category. Please see attached pictures.
Summary Sheet -
Template Sheet
My goal is to have the number of columns on the template increase/decrease as categories are added to or removed from the summary sheet. I already have a cell that contains a count of coloured rows (category titles), and i figure i should be able to use this to determine the amount of rows that need to be added to or removed from a table. I can't think of any ways for this to be done without macros or VBA code of some kind, which I am open to but I would like to minimise the use of.
Can anyone provide me with some code or a push in the right direction for this kind of function? Category names will be stored in column A, or my count is stored in B84. Columns for each category start from column S in the template sheet.
Kyle, you can visit the following sites with a lot of vba beginners information.
http://www.excel-easy.com/vba.html
http://www.homeandlearn.org/
https://www.tutorialspoint.com/vba/
Try implementing your idea using pseudo-code.
http://www.vikingcodeschool.com/software-engineering-basics/what-is-pseudo-coding
https://www.youtube.com/watch?v=4G0EYfrrDT8
https://www.khanacademy.org/computing/computer-programming/programming/good-practices/p/planning-with-pseudo-code
Once you be able to write your code and you are having some code error or problem you cannot find a solution in the web, come again and ask specific questions, there are a lot of experienced programmers that will be happy to help you to solve your coding problems.

Job/Work scheduler/planner for teams

Currently we use an excel spreadsheet with 4 tabs (as tab for each team), each tab is a planner/diary that week, each day has 4 "slots" for jobs that are planned in.
The people type in the information for each job into one of the 4 slotsfor the day. Each slot contains information in different cells; Job No., Address, Duration, Notes, etc...
The issue/time consuming aspect of this method is when jobs have to be moved about between teams or moved to a different day it involves lots of copying, pasting and deleting.
What I am trying to achieve is some form of drag/drop interface (similar to the way the calendar in Outlook works), where the people who use the spreadsheet can input details for the job in a form and then have a drag an drop "tile" to be able to drag into one of the "slots.
The data contained in these slots needs to be able to produce a report which is a linear list of all the jobs planned that week.
I am at a loss to figure out what method I should use to create this, it seems it is beyond the reach of Excel from what I can find, also beyond the reach of Access, so I suspect it will involve some form of programming. I have basic understanding of VBA, but that's about it.
Can anyone make any suggestions.
Thanks
Working with data in this format will always be hard and I would not reccomend adding any drag and drop until you have sorted out the structure of your data.
You might like to consider another approach, which involves restructuring your data to make it easier to work with...
If you had one sheet with the following columns:
Teamname
Date (instead of Week Number and Day of week,)
Timeslot
JobNo
Address
Duration
Notes
Etc
Then each row would represent a job and have details of when it is occurring.
This is way a database would store the information about the jobs that are happening.
To change when a job is happening, all you have to do is update the date (and timeslot) of the row.
You could then create another worksheet for each team that uses excel formulas to lookup jobs happening this week and display them in a column for each day - like you have now. These sheets would be for display purposes only.
(You would use formulas like VLOOKUP etc)
If you want to change the date of a job you then simply go to the worksheet with the long list and change the date.
Having your data in this long list (ie normalised) format will give you many benefits. For instance you can summerise the report of the data using pivot tables.
If you add filters to the long list (SEE Ribbon>>Data>filters) you can order the data and reduce the list to only show jobs on a selected date, or for a selected team etc...
This is definitely the way forwards. Having restructured the data in this way, you could then add flashy ways to change when a job is taking place by adding buttons on the team worksheets or something.
PART2:
So in you ling list I would recommend excel CONCATENATE function to add an extra column with the text you want displayed elsewhere in your "calendar". #COLUMNA
I would also create another column which is a unique identifier for the row. This will probably be the "Date in YYYMMDD format & timeslot in HHMI format" concatenated together.
In the calendar view sheets say you need a column to display each day of the week with a row for each time slot: You need to
enter a StartDate of the period you want to view
Set up a column for each day (with a formula to calculate the date of the day (ie StartDate, StartDate+1 etc). The date is displayed as a column header (perhaps with a format that shows the dayname, but is stores the date)
each row will have a column that has the timeperiod name and each row will have a value entered. The value is the same as those used in the long list's timeperiod column.
When a user double clicks on a cell you can use the worksheet event
Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
in the VBA code, for this event you need to:
see if the cell double clicked is one you want to respond to
if it is find the date and timeperiod from the corresponding column and row header.
use VBA to activate the long list sheet and probably filter to only show the jobs taking place that day or perhaps that week. Then select (or highlight?) the job that was double clicked.
This is quite easy once you know VBA.
There is a lot to know though I would:
use a named range to define the row with the date and another to define the column with the timeperiod
use a named range to define the cells that will respond to a double click
use range functions in VBA like intersection and the entirerow, entirecolumn range properties to find the date and timeperiod of the double clicked cell.
use the macro record to write the initial code that will do the filter.
adjust the code to be well written and to find the date and timeperiod as required.
Create a sheet for all departments? Or just create one sheet where the dept can be chosen.
You could get clever and allow the user to select two cells in the calendar and provide buttons that do things like:
swap the jobs
If they select many cells then a button to Goto the list and only show jobs that were selected in the calendar.
Perhaps the user could select one job and buttons could move the job back or forwards 1,2,7,14 days.
Note that you can use the long list to record other details of the job like time taken, customer, start time, end time etc...
See about Pivot tables here which might give you some ideas about how you can summarise you data to give info like jobs per day, total value of jobs by day, etc...
Before you tie yourself in knots using Excel, you should always consider using Access.... as with someone who know what they are doing, access will deliver a lot more in a shorter period of time and can do nice things like print one page per job etc etc...
(Do you need a contractor?)
mr#HarveyFrench.co.uk

VBA to transfer a figure from one sheet to another based on matching values other col

I am working on a spreadsheet which is being used to transfer a product from one location to another. Each day I will have a new list of products that needs sending to another location and I already have a "pre-populated" sheet that has suitable locations listed for where these products can be sent to.
I've already worked out the formula's to use which defines the location these products can be sent to (through index and match formula) but once this has been completed, I'd like to update the "pre-populated" sheet with the quantity I'm sending these locations so that limits can be deducted accordingly.
Essentially, I want to copy the figure from column G in ("Task") into column I in ("interstore transfer") where the two "REF" columns in either sheet match. The "New Limit" column will then automatically populate with the new limit based on the figure input into Column I. Once its finished working its way down the list in the sheet ("Task") then end.
I've had a rough attempt at this, but I'm coming stuck with defining the appropriate variables and how it should update.
Any ideas to better my approach would be appreciated.
A VBA solution with variables may not be your best approach for this. Variables declared within VBA code usually have a limited lifetime based on their scope, so when the code ends the values in the variable will be lost.
Another alternative may be to set aside another cell as a counter. Perhaps a good place for this would be next to the "New Limit" column?
Cell values are retained even when VBA code isn't running. Of course cell values also are saved when the workbook saves, so when you get a new list of products at the beginning of the day you can compare to or edit the previous day's work.
To get started with this, I'd recommend getting familiar with how to reference cells and ranges. And, there is some useful information here on Stack Overflow on how to reference well in Excel VBA.