(VBA) Extract and copy and paste column values for specified date range for corresponding row values - vba

I've been stuck on this for a while now. What I need is the title - what I have is:
Sheet 1:
1 billion mostly unnecessary columns.
Column D: Values I need Column F: Column M:
Revenue Names Date
12 John 1/24/2016 2:40:02 AM
15 Sarah 3/2/2016 4:35:17 PM
14 Sarah 7/17/2016 1:50:10 PM
20 Matt 8/20/2012 4:16:12 AM
10 John 11/19/2015 5:04:05 AM
etc. etc.
Current Sheet 2: Pivot Table*
Row Label:
Sarah
Matt
John
etc.
Desired Sheet 2 Pivot Table+*
Row Label: Column __:
Revenue
Sarah 29
Matt 0 *(note: see below, but = 0 because Matts value corresponded to date 2012)
John 22
etc.
The important thing about Sheet 2 is that I would like to tell VBA to find the nearest emptiest column within the sheet, and input the values from Column D from S1 (and sum for duplicates) but only sum and extract values that range from today's date to 11 months prior. Currently I made a module to automatically create a pivot table for the next sheet, but I have wanted to add the above for a while, just stuck.
I'm also assuming I won't have to specify sum if I tell VBA to extract data for corresponding row label names? And I was thinking about putting a Begin Date and End Date cells on Sheet 2 to refer to, or using the TODAY() function somehow for the date, but not sure how to specify to extract and sum values for the date range from today to 11 months prior.
Edit: *Please note this is a watered down version of my current situation, everything is bigger so to avoid confusion, pivots are necessary, but I choose to dilute it for the sake of this issue.

Do you really need a pivot table?
If you dont, just create a new sheet with all the names, set a cell for the starting date you want to consider and do =sumifs
For example
On Sheet2 B1 you put 1/24/2016.
Then on A2 you insert the name (Sarah, for example)
On B2 you put the formula
=Sumifs(Sheet1!D:D,Sheet1!F:F,A2,Sheet1!M:M,">"&B$1)
After that just make a list of name and drag the formula beside every name.
*My excel is in portuguese so maybe the formula needs some other ajustments to work.

Related

How to compare a list of rows to another list of rows in Excel?

I am trying to figure out if there are any differences between a list of data with another. In order for a row of data to "match" with another row, the row must have the same values in their corresponding column. The rows themselves do not have to be in any particular order. In particular, I am dealing with a parts list, where there are part numbers, descriptions, etc. I am trying to figure out if any rows of data are different from rows of data from another list.
I found Compare two sheets using arrays, which may have the answer to my problem, but I am having trouble figuring out how to adapt to my code due to inexperience in Visual Basic.
I was able to get it to work for a single column of data, comparing one column of data from one sheet to another, but cannot get it to compare entire rows of data.
Here is an example of I want this to work:
Sheet 1 Sheet 2
Column 1 Column 2 Column 1 Column 2
Row 1 22a 33 11 11
Row 2 22a 33a 22a 33
Row 3 55 22b 55 23b
The code in the link will tell you what is not in sheet 1 but in sheet 2 and vice versa. In this example, I would like the code to tell me Sheet 1 Row 2 and Sheet 1 Row 3 are not in Sheet 2, and Sheet 2 Row 1 and Sheet 2 Row 3 are not in Sheet 1 (Sheet 1 Row 1 and Sheet 2 Row 2 match).
If that is ok by you, you can do it without VBA using the following formula:
={IF(IFERROR(MATCH(A1&"|"&B1;Sheet7!$A$1:$A$3&"|"&Sheet7!$B$1:$B$3;0);-1)=-1;"Unique";"")}
Assuming that each of your tables start in A1 (so that the tables with three entries span A1:B3), and entering this formula into C1 (and copying it down), press CTRL+SHIFT+ENTER when entering the formula to create an array formula, this will show the word "Unique" in column C if the pair in that row on that sheet is not in any of the row-pairs on sheet 2.
You can then use conditional formatting to highlight unique rows, filter on the tables to include only unique rows, or some other way of doing what you need.
NOTE 1: I have entered my numbers in Sheet6 and Sheet7 instead of 1 and 2. The formula written above goes into Sheet6.
NOTE 2: My language use ; instead of , as function separator, so if yours use , you need to change that.
NOTE 3: You will need to expand the ranges Sheet7!$A$1:$A$3 and Sheet7!$B$1:$B$3 if your set grows (this will happen automatically if new rows are inserted in between the old ones). The best is still probably to create named ranges for each of the 4 columns, exchange the references with those, and manage the named ranges instead of the formulas.
NOTE 4: If your data set contains the character "|", you need to change that as well, to match some character that you for sure do not have there.
Alternatively you could in column C on each cheet enter (assuming first entry in C1)
=A1&"|"&B1"
and copy this down, then run the solution from your copied example using that C column instead of on A1 and B1.

Excel Lookup Data Based on Column Name not Cell Name

I am trying to accomplish a strange task in excel and don't know how to go about it. I'm using Excel 2007 at work and I know very basic vba.
I want to automate a process where a person takes three spreadsheets and dumps certain data from them into one master sheet. The three spreadsheets vary every month in their column order, so unfortunately I can't just program vlookups to run and get the data.
I could be wrong, but it seems like Vlookup Match, Index Match Match, etc. wouldn't work either because they are still referencing cells. I basically need something that will find a column based on the text in the column, rather than its location, because the location will change, but the text will always be the same.
I have two ideas but I have no idea if they work and don't really know where to start on implementing them:
Convert the three spreadsheets to tables and reference the headings with table syntax (I haven't been able to get this to work)
Complex VBA that IDs everything
Can anyone help point me in the right direction to accomplish this task? Thanks so much for your help.
EDIT EXAMPLE
My main template that I'm trying to dump things into is just going to have the ID#s and empty columns:
ID# AltID# Deal
1
2
3
4
5
And then another spread sheet might have look like this
ID# AltID# Deal
1 10101 AAAA
2 20202 BBBB
3 30303 CCCC
4 40404 DDDD
5 50505 EEEE
I could of course vlookup, but the problem is next month, those columns in the second spreadsheet won't be in the same place, in the same order. They could be all the way on colum DD or whatever. So I need a formula that looks them up regardless of the columns location. Just matching them by the heading text. I hope that clarifies...
If you combine VLOOKUP with MATCH you can search based on column name, not index. Remember that the MATCH function returns the numeric index of the match. Let's assume that your data looks like the below:
NAME DAY1 DAY2 DAY3
Bob 123 345 567
Tim 456 789 998
A functioning Vlookup to return the DAY2 column for Tim would look like:
=VLOOKUP("Tim", A2:D3,MATCH("DAY2",A1:D1,0),FALSE)
A couple of notes. Where I've hard coded in words, you can use cell references.
Here is one idea:
get the index of the column ID# in Sheet2 MATCH("ID#",Sheet2!$1:$1,0)
convert to a character: CHAR(MATCH("ID#",Sheet2!$1:$1,0)+64)
get the column range:
INDIRECT(
CONCATENATE("Sheet2!",
CHAR(MATCH("ID#",Sheet2!$1:$1,0)+64),":",
CHAR(MATCH("ID#",Sheet2!$1:$1,0)+64)))
similarly for column AltID#:
INDIRECT(
CONCATENATE("Sheet2!",
CHAR(MATCH("AltID#",Sheet2!$1:$1,0)+64),":",
CHAR(MATCH("AltID#",Sheet2!$1:$1,0)+64)))
with range of ID# and AltID#, we can do the match+index:
=INDEX(
INDIRECT(
CONCATENATE("Sheet2!",
CHAR(MATCH("AltID#",Sheet2!$1:$1,0)+64),":",
CHAR(MATCH("AltID#",Sheet2!$1:$1,0)+64))),
MATCH(A2,INDIRECT(
CONCATENATE("Sheet2!",
CHAR(MATCH("ID#",Sheet2!$1:$1,0)+64),":",
CHAR(MATCH("ID#",Sheet2!$1:$1,0)+64))),0))
Here is an idea to consider:
Give a name to cell ID# e.g "dataJanuary" and use this named-cell as reference using an OFFSET function then you can explore it with vlookup or index-match function or other method.
Next month even if the ID# column is moved to other column or row, it will always reference to the same data since the cell is already named with "dataJanuary".

Pivot Table Number Ranges - Novice

I am a complete novice to excel and having my first go with Pivot Tables.
I have sheets with lost of data mostly numeric, these sheets contain up to 80,000 rows and 25 columns.
I am using pivot tables to filter the data and have managed to get it going ok to give me the values I require.
However I would like to now add the ability to break down the filter ranges into numbers with between values.
For example I have one particular column with numbers ranging from -600 to +2450.
I would like to look at values between 35 and 180 for example without having to select multiple values in the filter and then tick every box i require.
Is this possible for a novice (I have no idea what or where VBA is having had a look around
IF it is possible would it then be possible to do the same for columns that contained a certain piece of text
Hope someone can help
Ian
this is how i would do it.
let's say your "one particular column with numbers ranging from -600 to +2450" is column A.
You can set a new column with the formula IF(OR(A*>=35,A*<=180),1,0),
drag the formula to fill the rest of the cells in the new column,
filter the new column for 1 and it should display all the rows where the values are more than 35 and less than 180.
if it doesn't work with the pivot table, try select all the data in the pivot table, copy and paste the data as values to a new sheet.
you can try it for text as well. just if you are searching for "APPLES" in a column A,
use IF(A*="APPLES",1,0) in a new column and filter out 1.
hope this helps.
For ad-hoc analysis, you could right-click on the relevant field header and select Value Filters and Between. This will save ticking/unticking every number in the range.
You can also group your data field. If you e.g. have the following pivot table layout:
Header1 Header2 Data
A AB 1234
AC 2345
AD 3456
B BB 4567
you can select e.g. AB-AC and right-click and Group... the items. This will add another column with the grouped Header2 fields:
Header1 Header2_2 Header2 Data
A Group1 AB 1234
AC 2345
AD AD 3456
B BB BB 4567

Conditional formatting formula to highlight appropriate date in range

I need a formula for conditional formatting that will highlight a date between A2:Z2 which matches a number that I enter into a “Committed Sessions Cell” (A1). In row 2 there are a series of numbers that appear above each date column (1,2.3, etc). For example, if I enter a “3” in cell A1, the date in J3 should match the number 3 above it and be highlighted. The idea here is to provide a quick visual prompt for how many sessions are in a client’s contract.
Note: the sequenced numbers 1,2,3 etc in row 2 appear every 5th column (with nothing in between) but there IS other data in between the dates in row 3. Only the appropriate date should be highlighted.
A B C D E F G H I J K L M
1 3
2 1 2 3
3. 1/2/14 2/3/14 2/15/14
With grateful thanks,
~ Jay
Your example is not consistent. If you have the date every fifth column, the dates should be in columns A,F,K,P, etc. with 4 columns between 2 points.
I came up with the following formula: =AND(A2=$A$1,MOD(COLUMN(A3)-1,5)=0) which is applicable to the entire 3rd row. Create it as follow:
IMPORTANT: Select cell A3 (the reference point for the formula)
Without selecting another cell, highlight the entire row 3
Go to Conditional Formatting -> New rule
Choose "Use a formula to determine which cells to format"
In "Format values where this formula is true", put =AND(A2=$A$1,MOD(COLUMN(A3)-1,5)=0)
Choose the formatting that you want (example: Fill with yellow)
Click OK all the way
NOTES:
To change the location of your Committed Sessions, change $A$1 to another cell. Important to keep the dollar signs
The MOD function is the one that controls every fifth column. If you want the highlight every 4th column (i.e. 3 cells between each point such as A, E, I, etc.), replace the number 5 with the number 4

Summarize data in Excel with VBA

I have a table in Excel with dates and values. Every day there were a number of different values.
I want to summarize how many of each value there were every day.
Example:
From this
Date Value
10/1 Blue
10/1 Blue
10/1 Red
11/1 Blue
11/1 Blue
I want to get a new table with something like this:
10/1 11/1
Blue 2 2
Red 1 0
I'm convinced this is possible to do in VBA/Excel. Does anyone have any ideas?
Assuming the first table is in A1 and the second in E1, you could use a formula in F2 like:
=SUM(($A$2:$A$6=F$1)*($B$2:$B$6=$E2))
This is an array formula so you need to validate it by pressing CTRL+SHIFT+ENTER.
A2:A6 is the range with the dates, B2:B6 the range with the colors in your first table.
The first part of the formula says: Retain records where the value in column A is 10/1 ( = F1).
The second part says : Retain records where the value in column B is Blue ( = E2).
The '*' is equivalent to AND.
More about it here: Multiple conditions in excel 2002
The solution was, just as Alex K said, to use the Privot Table function included in Excel.
Thanks for your help!