1

I am new to excel VBA, and I need help to figure out how to transpose values from one sheet to another sheet.

Below are the images on how my data is before and how I need the data transposed:

BEFORE TRANSPOSE

COUNT OF CHARACTERISTICS |CLASS |CHAR 1 |CHAR 2 |CHAR 3 |CHAR 4 |CHAR 5 |CHAR 6 |
3 |ABSORBER |TYPE |SIZE |MATERIAL
5 |ADAPTOR |TYPE |SIZE_A |CONNECTION_A |SIZE_B |CONNECTION_B |MATERIAL

AFTER TRANSPOSE

|CLASS |CHAR|
ABSORBER |TYPE
ABSORBER |SIZE
ABSORBER |MATERIAL
ADAPTOR |TYPE
ADAPTOR |SIZE_A
ADAPTOR |CONNECTION_A
ADAPTOR |SIZE_B
ADAPTOR |CONNECTION_B
ADAPTOR |MATERIAL

I have more than 1000 rows of data that needs to be transposed.

Sorry, this is the best I could do with regards to the table.

1 Answer 1

1

I think this function will work based on the sample data in your question. This assumes a few things ("Count" is the first column in the raw data, no blank "Char" fields, ect.), so I can help adjust it more specifically to your exact scenario if needed. We could also probably eliminate a few lines if everything is in one workbook and you don't need/want to set workbook and worksheet objects as variables.

Public Sub Transpose()

Dim Row1 As Long
Dim Column1 As Long
Dim Row2 As Long
Dim CurrentWB As Workbook
Dim CurrentWS As Worksheet
Dim TransposeWB As Workbook
Dim TransposeWS As Worksheet
Dim CurrentClass As String

'Set workbook(s) and worksheets as variables just to make it easier to reference them
'Replace necessary workbooks and worksheets with what holds your actual data
Set CurrentWB = ActiveWorkbook
Set CurrentWS = CurrentWB.Worksheets("Sheet1")
Set TransposeWB = ActiveWorkbook
Set TransposeWS = CurrentWB.Worksheets("Sheet2")

'Set the starting row for the worksheet in which the transposed data will go
Row2 = 1

'Adjust as needed for the row in which your raw data starts and ends
For Row1 = 1 To 1000

    'Store the current class
    CurrentClass = CurrentWS.Cells(Row1, 2)

    'Assuming first "Char" column is 3
    Column1 = 3

    While CurrentWS.Cells(Row1, Column1) <> ""

        TransposeWS.Cells(Row2, 1) = CurrentClass
        TransposeWS.Cells(Row2, 2) = CurrentWS.Cells(Row1, Column1)

        'Move to the next "Char" column in untransposed sheet
        Column1 = Column1 + 1

        'Move to the next row in transposed sheet
        Row2 = Row2 + 1
    Wend

Next Row1

End Sub

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.