Transposing Sections of Data in Excel VBA - excel

I need some help to 'transpose' several blocks of data to another area of the worksheet on some sort of loop. The data is currently set out as per below:
Rob Dave John Peter Jane Lily Mel Amy
1 4 7 3 2 5 8 6
blue green brown black purple orange yellow white
apple pear grape lemon banana kiwi mango strawberry
I was wondering if anyone might know some relevant VBA code that might let me transform it into the following:
Rob Dave John Peter
1 4 7 3
blue green brown black
apple pear grape lemon
Jane Lily Mel Amy
2 5 8 6
purple orange yellow white
banana kiwi mango strawberry
This is just two groups of horizontally arranged data that I want to shift into vertical blocks. The actual workbook contains many more blocks of data so may require some sort of loop. Thanks in advance.

Set the variable nBlocks to the total number of 4 X 4 blocks you have and run:
Sub BlockMover()
Dim nBlocks As Long, N As Long, i As Long
Dim r1 As Range, r2 As Range
nBlocks = 3
For i = 2 To nBlocks
N = Cells(Rows.Count, "A").End(xlUp).Row + 1
Set r1 = Range(Cells(1, 4 * (i - 1) + 1), Cells(4, 4 * (i - 1) + 4))
Set r2 = Range("A" & N)
r1.Copy r2
r1.Clear
Next i
End Sub

I would use the built in function Transpose().

Related

Filter rows based on the count of unique values

I need to count the unique values of column A and filter out the column with values greater than say 2
A C
Apple 4
Orange 5
Apple 3
Mango 5
Orange 1
I have calculated the unique values but not able to figure out how to filer them df.value_count()
I want to filter column A that have greater than 2, expected Dataframe
A B
Apple 4
Orange 5
Apple 3
Orange 1
value_counts should be called on a Series (single column) rather than a DataFrame:
counts = df['A'].value_counts()
Giving:
A
Apple 2
Mango 1
Orange 2
dtype: int64
You can then filter this to only keep those >= 2 and use isin to filter your DataFrame:
filtered = counts[counts >= 2]
df[df['A'].isin(filtered.index)]
Giving:
A C
0 Apple 4
1 Orange 5
2 Apple 3
4 Orange 1
Use duplicated with parameter keep=False:
df[df.duplicated(['A'], keep=False)]
Output:
A C
0 Apple 4
1 Orange 5
2 Apple 3
4 Orange 1

Excel find string in string in range return data in corresponding column

The Excel thingy again. ;-)
I have columns like this:
A B C
UserID Name Org_Name
1 Brian Green Susan Red
2 Niels Red Susan Blue
3 Susan Yellow Brian Green
4 India Orange Serge Black
I am looking for a formula that can find ORG_Name(C) in Name(B) and return the UserID(A) and Name(B) found.
In this case it could look like this:
A B C D E
UserID Name Org_Name FoundID FoundName
1 Brian Green Susan Red N/A N/A
2 Niels Red Susan Blue N/A N/A
3 Susan Yellow Brian Green 1 Brian Green
4 India Orange Serge Black N/A N/A
Anyone?
Formula for FoundID column:
=INDEX($A$2:$A$5,MATCH(C2,$B$2:$B$5,0))
Formula for FoundName column:
=INDEX($B$2:$B$5,MATCH(C2,$B$2:$B$5,0))
Adjust the end row as required (the 5s in both formulas)

Excel repeat cells

I need and approach for this:
A B C
1 house 1 house
2 car 1 car
3 boy 1 boy
4 2 house
5 2 car
2 boy
3 house
3 car
3 boy
…
I want to make a function that when I drag it down I get this result.
In C1 enter:
=ROUNDUP(ROW()/3,0) & " " & INDEX(B:B,IF(MOD(ROW(),3)=0,3,MOD(ROW(),3)))
and copy down
Please try:
=INDIRECT("A"&FLOOR(1+(ROW()-1)/3,1))&" "&INDIRECT("B"&MOD(ROW()-1,3)+1)

Get Top Performer by Subgroup Using Index and Match

I am trying to rank names in Column C from largest to smallest score.
Category Score Name Total Rank Apple Rank Orange Rank
Apple 10 Joe Rachel Rachel 0
Orange 15 Don Natalie 0 Natalie
Apple 20 James Tom Tom 0
Apple 1 Rob Nothing Nothing 0
Orange 3 Mary Gina 0 Gina
Orange 100 Rachel James 0 James
Orange 99 Natalie Don 0 Don
Orange 87 Tom Joe 0 Joe
Apple 27 Gina Mary Mary 0
Orange 30 Nothing Rob 0 Rob
This works in Column E for Apples AND Oranges, with formula in E2 that is
=INDEX($C$2:$C$25,MATCH(1,INDEX(($B$2:$B$25=LARGE($B$2:$B$25,ROWS(E$1:E1)))*(COUNTIF(E$1:E1,$C$2:$C$25)=0),),0))
However, the goal is to compare Apples to Apples and Oranges to Oranges.
Only, the formulas in Columns F and G show "0" values for those rows that aren't in the right Apple/Orange category.
For F2:
=IF($A:$A="Apple",INDEX($C:$C,MATCH(1,INDEX(($B:$B=LARGE($B:$B,ROWS(F$1:F1)))*(COUNTIF(F$1:F1,$C:$C)=0),),0)),0)
For G2:
=IF($A:$A="Orange",INDEX($C:$C,MATCH(1,INDEX(($B:$B=LARGE($B:$B,ROWS(G$1:G1)))*(COUNTIF(G$1:G1,$C:$C)=0),),0)),0)
How do I modify the codes so that 0 values won't show up?
Something like this would be great: (screenshot made by just copy pasting values...)
Apple Rank Orange Rank
Rachel Natalie
Tom Gina
Nothing James
Mary Don
Joe
Rob
Note: Unless the whole column ranges are required the steps below may seem to take an uncomfortably long time if these ranges are not restricted.
Assuming you have what below is in ColumnA:G and a corresponding layout:
then ColumnsI:J may be achieved quite simply by copying ColumnF:G and Paste Special..., Values into I1, then select ColumnsI:J, HOME > Editing - Find & Select, Replace..., Find what: 0, Replace with: , Replace All followed by Find & Select, Go To Special..., select Blanks (only), OK, right-click on one of the chosen cells and Delete..., Shift cells up, OK.
To remove the 0s from ColumnF:G only replacing the final 0 in each formula with "" is sufficient.

excel/vba - find fist and last occurrence of a particular value in a column

So if I have a column such as:
A1
1 Apple
2 Apple
3 Apple
4 Oj
5 Oj
6 Oj
7 Oj
8 Pear
9 Pear
How could I return the values 1 & 3 for Apple, 4 & 7 for OJ, etc?
Formula-wise you can use MATCH functions, e.g. for first Apple position
=MATCH("Apple",A1:A9,0)
for last
=MATCH(2,INDEX(1/(A1:A9="Apple"),0))
or if the fruit are sorted as per your example (or merely grouped) you can get the last by adding the number of apples to the first -1
so with first MATCH function in C1 that would be
=COUNTIF(A1:A9,"Apple")+C1-1

Resources