попытается настроить последний столбец поиска для поддержки этого, который просто должен обработать ошибку, если соседняя ячейка пуста:
Dim r As Long, lr As Long, lc As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
If Not IsEmpty(Cells(r, 2).Value) Then
lc = Cells(r, 1).End(xlToRight).Column
Cells(r, 1).Copy Range(Cells(r, 2), Cells(r, lc))
End If
Next r
Редактировать:
Аннотирующий код для дополнительной помощи.Обратите внимание, что вы можете также .fillright, используя этот метод, где последний столбец находится в строке.
Sub fsda()
Dim r As Long, lr As Long, lc As Long 'iterating row, last row, last column
lr = Cells(Rows.Count, 1).End(xlUp).Row 'dynamically find last row of column 1, removing need for ".select/.activate" efforts
For r = 2 To lr 'assumes start in row 2 as header is in row 1
If Not IsEmpty(Cells(r, 2).Value) Then 'check for column 2 to make sure it isn't blank... this is needed for 2 reasons: 1) to ensure you don't see 'last column' as the first column of next table to the right and 2) to ensure you don't get an infinite output for lc (no error, just goes on forever)
lc = Cells(r, 1).End(xlToRight).Column 'find last column in specific row
Cells(r, 1).Copy Range(Cells(r, 2), Cells(r, lc)) 'copies, then pastes code into specified range
End If
Next r
End Sub
Edit2:
Использование .fill right:
Dim r As Long, lr As Long, lc As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
If Not IsEmpty(Cells(r, 2).Value) Then
lc = Cells(r, 1).End(xlToRight).Column
Range(Cells(r, 1), Cells(r, lc)).FillRight
End If
Next r