VBA转置数据

时间:2017-09-28 16:04:32

标签: vba excel-vba excel

如何使数据显示为转置而不是水平?

这是我的代码。它从Cell A1获取值,并仅将数值放入单独的单元格中。

Sub match2()
Dim count, count1 As Integer
Dim holder As String
Dim sample, smallSample    As String
count = 0
count1 = 1
sample = Worksheets("Match2").Range("A1").Value
holder = ""
With Sheet4
Do While count <> Len(sample)
    smallSample   = Left(sample, 1)
    If smallSample   = "0" Or smallSample  = "1" Or smallSample  = "2" Or smallSample = "3" Or smallSample = "4" Or smallSample  = "5" Or smallSample  
= "6" Or smallSample  = "7" Or smallSample  = "8" Or smallSample = "9" Then 
    holder = holder & smallSample
    Else
        If holder <> "" Then
          Cells(count1, 1) = holder
            count1 = count1 + 1     
        End If
        holder = ""
    End If
    sample = (Right(sample, Len(sample) - 1))
Loop
End With
End Sub

1 个答案:

答案 0 :(得分:1)

使其转换为整个A列

代码有一个问题,如果它以数字结尾,它将结束该功能而不打印最后一个数字。例如:inpute:i1i2只输出1。但是,它已使用If Len(sample) = 1 Then Cells(i, count1) = holder

修复
Sub match2()
Dim count As Long, count1 As Long
Dim holder As String
Dim sample As String, smallSample  As String
Dim LastRowa As Long

count = 0
LastRowa = Worksheets("Match2").Cells(Rows.count, "A").End(xlUp).Row
With Sheet4
    For i = 1 To LastRowa
    count1 = 1
    holder = ""
    sample = Worksheets("Match2").Range("A" & i).Value
    Do While count <> Len(sample)
        smallSample = Left(sample, 1)
        If smallSample = "0" Or smallSample = "1" Or smallSample = "2" Or smallSample = "3" Or smallSample = "4" Or smallSample = "5" Or smallSample = "6" Or smallSample = "7" Or smallSample = "8" Or smallSample = "9" Then
        holder = holder & smallSample
        Else
            If holder <> "" Then
              Cells(i, count1) = holder
              count1 = count1 + 1
            End If
            holder = ""
        End If
        If Len(sample) = 1 Then Cells(i, count1) = holder
        sample = (Right(sample, Len(sample) - 1))
    Loop
    Next i
End With
End Sub