假设您在Sheet1上获得了这组数据:
A B C
31 123
32 45
33 6789
(有很长的数字列表,其中没有一个会超过7位数。)
我想要一个将在Sheet2上显示如下数据的宏:
X Y Z AA AB AC AD
58 1 2 3
59 4 5
60 6 7 8 9
请记住数据显示在Sheet1和Sheet2上的范围。
这是我到目前为止所做的:
Sub IfBlankNext()
Dim i As Integer, x As Integer, LastRow As Long, DestLast As Long, HoldVal As String
LastRow = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
For i = 31 To LastRow
If Sheets("Sheet1").Range("C" & i).Value > 0 Then
DestLast = Sheets("Sheet2").Range("X" & Rows.Count).End(xlUp).Row + 1
If DestLast < 58 Then DestLast = 58
HoldVal = Sheets("Sheet1").Range("C" & i).Value
For x = 1 To Len(HoldVal)
Sheets("Sheet2").Cells(DestLast, x + 23).Value = Mid(HoldVal, x, 1)
Next x
End If
Next i
End Sub
答案 0 :(得分:1)
看起来你走在正确的轨道上!这个专栏有点棘手,因为你必须从最右边的列30
开始,减去一个数字的位数,Len(HoldVal)
,然后通过添加x
从那里算起来。 。最终代码如下所示:
Sub IfBlankNext()
Dim i As Integer, x As Integer, LastRow As Long, DestLast As Long, HoldVal As String
LastRow = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
For i = 31 To LastRow
If Sheets("Sheet1").Range("C" & i).Value > 0 Then
DestLast = Sheets("Sheet2").Range("AD" & Rows.Count).End(xlUp).Row + 1
If DestLast < 58 Then DestLast = 58
HoldVal = Sheets("Sheet1").Range("C" & i).Value
For x = 1 To Len(HoldVal)
'--- NEW CODE -------------------
Dim columnNumber As Integer
columnNumber = (30 - Len(HoldVal)) + x
Sheets("Sheet2").Cells(DestLast, columnNumber).Value = Mid(HoldVal, x, 1)
'--------------------------------
Next x
End If
Next i
End Sub
我刚刚在这里运行它,输出符合您的要求。希望有所帮助!