[VBA] Abraca Dabra's Question



  1. 下载vba文件

  2. 把原本的数据复制到Sheet [1.input] 的Cell A1

  3. 然后点击黄色的按钮,完成

  4. 成果会在Sheet [2.output]
  5. 其他:此次用到的VBA代码

  6. Option Explicit
    
    Sub main()
        Dim horizontalCell As Range
        Dim verticalCell As Range
        Dim verticalRange As Range
        Dim horizontalRange As Range
        Dim i As Integer 'myVeritcalCounter
        Dim j As Integer 'myhorizontalCounter
        i = 1
        
        'To clear output sheet first
        ThisWorkbook.Worksheets("2.output").Cells.Clear
        
        With ThisWorkbook.Worksheets("1.input")
            Set verticalRange = .Range(.Cells(2, 1), .Cells(2, 1).End(xlDown))
            Set horizontalRange = .Range(.Cells(1, 2), .Cells(1, 2).End(xlToRight))
        End With
        
        For Each verticalCell In verticalRange
            
            
            For Each horizontalCell In horizontalRange
                
                With ThisWorkbook.Worksheets("2.output")
                    .Cells(i, 1).Value = verticalCell.Value
                    .Cells(i, 2).Value = horizontalCell.Value
                    .Cells(i, 3).Value = ThisWorkbook.Worksheets("1.input").Cells(verticalCell.Row, horizontalCell.Column)
                    
                    i = i + 1
                End With
            Next horizontalCell
        Next verticalCell
        
        Set verticalRange = Nothing
        Set horizontalRange = Nothing
        
        MsgBox "Done. The output is in Sheet [2.output] now"
    End Sub
        
    
    
                  
    
                



Source: ---


Disclaimer: The information in this webpage is shared by anonymous users from external online sources. We cannot guarantee its accuracy or truthfulness. Users should exercise caution and verify information independently.


© 2023 sunnypeaceisland.com