How to convert a table to multiple columns - excel

I have tons of spreadsheets that are formatted like this first table (but with a lot more records and different number of records). I need to make it take all the right information but format it like the second table for importing into Access. Can this be done? Thanks.
..........part 1 part 2 part 3 part 4
Test 1..5
Test 2.............x.........5
Test 3..2.........x..................x
Test 4.......................x.........x
Test 5..x..............................2
Test 1 Part 1 5
Test 2 part 2 x
Test 2 part 3 5
Test 3 part 1 2
Test 3 part 2 x
Test 3 part 4 x
Test 4 part 3 x
Test 4 part 4 x
Test 5 part 1 x
Test 5 part 4 2

Here's one way to do it. You could also use For Each loops for the Ranges.
Dim workingRange1 As Range, workingRange2 As Range
Set workingRange1 = Sheets("Sheet1").UsedRange
Set workingRange2 = Range("PutStuffHere")
For i = 0 to workingRange1.Rows.Count - 2
For j = 0 to workingRange1.Columns.Count - 2
If Not IsEmpty(workingRange1.Cells(i+1, j+1))
workingRange2.Offset(0, 0) = workingRange1.Cells(i+1, 1)
workingRange2.Offset(0, 1) = workingRange1.Cells(1, j+1)
workingRange2.Offset(0, 2) = workingRange1.Cells(i+1, j+1)
End If
Set workingRange2 = workingRange2.Offset(1,0)
Next j
Next i

Related

In Excel, how can I create a counter to increment based on a condition?

Lets say in Excel, that I have this in a column. When the number in column N is greater than 3, then the number in the Counter column increases. I have tried if statements, but the count never works.
N Counter
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3
Try this simple COUNTIFS() function.
=COUNTIFS($A$2:$A2,A2)

Excel - mark duplicate value and if required or not

I have a data like this:
ID ID-Name CountUnique Required Available **Results**
1-Line 1 Line 1 1 1 Y Y
2-Line 1 Line 1 0 0 N Y-Duplicate
3-Line 1 Line 1 0 1 N Y-Duplicate
1-Line 2 Line 2 1 0 N N-Duplicate
2-Line 2 Line 2 0 1 N N
3-Line 2 Line 2 0 1 N N-Duplicate
I am using excel and I want to use an if condition to determine when a ID have the same values and if it is available or not. If it has duplicates and is available, I want to have Y (for the one that is available) and Y-Duplicate in Results column for all the same ID-Names (regardless if other ID-Names are available or not) and if not available similar logic.
How can I do this for the whole sheet?
My attempts were based on the following logic. If I am able to do it for individual steps then I can combine. The issue I noticed is that I need to take into account the ID-Name and have it used.
Current formulas:
=IF(AND([#Available] = "Y", [#CountUnique] =1),"Y", "Y-Duplicate")
=IF(AND([#Available] = "Y", [#CountUnique] =0),"Y-Duplicate","")
=IF(AND([#Available] = "N", [#CountUnique] =1),"N", "N-Duplicate")
=IF(AND([#Available] = "N", [#CountUnique] =0),"N-Duplicate","")
Thanks.
Not sure if I understood your logic properly, but just in case, note that you may benefit from you field CountUnique
My formula in Results is:
=IF(C2=1;E2;E2&"-Duplicate")

VBA to list number combinations sequentially in two columns

I would like to list the possible combinations of numbers 1 to 26 in columns A and B, in the following format:
1 1
1 2
1 3
1 4
...
1 25
1 26
2 1
2 2
2 3
...
etc
For Column A, I could have:
Range("A1:A26") = 1
Range("A27:A52") = 2
etc
But this seems long winded and there must be a better way of doing this.
I have found this code as an answer to another question which gives
Range("A1")=-500
Range("A1").Select
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Stop:=500, Trend:=False
as a way to list numbers sequentially but I would need to amend it to reach 26 and then start again from 1, all the way to the end of the list in Column A. How can I achieve this?
Try these formulas and then drag down as far as you need (row 676):
A1 =ROUNDUP(ROW()/26,0)
B1 =IF(MOD(ROW(),26)=0,26,MOD(ROW(),26))
A VBA solution as an alternative to the Excel formulas given in the previous reply. (Untested)
MyRow = 1 ' or wherever you want to start
MyCol = 1 ' or wherever you want to start
For A = 1 to 26
For B = 1 to 26
Cells(MyRow,MyCol).Value = A
Cells(MyRow,MyCol+1).Value = B
MyRow = MyRow + 1
Next B
Next A
The advantage of this approach instead of the formula described above is that it is not dependent on where you place the data. It can also be adapted if you want (e.g.) combinations of 5 to 24, or even A is 1 to 26 and B is 1 to 10.
But, the formula described in the previous answer is also a cool way of doing it.
If you changed:
For A = 1 to 26
For B = 1 to 26
to
For A = 1 to 26
For B = A to 26
then this would be useful for a non-directional combination (e.g. if [1,2] is the same as [2,1]).

fetching data from excel in matlab

I am trying to fetch a column from excel with rows more than 17500. Now problem is that when i call it in MATLAB , it does not gives me whole matrix with all data. it fetches data from somewhere in middle.
Now the real problem is that i have to add up 4 numbers in the column and get average , save it in another column and proceed to next consecutive set of numbers and repeat again till the end..How could i do that in MATLAB .Please help me solve this problem as i am just a rookie. Thank you.
so far i have done is this:
clc
g=xlsread('Data.xlsx',1,'E1:E17500');
x=1;
for i = 1:(17500/4) %as steps has to be stepped at 4 since we need avg of 4
y{i}=((g{x}+g{x+1}+g{x+2}+g{x+3})/4);
x=x+4;
end
xlswrite('Data.xlsx', y, 1, 'F1:F4375');
I see several things here: xlsread with one output gives you a numeric matrix of doubles (not a cell-array). Therefore you should address entries with () and not with {}. The for-loop can be omitted when we use reshape to create a matrix with dimensions 4x4375. The we calculate the average of the 4 values in each column directly with mean (evaluated over the first dimension). To get a column-vector again we have to transpose the result of mean using '.
Here is the code:
g = xlsread('Data.xlsx',1,'E1:E17500');
y = mean(reshape(g,4,[]),1)';
xlswrite('Data.xlsx',y,1,'F1:F4375');
To see in detail what happens within the code, let's see the results of each step using random data for g:
Code:
rng(4);
g = randi(10,12,1)
a = reshape(g,4,[])
b = mean(a,1)
y = b'
Result:
g =
10
6
10
8
7
3
10
1
3
5
8
2
a =
10 7 3
6 3 5
10 10 8
8 1 2
b =
8.5000 5.2500 4.5000
y =
8.5000
5.2500
4.5000

How to find which value creates maximum combination - Excel VBA

I need to find which value creates the maximum combination shown below in Excel Vba
suppose,
I have combinations like this
a a+b a+b+c 1 1+2 1+2+1 3 4
b a+c a+b+d 2 1+1 1+2+3 2 6
c a+d a+b+e 1 1+3 1+2+2 4 5
d a+e a+c+d 3 1+2 1+1+3 3 5
e b+c a+c+e 2 2+1 1+1+2 3 4
b+d a+d+e 2+3 1+3+2 5 6
b+e b+c+d 2+2 2+1+3 4 6
c+d b+c+e 1+3 2+1+2 4 5
c+e b+d+e 1+2 2+3+2 3 7
d+e c+d+e 3+2 1+3+2 5 6
I need to find which combinations creates maximum value, In this case the value "7" is maximum which is created by 2,3,2. Hence I want these value as a output in unique cells.
I may have thousands of combinations, hence i want these to be found automatically and to be output in unique cells automatically and run the program further.
Please help.
Thanks
Balaji
Concept :
The greatest sum of m values within a set of n is the sum of the m bigger values.
Once the values are ordered in descending order, the solution is trivial.
Here is a pure Excel solution, without VBA.
Design & Implementation :
The user input (variable names and values) are in C4:D8.
The idea is the following:
Determine the order of data. For this, I use RANK in the column B.
Then use VLOOKUP in columns G and H to sort the data according to rank.
Then the maximum sums are trivial to compute in : sum of the n bigger numbers.
Here is the code:
A B C D E F G H I J K
1
2 User input Sorted by rank Maximum sums
3 Rank name value Rank name value name value
4 =RANK.EQ(C4;$C$4:$C$8) a 45 1 =VLOOKUP(E4;$A$4:$C$8;2;FALSE) =VLOOKUP(E4;$A$4:$C$8;3;FALSE) 2 numbers =CONCATENATE($F$4;"+";$F$5) =$G$4+$G$5
5 =RANK.EQ(C5;$C$4:$C$8) b 1 2 =VLOOKUP(E5;$A$4:$C$8;2;FALSE) =VLOOKUP(E5;$A$4:$C$8;3;FALSE) 3 numbers =CONCATENATE($F$4;"+";$F$5;"+";$F$6) =$G$4+$G$5+$G$6
6 =RANK.EQ(C6;$C$4:$C$8) c 2 3 =VLOOKUP(E6;$A$4:$C$8;2;FALSE) =VLOOKUP(E6;$A$4:$C$8;3;FALSE) 4 numbers =CONCATENATE($F$4;"+";$F$5;"+";$F$6;"+";$F$7) =$G$4+$G$5+$G$6+$G$7
7 =RANK.EQ(C7;$C$4:$C$8) d 12 4 =VLOOKUP(E7;$A$4:$C$8;2;FALSE) =VLOOKUP(E7;$A$4:$C$8;3;FALSE) 5 numbers =CONCATENATE($F$4;"+";$F$5;"+";$F$6;"+";$F$7;"+";$F$8) =$G$4+$G$5+$G$6+$G$7+$G$8
8 =RANK.EQ(C8;$C$4:$C$8) e 33 5 =VLOOKUP(E8;$A$4:$C$8;2;FALSE) =VLOOKUP(E8;$A$4:$C$8;3;FALSE)
Here is how the result looks:
A B C D E F G H I J K
1
2 User input Sorted by rank Maximum sums
3 Rank name value Rank name value name value
4 1 a 45 1 a 45 2 numbers a+e 78
5 5 b 1 2 e 33 3 numbers a+e+d 90
6 4 c 2 3 d 12 4 numbers a+e+d+c 92
7 3 d 12 4 c 2 5 numbers a+e+d+c+b 93
8 2 e 33 5 b 1
Note that it can be easily extended to more variables.
Expanding on d-stroyer's answer, and converting it to VBA:
create UDF:
Option Explicit
Function GetMax(RNames As Range, RVals As Range, MaxNums As Long) As String
Dim i As Long
Dim j As Long
Dim tmpName As String
Dim tmpVals As Long
Dim Names()
Dim Vals()
Names = RNames
Vals = RVals
For i = 1 To UBound(Names) - 1 'bubble sort
For j = i + 1 To UBound(Names)
If Vals(i, 1) < Vals(j, 1) Then 'largest first
tmpName = Names(i, 1)
Names(i, 1) = Names(j, 1)
Names(j, 1) = tmpName
tmpVals = Vals(i, 1)
Vals(i, 1) = Vals(j, 1)
Vals(j, 1) = tmpVals
End If
Next j
Next i
For i = 1 To MaxNums
If Vals(i, 1) <= 0 Then Exit For
'adding zero, or negative numbers will lower the total
Next i
For j = 1 To i - 1
GetMax = GetMax & Names(j, 1) & ","
'now we know how many values to use (from previous loop)
'make the string up
Next j
If Len(GetMax) > 0 Then
GetMax = Left(GetMax, Len(GetMax) - 1)
'remove the final comma
Else
GetMax = "No result found"
End If
End Function
Call using =getmax(A1:A5,D1:D5,3)
result would be d,b,e
I'll leave the rest up to you ov checking for error conditions, such as more than 1 column passed in the range, or ranges of unequal size, or a max # of items returned being larger than the range size

Resources