Поскольку ваши три столбца имеют разные размеры, самое безопасное - скопировать их один за другим.Любые ярлыки в стиле PasteSpecial, вероятно, будут вызывать головную боль.
With Range("A1")
Range(.Cells(1, 1), .End(xlDown)).Copy myDestinationRangeA
End With
With Range("B1")
' Column B may be empty. If so, xlDown will return cell C65536
' and whole empty column will be copied... prevent this.
If .Cells(1, 1).Value = "" Then
'Nothing in this column.
'Do nothing.
Else
Range(.Cells(1, 1), .End(xlDown)).Copy myDestinationRangeB
EndIf
End With
With Range("C1")
Range(.Cells(1, 1), .End(xlDown)).Copy myDestinationRangeC
End With
Теперь это уродливо, и более понятным вариантом будет цикл по столбцам, особенно если у вас много столбцов, и вы вставляете их в соседние столбцы в том же порядке.
Sub CopyStuff()
Dim iCol As Long
' Loop through columns
For iCol = 1 To 3 ' or however many columns you have
With Worksheets("Sheet1").Columns(iCol)
' Check that column is not empty.
If .Cells(1, 1).Value = "" Then
'Nothing in this column.
'Do nothing.
Else
' Copy the column to the destination
Range(.Cells(1, 1), .End(xlDown)).Copy _
Destination:=Worksheets("Sheet2").Columns(iCol).Cells(1, 1)
End If
End With
Next iCol
End Sub
РЕДАКТИРОВАТЬ Итак, вы изменили свой вопрос ... Попробуйте пройтись по отдельным ячейкам, проверить, пуста ли текущая ячейка, и если нет, скопировать ее.Не проверял это, но вы поняли:
iMaxRow = 5000 ' or whatever the max is.
'Don't make too large because this will slow down your code.
' Loop through columns and rows
For iCol = 1 To 3 ' or however many columns you have
For iRow = 1 To iMaxRow
With Worksheets("Sheet1").Cells(iRow,iCol)
' Check that cell is not empty.
If .Value = "" Then
'Nothing in this cell.
'Do nothing.
Else
' Copy the cell to the destination
.Copy Destination:=Worksheets("Sheet2").cells(iRow,iCol)
End If
End With
Next iRow
Next iCol
Этот код будет очень медленным, если iMaxRow
большой.Я догадываюсь, что вы пытаетесь решить проблему неэффективным способом ... Определить оптимальную стратегию сложно, когда вопрос постоянно меняется.