VBA Transpose from column to Row

Krazy

New Member
Joined
Feb 16, 2016
Messages
8
Hi all,

Thank you in advance for helping.

I am very new for VBA and have some data would like to transpose from column to row where it is dynamic, see below, please -

Item
- Value
1
D
2
A
B
C
D
E
3C

<colgroup><col style="width:58pt" span="2" width="77"> </colgroup><tbody>
</tbody>

To Make it like below

Item Whitelist Values
1 D
2 A B C D E
3 C

I have searched the forum for quite long, but only could found those transpose from row to column.:confused:

After transposed, I would like to use VBA to validate data from other range,

say, if the cell "A1" equals 2 in other range, then VBA to help to check the "white list's value" of 2, thus, the cell "B1" could be either A/B/C/D/E, mark "Value in range", else mark "Value not in range" in "C1"

As below -

A B C
2 D "

If the item is not on the list, mark "Item not found" in "C1"

A B C
4 A "Item not found"


Grateful if anyone could help, please. Thank you very much.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Krazy,

Welcome to the MrExcel forum.

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?


I found your instructions a little confusing.

Here is a macro for you to consider, based on your flat text display.

You can change the raw data worksheet name in the macro.

Sample raw data, and, results:


Excel 2007
ABCDEF
1Item- ValueItemWhitelist Values
21D1D
32A2A B C D E
4B3C
5C4Value not in range
6D
7E
83C
94
10
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgData()
' hiker95, 02/16/2016, ME921710
Dim a As Variant, lr As Long, r As Long, nr As Long, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = .Columns("A:B").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
  a = .Range("A1:B" & lr)
  With .Range("A2:A" & lr)
    .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C"
    .Value = .Value
  End With
  .Range("A2:B" & lr).Sort key1:=.Range("A2"), order1:=1, key2:=.Range("B2"), order2:=1
  .Columns("E:F").ClearContents
  .Range("E1").Resize(, 2).Value = Array("Item", "Whitelist Values")
  nr = 1
  For r = 2 To lr
    n = Application.CountIf(.Columns(1), .Cells(r, 1).Value)
    If n = 1 Then
      nr = nr + 1
      .Cells(nr, 5).Value = .Cells(r, 1).Value
      If Not .Cells(r, 2) = vbEmpty Then
        .Cells(nr, 6).Value = .Cells(r, 2).Value
      Else
        .Cells(nr, 6).Value = "Value not in range"
      End If
    ElseIf n > 1 Then
      nr = nr + 1
      .Cells(nr, 5).Value = .Cells(r, 1).Value
      .Cells(nr, 6).Value = Join(Application.Transpose(Range("B" & r & ":B" & (r + n) - 1)), " ")
    End If
    r = r + n - 1
  Next r
  .Range("A1:B" & lr) = a
  .UsedRange.Columns.AutoFit
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorgData macro.
 
Last edited:
Upvote 0
Thanks a lot for the prompt reply and sorry for the confusion caused.

I am using Windows 7 with Excel 2010 and 2013.

Let me try to make it clear -

I am creating a database for data validation, not sure whether it is necessary for data validation to make those values transposed. I've tried your code, can it be making those values of item one cell each rather than one cell to include all the values?

After creating the database, I need to use those transposed data to verify new collected raw data, say from another worksheet, then I wish to run the code to check whether those values of the items within my whitelist.


<tbody>
item
value ItemWhitelist Values
Raw Data (item)valueMatching Result
1D 1D 1E <--not match the values on my whitelist, show "error message"
2A 2A B C D
1D <--match the values on my whitelist, show "match"
B 3D 5A <--item not in my whitelist, show "new item & value"
C 4E
D
3D
4E

<colgroup><col><col><col span="2"><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
</tbody>
 
Upvote 0
Don't know why I can't paste my excel table here..

item
value Item
Whitelist Values Raw Data (item)valueMatching Result
1
D 1D

1E
<--not match the values on my whitelist, show "error message"
2A
2A B C D
1D
<--match the values on my whitelist, show "match"
B 3D 5A <--item not in my whitelist, show "new item & value"
C 4E
D
3D
4E

<colgroup><col style="mso-width-source:userset;mso-width-alt:1316;width:27pt" width="36"> <col style="mso-width-source:userset;mso-width-alt:1499;width:31pt" width="41"> <col style="width:48pt" span="2" width="64"> <col style="mso-width-source:userset;mso-width-alt:1316;width:27pt" width="36"> <col style="mso-width-source:userset;mso-width-alt:4022;width:83pt" width="110"> <col style="width:48pt" width="64"> <col style="mso-width-source:userset;mso-width-alt:3766;width:77pt" width="103"> <col style="width:48pt" width="64"> <col style="mso-width-source:userset;mso-width-alt:3730;width:77pt" width="102"> <col style="mso-width-source:userset;mso-width-alt:14848;width:305pt" width="406"> </colgroup><tbody>
</tbody>
 
Upvote 0
Normally, I use match and vlookup to check, but I can't get it work for those with several whitelist values, like item 2, it could be A/B/C/D, match seems could not handle this, and I don't want to write a lot of "or" if I could transpose those values of item 2 into one cell each in the same row.

=MATCH(I5,(VLOOKUP(H5,E:F,2,FALSE)),0)

Thank you again for your help.
 
Upvote 0
Krazy,

I do not understand what you are trying to do from your reply #3, #4, and, #5.

Click on the Reply to Thread button, and just put the word BUMP in the thread. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Upvote 0

Forum statistics

Threads
1,215,489
Messages
6,125,093
Members
449,205
Latest member
ralemanygarcia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top