Excel copy format between cells using formulas - excel

Hy everybody,
I have a question about the cell formatting in excel. I have a table like that, where I have a series of true/false values:
Next I have another page in which I use a formula to determine if a 1 on a row is present and copy it to a single vertical table, like that:
That is pretty simple, by using a nested if I can easily achieve this result. Here is an example:
=IF(PAG1!A1=1; PAG1!A1; IF(PAG1!B1=1; PAG1!B1; IF(...)))
So I can stop on the first one. Now the problem is that I have a series of colors that must be copied too. As you can see from the picture I need to copy also the green and the red from the table, because at last I need a result similar to this one:
Is there a way to obtain this result programmatically? Green and red ones can be everywhere each time the table changes so I need a way to copy the format dinamically. Actually my idea is to try to get somehow the cell reference from the if statement (any clue on that point?) and once having the origin cell try to copy the format for the new cell in the new page. Is there a better way to do that?
At least it will be good also to have another table where I report the cell reference, like: D1, A2 and B3, so I can write a VBA script to color the cell based on their origin. Is there a function taht can be used to achieve that?

Table where each row might have a single 1 on one of the columns starting at A2 for example, with 4 columns. Find the column where 1 might exist use the =MATCH() function
A
B
C
D
E
F
1
2
0
0
1
0
=MATCH(1.0,A2:D2,0)
3
1
0
0
0
=MATCH(1.0,A3:D3,0)
4
0
0
0
0
=MATCH(1.0,A4:D4,0)
This will return the index in the range where a match is made, or N/A
F
3
1
N/A
In this case the range includes columns A through D so 3=C and 1=A.
You can take it from here with VBA to find the cell in the table from each row and column information.

Related

Copy and paste from horizontal to vertial

I am wondering if there is a way to do what I want more automatically, i've been doing it with good old regular copy paste but it's taking a lot of time, I need to take the horizontal data I have currently and put it vertically while keeping the first column for each rows, the first column is my "main" part number and I need to link all the other numbers starting from column B to this main part number, example below,
I sometimes have hundreds of rows and columns to do this for, here is what i'm working with
[1]: https://i.stack.imgur.com/dIyZv.png
And here's what the end result needs to look like;
[2]: https://i.stack.imgur.com/PvxGh.png
Thank you in advance!
It can be achieved either with VBA Code or with formulas. I did it with formulas and will try to show you how below.
I have one sheet with all the data, lets call it Sheet1. It looks like this:
Then, I have another sheet which returns what you want (Sheet2). It looks as follows:
Note that the first two columns are necesary for the formulas to work. The actual result is on Columns C and D.
Now, below are the formulas (or values) you would need to put in Sheet2 to make it work:
Cell A1: 0
Cell B1: 1
Cell A2: IF(B2=1,A1+1,A1)
Cell B2: IF(B1+1=$G$1,1,B1+1)
Cell C1: OFFSET(Sheet1!$A$1,A1,0)
Cell D1: OFFSET(Sheet1!$A$1,A1,B1)
Cell G1: The number of columns in the data on Sheet1
Then, just drag the formulas (not the values in A1, B1), until you have the expected result.
I would suggest you try to implement this simple example first, and then move it to your actual spreadsheet.
Let me know how it goes.
EDIT: Regarding your comment, we could do a trick to make it work for a variable number of columns.
First, lets add some variability to the number of columns in Sheet1. This is how my new Sheet1 looks like:
On Sheet2 I added a formula to count the number of columns per row. See Column F below:
Then, the trick is to change the formulas in Column B in Sheet2:
Before: Cell B2: IF(B1+1=$G$1,1,B1+1)
After: Cell B2: IF(B1+1=OFFSET($F$4,A1,0),1,B1+1)
Note: My formula to count the number of columns per row is:
Cell F4: COUNTA(Sheet1!A1:D1)
Note: Change D1 to the max column in Sheet1. Eg. M1.
Note2: You can get rid of cell G1 now.
Start with column B and everything else becomes a doddle. I only use 1 formula for column B and no more than 1 or 2 for column A (there are 3-4+ methods, no need to duplicate yourself but choose any of them to construct A).
I'm only using 1 formula to make each column. Entire job task done in a minute-5.
Column B. Do this first for all your "column B's":'
=IF(ROW(A1)=1,INDEX(A$1:T$1,INT(((ROW()-20)/20+1-ROW($A$1))/COLUMNS(A$1:T$1))+1,MOD(ROW()-20-ROW($A$1),COLUMNS(A$1:T$1))+1),INDEX(A$1:T$1,INT(((ROW()-20)/20+1-ROW($A$1))/COLUMNS(A$1:T$1))+1,MOD(ROW()-20-ROW($A$1),COLUMNS(A$1:T$1))+1))
So all you have to do is drag down. Change only the row references to refer to your 100's if different rows you need to do this for. 1 minute. Job done
for 30 columns/cells change T to AD, for 200 cells , change T to GR, etc. You can also force it to stop at number of your choice.
If you put it anywhere else, it will still work, but your going to have to jiggy with the math a bit to get it to start from the the first entry in the row ( or the specific Wherever you want). Its up to you.
It acts like a modular clock. Been using it for a few years. Ticker tape. Rolling slabs of concrete laying out for you.
here, this is what I get when I plug it into the first row of any column , referring to a 20 field length header on my sheet.
Etc... It continues forever (or for however long you want it). (and you can change the mod anytime).
The first argument you could change to whatever your requirements are , for instance , if(LEN(F8)<1, or whatever, to Start/"Set the clock."
And For any corresponding ranges (your first column for each) (** Your "Column A's" **):
=IF(LEN(H20)<1,K21,H20)
or you can use this logic. It Becomes elementary.
=IF(J21=K21,J21,J20)
Better if you use this starting from row 2 on each column A: if(and(j1=it's next door neighbour, row(it's next door neighbour cell=1)), it's self j1,.. blah blah blah blah
OR AM I MISSING THE POINT ?
Method for getting column A;
Since you already have B, A becomes a simple trivial matter. Like cell =$fixed$cell , i.e. A1=$B$1 and drag down.
simple. headache over. game over. Its all effortless.
But if you want me to elaborate more ,
method 2 for getting 1st column, column A's;
=IF(LEN(AV1)>1,IF(ROW(AV1)=1,AV1,INDIRECT("Av"&1)),"")
=IF(ROW(B1)=1,B1,INDIRECT("B"&1)) <---- drag down from row 1
Method 1 for getting 1st column;
=IF(LEN(AV1)>1,$AV$1,"") <----- drag down from row 1
Method 3, below was my favourite:
=$B$1 <---- from row1 drag down (where row1 was just = column B, cell b1, a1=b1) easy peasy .
You could always demand a further simplification of M3:
A1=if(len(b1)>1,$b$1,"") <--drag down from b2 (where b1 was already set) *probably best because only returns a value as long as column B is .otherwise returns blank.
It's like handling duplicates by formula. Similar .
there was a method 4 too. bUt its late. (Written # 02:00)
Or did I miss the point? It's easy. Imo
Im only using 2 formulas. 1 for each column you need done . do column B first, and colimn A becomes a matter of fact . A doddle.
method 1 is my new favourite.
Its late, my naighbours have pissed me off again. Pardon my fonts and writing but seriously. didnt expect this. some people need to go to jail.(my naighbours)
So in the end: 1 formula for B. 1 for A (any one of any the 4+ methods. There are more also) , plug in and scroll for all the rows you need this done for . takes you 30 seconds? 5 minutes tops for all your rows.

Convoluted formula using INDIRECT, ADDRESS and MATCH - is there a better way?

I need to get a MAX of a range that is bound by two 1s in a helper column.
The 1s are a variable number of rows apart.
By way of example, here is a sample of what I'm talking about:
1
0 -1.10%
0 0.00%
1
0 1.43%
0 1.15%
0 2.12%
0 2.69%
0 1.32%
0 0.86%
0 -0.69%
1
~
[and so on]
So, for instance, there are two ranges visible that I'm interested in here -- the range between rows 1 and 4 and the range between rows 4 and 12.
In a third column, wherever there is a 1 in the helper column, I want the MAX of the ranges.
I've managed to cobble together this formula that does the job (this is copied directly from the spreadsheet so it's in row 122 and the data currently goes to row 16120, the helper column is column E and the column with the values is F):
=IF(E122=1,MAX(F122:INDIRECT(ADDRESS(ROW()+MATCH(1,E123:$E$16120,0),COLUMN(F122),4))),"")
My basic thinking is to build the bottom of the range by looking for the next 1 down the helper column (using MATCH), add that to the current row (using ROW and COLUMN) wrapped inside an ADDRESS function and then tie it all together using INDIRECT. Finally, it sits inside an IF to only hit the rows with a 1 in the helper column.
Can anyone think of a more elegant, less cumbersome way?
Thanks in advance.
If you have a larger dataset, I'd recommend to use INDEX instead of OFFSET! The latter is volatile, i.e. Excel will recalculate all OFFSET formulas and any dependants every time it does any recalculation. INDEX on the other hand is non-volatile, i.e. only if any of it's predecessors change will Excel recalc the formula.
Therefore, give this formula a try:
=IF(E122=1,MAX(F123:INDEX(F123:$F$16120,MATCH(1,E123:$E$16120,0))),"")
You could use OFFSET:
=IF(E122=1,MAX(OFFSET(E122,1,1,MATCH(1,E123:$E$16120,0)))-1,"")

excel 2007 X Y lookup

Good morning. I have been searching for some code either a formula or VBA to lookup values in a table. I have two tables that I am looking to migrate data from the old with the new. Both tables are relatively the same.
A B C D E
1 Store 1234 2345 3456 4567
2 1234 0 5 10 15
3 2345 5 0 20 25
4 3456 10 20 0 35
5 4567 15 25 35 0
It is basically a mileage table with location to location distance. I am looking for a way to take the values from the old table into the new when the row columns dont match up exactly due to new locations added.
I know how to do a vlookup, but that dosent do what Im looking for... I want a way for a formula or vba to something like "find Value in B2 "1234" where it intersects the value in D1 "2345" = D2 "5"
Should be able to do a Index and Match combo to find it. I1 = From, K1 = To you can just type in the locations into those cells and get the range out you want.
=INDEX(A1:E5, MATCH(I1,A1:E1,0), MATCH(K1,A1:A5,0))
Can you implement IF formula to vlookup? In abstract: =IF((vlookup)=something),do this, else)
I would be hapy to look into this issue further, if you could provide complete BEFORE data and expected AFTER data.
Also, maybe =DGET would work for you?
I used this formula slightly modified and it worked fine,
=INDEX($A$1:$G$7, MATCH(B12,$A$1:$A$7,0), MATCH(C12,$A$1:$G$1,0))
the first part of the equation is the entire table including the X and Y coords. the next two parts are the X and Y coords (you can swap these round if you wish)
so the requested information for x and y was put in cells C12 and B12 and all those cells below...
then I used the dollar symbol to fix all the other look up cells, so i could use auto fill, and entered this in E12 (this is where the result will appear.
You can see the table in the attached (or could if I was allowed to attach it). I am looking up B12 (3) in column A then looking up C12 (6) in row 1 and returning the value at the intersection (in this case returns "r").
or there would be an attachment if the stupid web page did not insist on having me obtain a 10 reputation... hopefully you get the idea...

Different approach to my situation

on a excel sheet I've got columns, each column represents a weeknumber.
I want to calculate the so-called 4 wk average for each row and for each week and this is the formula I use:
((value*Tvalue)+(value*Tvalue)+(value*Tvalue)+(value*Tvalue))/(Tvalue) (this is not the actual formula but simplified, that's not really important).
It's the checks that make things a bit more complex. If a value of a weeknr is zero, skip it, but if the next value is also zero, just skip the formula alltogether and make it a zero (or text like "false"). So another thing that has to be accounted for is that if a value is zero, the next weeks value is taken instead.
Example (see included file):
I want to calculate the formula (mov 4wk avg) for the third value for week 12, which will make the formula (0.2*6)+(0.3*6) now there's a zero on week 14 so I skip it, then formula will be:
(0.2*6)+(0.3*6)+(0.6*6)+(0.9*6)/(6). Hope that made some sense.
Right now I'm doing this in VBA with a lot of variables and a lot of if statements.
Is there an easier more effective way to go about this?
Example sheet
https://dl.dropbox.com/u/3121767/Book1.xlsx
PS I know the example sheet is a 2007/2010 version but I need to accomplish this for 2003
Array formula could be used (Ctrl-Shift-Enter in formula window, curled brackets will be inserted by Excel itself, not by a user):
A3: T value for row 3
B3:E3: values for single weeks in row 3
={IF(OR(SUM(IF((B3:C3)=0;1;0))>1;SUM(IF((C3:D3)=0;1;0))>1;SUM(IF((D3:E3)=0;1;0))>1);"Fail";(B3*$A3+C3*$A3+D3*$A3+E3*$A3)/$A3)}
The formula takes pairs of weeks, if the sum of the pair is 0, the whole formula will fail. If there's only one zero, everything will remain the same, as 0*value is equal to skip.
The formula can be copied to the right and downwards.
The updated exampe is available at http://www.bumpclub.ee/~jyri_r/Excel/4_week_average.xls
I tried a 2-step approach
1) transform each cell according to following rules:
if myweek = 0 and mynextweek = 0 then
myweek = 0
elseif myweek = 0 and mynextweek <> 0 then
myweek = mynextweek
else
myweek = myweek
endif
in Sheet2!B2 I entered =IF(AND(Sheet1!B2=0,Sheet1!C2=0),0,IF(Sheet1!B2=0,Sheet1!C2,Sheet1!B2)) and copied to all cells where in Sheet1 dat aexists, together with a copy of the base value in column A
2) Then I create a standard moving average across 4 weeks, for each set of 4 columns, starting with W15 in column M ... matter of taste if you display this in Sheet1 or Sheet2

Look up a value in a table based on two input values

I am very familiar with vlookup and hlookup functions in Excel. However, I am looking for a method of doing both. Take this example:
A B C
1 Resources
2 Task Mgr Sr. Mgr
3 -----------------------------
4 Task 1 30% 70%
5 Task 2 40% 60%
6 Task 3 50% 50%
7 Task 4 70% 30%
If I wanted to put a formula in a new cell to look up both a task and a resource type to return the appropriate percentage, how could I do this?
A combination of INDEX and MATCH will do the trick:
=INDEX($B$4:$C$7,MATCH("Task 3",$A$4:$A$7,0),MATCH("Mgr",$B$2:$C$2,0))
Another possibility:
=VLOOKUP(E3,A2:C7,MATCH(E2,A2:C2,0),FALSE)
Where
E3 contains the task to look up
E2 contains the header column name
(eg Mgr)
A2:A7 is the table of data
A2:C2 is the header
Okay, assume you have an Excel sheet with the following format where your lookup table occupies the cell range A1:E5
C1 C2 C3 C4
R1 R1C1 R1C2 R1C3 R1C4
R2 R2C1 R2C2 R2C3 R2C4
R3 R3C1 R3C2 R3C3 R3C4
R4 R4C1 R4C2 R4C3 R4C4
Also assume you want to enter the row header name and column header name into cells G3 and H3 respectively (which I have the text values "R3" and "C2").
In the cell you wish to display your output value, you could either use HLOOKUP like so:
=HLOOKUP(H3,A1:E5,MATCH(G3,A1:A5,0))
or VLOOKUP like so:
=VLOOKUP(G3,A1:E5,MATCH(H3,A1:E1,0))
Either displays the value "R3C2" in my output cell.
=OFFSET(A3,MATCH("Task 3", A4:A7, 0),MATCH("Mgr",B2:C2,0))
Of course, you're probably getting the things to look for from other cells, so replace "Task 3" and "Mgr" above with references to those cells.
Okokokok so
I just figured out an alternate, much simpler answer... its an IF function!
so okay, what I mean by this is the following;
you have 2 input cells, both formatted with data validation lists. One has the tasks, and one has the position, as shown in the question asked.
now we use a vlookup function to determine what row we are going to get, and then an IF function to determine the column!!
now lets say your input cells are next to each other at E1 and F1
So an example of this formula would be,
=vlookup($E$1,$A$4:$C$7,IF($F$1="MGR",2,3),FALSE)
This works so well and can even be used with more than 2 columns by using the IFS Function!
I Hope this helps some kid in the future who did exactly what I did and went to the internet for answers after being very confused hahaha

Resources