在一张纸上,我有一个有左右对齐字符串的列。我需要过滤所有左对齐并为每个对齐创建一个新的单元格/行。
我已经开始了
Sub test()
Dim c As Variant
For Each cell In Columns("C")
If cell.HorizontalAlignment <> xlLeft Then
Else: c = cell.Value
End If
Next cell
End Sub
这只是我的基本想法,它肯定不起作用,但也许有人可以帮助我如何在此基础上构建我需要的东西。
答案 0 :(得分:3)
这是你在尝试的吗?
Sub Sample()
Dim oSht As Worksheet
Dim lastRow As Long, i As Long
Dim r1 As Long, r2 As Long
'~~> Change this to the relevant sheet
Set oSht = Sheets("Sheet1")
'~~> Start Row for output sheets
r1 = 1: r2 = 1
With oSht
lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
For i = 1 To lastRow
'~~> I am using Sheet2 and Sheet3. Change as applicable
If .Range("C" & i).HorizontalAlignment = xlLeft Then
.Range("C" & i).Copy Sheets("Sheet2").Range("A" & r1)
r1 = r1 + 1
ElseIf .Range("C" & i).HorizontalAlignment = xlRight Then 'xlGeneral????
.Range("C" & i).Copy Sheets("Sheet3").Range("A" & r2)
r2 = r2 + 1
End If
Next i
End With
End Sub
另请注意xlRight
可能不是您认为的那样。它可能是xlGeneral
所以您可能想要检查它。默认情况下,数字与xlGeneral