Eu tenho uma planilha parecida com esta:
A | B | C | D | E |
----------------------------------------
62| Value1| Value2| | |
345| Value3| Value4| Value5| Value6|
17| Value7| Value0| | |
111| Value8| Value9| ValueA|ValueC |
Eu gostaria de transformá-lo assim (A é padrão, próximas duas células - B&C, D&E, ..):
A | B | C |
-----------------------
62| Value1|Value2|
345| Value3|Value4|
345| Value5|Value6|
17| Value7|Value0|
111| Value8|Value9|
111| ValueA|ValueC|
Atualmente estou usando a macro abaixo para converter apenas uma linha, mas quero com valor de duas células.
Sub Transform()
Dim rowStr As String
Dim rowIndex As Integer
rowIndex = 1
For Each Cell In Sheet1.Range("A1:E5")
If Cell.Column = 1 Then
rowStr = Cell.Value
ElseIf Not IsEmpty(Cell.Value) Then
Sheet2.Cells(rowIndex, 1) = rowStr
Sheet2.Cells(rowIndex, 2) = Cell.Value
rowIndex = rowIndex + 1
End If
Next Cell
End Sub
Responder1
Eu usaria isso.
Sub Transform_2()
Dim c As Range
Dim rngFirstCol As Range
Dim rowIndex As Long
Dim j As Long
rowIndex = 1
Set rngFirstCol = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(1, 1).End(xlDown))
For Each c In rngFirstCol
For j = 0 To rngFirstCol.CurrentRegion.Columns.Count - 2 Step 2
If c.Offset(, j + 1).Value <> "" Or c.Offset(, j + 2).Value <> "" Then
Sheet2.Cells(rowIndex, 1).Value = c.Value
Sheet2.Cells(rowIndex, 2).Resize(1, 2).Value = c.Offset(, j + 1).Resize(1, 2).Value
rowIndex = rowIndex + 1
End If
Next
Next
End Sub
Ele decide automaticamente quantas linhas sua tabela de dados possui. Se desejar, você mesmo pode configurá-lo alterando
Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(1, 1).End(xlDown))
para umreferência de colunacomo
Sheet1.Range("A1:A6")