Change column width based on another cell value - excel

In excel, I have two three columns and after transpose I have some change to make. Let the data be ::
A B C
a K .2
a Q .23
a R .45
b L .3
b S .12
c M .43
d N .54
e O .32
f P .067
After transpose::
a K Q R
b L S
c M
d N
e O
f P
Each row is filled by whatever values I have in column B corresponding to column A. Now the problem is the cell in which I fill K L S Q R i.e. column B values should be of different sized and in ratio of their weight which is column C.For example K should be 0.2, R should be 0.45 or in the same ratio. And since these are written one below the top entire column could not be changed for "a" since the next row cells would be again of different sizes each.
I think this could be achievable by macro but I am not sure about that.Please let me know if you need any further information. Thanks!

Not exactly sure what you mean from your wording but it is certainly easy to change column width via vba.
If you want to change column F based on value of C3 then you need:
Range("F:F").ColumnWidth = Range("C3").Value
Obviously qualify your range so it's specific to whatever sheet / workbook you want etc.

Related

How to return row based on all searched instances of a value in a Table excel

I have a very large table with repeating values based off of a ID column. I'm after a function/combination of excel functions that would work to return the ID number where all instances of a searched value are met?
I dont have access to the "Filter" function so im looking for an alternative to this.
E.g, I wish to find all instances of F in the table. From here the function should return the row/associated "ID"
This would then output 2,4,25 where the matches in the table occur
ID
B
C
D
...
BB
BC
SEARCH
F
1
A
B
C
D
E
G
2
D
E
G
F
G
S
3
T
V
A
K
H
E
4
Y
F
J
N
R
K
5
I
O
W
H
X
Z
...
25
T
K
Q
E
H
F
array formula:
=IFERROR(INDEX(A:A,SMALL(IF(B$1:G$500="F",ROW($1:$500)),ROW(A1))),"")
You can try creating an extra column at the end and use this formula in each cell down that column. Make sure your "Search Cell" remains same in the formula though:
Drag this down from first cell down to all rows but make sure the search cell remains same. i.e. B1 in my example. I have added a last extra column H in my example to do this calculaton and the result:
=IF(IFERROR(MATCH(B1,B2:G2,0),"")<>"",A2,"")

Average based on criteria then output

I have an Excel table with the first six columns having a value of 1 or 2. The next six columns are associated with the first six columns and have values that will need to be averaged - two averages will be computed based on whether there is a 1 or 2 value in the first six columns. Then depending on the two averages, the last six columns will need to be assigned a value equal to H (high average) or L (low average). This is difficult to explain, so here is an example:
A B C D E F G H I J K L M N O P Q R
1 2 2 1 2 2 1 8 8 9 8 6 8 L L H L L H
Columns C and F have values equal to 1, so columns I and L need to be averaged. Then because columns A, B, D and E have values equal to 2, columns G, H, J and K need to be averaged. The average of the columns associated with a value of 1 (I and L) is 8.5, and the average of the columns associated with a value of 2 (G, H, J and K) is 7.5. Columns M-R now must be labeled with an H or L depending on whether the corresponding values from columns G-L were part of the high (H) or low (L) average. In this case, since columns I and L had the larger average, then columns O and R need to be assigned an H. The other columns (M, N, P and Q) will be assigned an L because their associated columns (G, H, J, K) had the lower average.
Please consider the following formula placed on the first row of Column M and then copied across to Column R:
=IF(AVERAGEIF($A$1:$F$1,A1,$G$1:$L$1)=MAX(AVERAGEIF($A$1:$F$1,1,$G$1:$L$1),AVERAGEIF($A$1:$F$1,2,$G$1:$L$1)),"H","L")
Logic is if the average of the values that correspond to either the 1 or 2 on A1 is equal to the MAX of calculations between both, then this corresponds to the High number. If not it corresponds to the Low value. Note that this does not consider for when the averages are equal in which case all entries are noted as High. You can extend this by adding to the formula to check if the value is equal to the MIN of the same. Hope this helps. Regards,
If you don't want to use Visual Basic, you could use this method, but it might require more columns.
xx A B C D E F G H I J K L
1 2 2 1 2 2 1 8 8 9 8 6 8
For cell M1 type in: =if(A1=1, G1, "")
Note that this is two regular quotes (")s in a row after the G1 term.
Copy this over to cells M1-R1.
Now cells M1-R1 should only contain data for columns marked with a 1.
Next for cell S1 type in: =average(M1:R1)
This shouldn't factor in blank cells, So you should just have the average of "1" cells.
Now copy the process for the "2" cells:
For cell T1 type in: =if(A1=2, G1, "")
Copy this to cells T1-Y1.
For cell Z1 type in: =average(T1:Y1)
Now for cell AA1 type in: =if(S1 > Z1, 1, 2)
Now AA1 will have the number that has the higher average. So if the "1" cells had a higher average, cell AA1 will be a 1, otherwise it will be a 2.
Now for cell AB1 type in =if(A1=$AA1, "H", "L")
Copy AB1 to cells AB1 through AG1 and you're done.
Cells AB1-AG1 will have your H's and L's. Note that there is one drawback to this method, apart from it being a little complex, that is that if the averages are equal, it will still print "2"s as having the higher average.
Anyways hopefully you can find a simpler method, but this one should work if you can't.

How to sort 3 columns of data in excel

I'm using Microsoft Excel 2011 on a Mac, and I need help sorting 2200 rows of data.
Basically, what I want to do is to sort the Type column, while keeping its corresponding data beside it after the type is shifted.
I would like all the type A's, B's, and C's to be in order, and have their adjacent data from experiment 1 and 2 follow to the new position once sorted.
Each data group is separated by a few empty rows.
Here's a small example of what my data looks like:
Type EXPERIMENT 1 EXPERIMENT 2
A A F
F R
S S
E G
B Y A
A A
S H
F
A A E
D F
F S
E
Y
A F H
R G
E A
D F
Y W
S E
S
C F S
D D
S U
F J
Y
T
And this is what I want:
Type EXPERIMENT 1 EXPERIMENT 2
A A F
F R
S S
E G
A A E
D F
F S
E
Y
A F H
R G
E A
D F
Y W
S E
S
B Y A
A A
S H
F
C F S
D D
S U
F J
Y
T
In an unused column to the right (aka Helper column) put this formula into the second row.
=VLOOKUP("zzz",A$1:A2,1)&TEXT(COUNTIF(A$1:A2,VLOOKUP("zzz",A$1:A2,1)),"0000")
... and fill down as necessary. Select all of the columns and sort with the Helper column as the primary sort key. The results from your sample should mirror the following.
                 
Additional formulas can be provided if you need sorting key criteria on the Experiment x columns. Show what you've tried together with some example of the results expected and additional assistance will no doubt be offered.
It seems to me it would be easy to sort if you would concatenate each data set into a row. Type goes in column A; Exp 1 into one cell in column B; Exp 2 into one cell in column C. Then the sort is simple. Otherwise you'll require a complicated macro. You can always parse it out later if required.
To concatenate you'll need to:
Save as new workbook first to avoid changing your original data
Insert a row between the headers and the first data
Put the formulas indicated into row 2 and copy down
Copy and PasteSpecial as values
Sort by column D
Delete all rows that are false
Now your sorting will be easy.
Edit: fixed my formulas to line up with true column.

Return value of last populated cell in variable range

Into column D, I'd like to copy the value from the last cell with data in columns E up until the column with the header "DETAIL". There might be anywhere from 2 to 15 columns from E until the column with that header, so that's where I'm stuck. So to be clear, an example:
A B C D E F G H DETAIL
1 x x x a b c d x
2 x x x x
3 x x x c b a x
3 x x x d c x
Should fill column D like so:
A B C D E F G H DETAIL
1 x x x d a b c d x
2 x x x x
3 x x x a c b a x
3 x x x c d c x
I don't mind handling this with a formula (which I couldn't come up with) or programmatically.
Create a dynamic named range by pressing CtrlF3 to bring up the Name Manager, click New, name the range something (I chose MyRange) and then use this formula to define it (Note you may need to change the Sheet name):
=Sheet1!E2:INDEX(Sheet1!2:2,MATCH("Detail",Sheet1!$1:$1,0)-1)
Then, in cell D2 and copied down, use this formula (I did not use IFERROR so that it would be backwards compatible):
=IF(COUNTA(MyRange),INDEX(MyRange,MATCH(REPT("z",255),MyRange)),"")
Here are the results (highlighted) using your provided sample data:
Please try:
=IFERROR(INDEX(F2:T2,,MATCH("zzzzzz",F2:T2)),"")
in D2 and copied down to suit.
Try using LOOKUP:
=IFERROR(LOOKUP(9^99,SEARCH("*",E1:H1),E1:H1),"")
SEARCH("*",E1:H1) returns a number when it matches any character and an error when the cell is blank. LOOKUP then returns the contents of the cells of the last number smaller than 9^99 in the array generated by SEARCH.
For example, in the first row, SEARCH("*",E1:H1) returns {1,1,1,1} so that LOOKUP returns the last 1, being d.
In the third row, SEARCH("*",E3:H3) returns {1, 1, 1, #VALUE!} and LOOKUP returns the last 1, which is a here.
This formula will work with numbers and text alike. The downside is that it is considered slower than INDEX/MATCH. On the other hand, you can modify the INDEX/MATCH to work with numbers, or modify it to work for both but becomes an array formula:
=IFERROR(INDEX(E1:H1,,MATCH(1,SEARCH("*",E1:H1))),"")
[Works with Ctrl+Shift+Enter, otherwise returns an empty cell with Enter alone]

How to delete entire column if it is Empty

I have columns
Row 1 is heading..
IF Column C doesn't have data in entire column then delete C D E F
IF Column D doesn't have data in entire column then delete D E F
IF Column E doesn't have data in entire column then delete E F
IF Column F doesn't have data in entire column then delete F
Same way for heading NN's
IF Column G doesn't have data in entire column then delete G H I J K L M N
IF Column H doesn't have data in entire column then delete H I J K L M N
IF Column I doesn't have data in entire column then delete I J K L M N
IF Column J doesn't have data in entire column then delete J K L M N
IF Column K doesn't have data in entire column then delete K L M N
IF Column L doesn't have data in entire column then delete L M N
IF Column M doesn't have data in entire column then delete M N
IF Column N doesn't have data in entire column then delete N
A B C D E F G H I J K L M N O P Q R
TI1 TE2 TT1 TT2 TT3 TT4 NN1 NN2 NN3 NN4 NN5 NN6 NN7 NN8 CMT K2 K3
BLAH BLAH
I would do this by adding in an extra row to work things out (you can always hide it later).
Suppose your headings are in row1, then add in an extra (helper) row in row2.
In C2, put in the following formula (replace C30 with an appropriate value for you):
=COUNTA(C3:C30)
In D2, put in the following formula:
=IF(C2=0,0,COUNTA(D3:D30))
Drag that formula across to E & F
Redo C's formula in G
Redo D's formula in H and drag across to N
... Etc.
Now it's just a matter of writing a very simple macro to delete any column where row2's value = 0.
That seemed like the easiest way to do it for me, but there are MANY other ways.
Hope this helps!!
This macro does your first bit - the second stage will be very similar.
You'll need to chage the 5s to the length of your table.
GoTo isn't generally liked but I've used it in this code.
Sub firstbit()
If Excel.WorksheetFunction.CountA(ActiveSheet.Range("C2:C5")) = 0 Then
Excel.ActiveSheet.Range("C2:F5").clearcontents
GoTo FirstStageComplete:
End If
If Excel.WorksheetFunction.CountA(ActiveSheet.Range("D2:D5")) = 0 Then
Excel.ActiveSheet.Range("D2:F5").clearcontents
GoTo FirstStageComplete:
End If
If Excel.WorksheetFunction.CountA(ActiveSheet.Range("E2:E5")) = 0 Then
Excel.ActiveSheet.Range("E2:F5").clearcontents
GoTo FirstStageComplete:
End If
If Excel.WorksheetFunction.CountA(ActiveSheet.Range("F2:F5")) = 0 Then
Excel.ActiveSheet.Range("F2:F5").clearcontents
End If
FirstStageComplete:
End Sub

Resources