Domanda

In quantity, how can I transpose this format:

a      a1
b      b1
c      c1
d      d1
e      e1

into a single column as below?

a
a1
b
b1
c
c1
d
d1
È stato utile?

Soluzione 2

You can also do this with VBA without having Excel select or activate anything. It's only coded to work on the active sheet right now, but it'd be very easy to make it work on any sheet that isn't actively selected.

Sub combineColumns()

        Dim sFirstCol As String
        Dim sSecondCol As String

        sFirstCol = "A"
        sSecondCol = "B"

        Dim values() As String
        Dim i As Integer
        Dim t As Integer
        Dim iFirstRow As Integer
        Dim iSecondRow As Integer
        iFirstRow = Range(sFirstCol & 1).End(xlDown).Row
        iSecondRow = Range(sSecondCol & 1).End(xlDown).Row

        ReDim values(iSecondRow - 1)

        For i = 0 To iFirstRow - 1
            values(i) = Range(sSecondCol & i + 1).Value
        Next i

        t = 0
        For i = 0 To iFirstRow * 2 - 1 Step 2
            Range(sFirstCol & i + 2).Insert
            Range(sFirstCol & i + 2).Value = values(t)
            t = t + 1
        Next i

        Range(sSecondCol & 1 & ":" & sSecondCol & iSecondRow * 2).ClearContents

 End Sub

Altri suggerimenti

There are very many ways to achieve what you want. One of the easiest to describe briefly is, assuming your data is in ColumnA:B, use the formula in C1 and copy down;

=IF(ISODD(MOD(ROW(),2)),INDIRECT(CHAR(COLUMN()+61)&(ROW()+1)/2),  
                         INDIRECT(CHAR(COLUMN()+62)&(ROW()/2)))

But one technique I am fond of barely even requires formulae (other than what Excel provides behind buttons). Subtotal A:B (for which A:B would require labels, even if blank) for each change in ColA, Count, Add subtotal to ColA and Summary below data. {This to add rows and would fall down if ColA has consecutive duplicate values - Count should always be 1).

Filter A:C and select B = 1. In B3 enter =C2 and copy down as required. Unfilter, copy ColB and Paste Special Values over the top. Remove subtotals and delete {new} ColA for tidiness. {Could also now delete ColB.}

PS This would be easier if your data was in alternate rows as seemed to be indicated in your post - before I edited it!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top