我试图找出如何编写代码,如果在Sheet1上给出一组数据:
例如:
C
30 1234
31 0
32 0
33 56
34 789
(所有这些数字的范围是1-7位。)
我想将数据复制到Sheet2上,如:
X Y Z Z AA AB AC AD
57 1 2 3 4
58 5 6
59 7 8 9
这只是一个简单的例子,我有一长串数据有时包含数字0,但我只想将大于零的数字复制到Sheet2上。
到目前为止,这是我到目前为止编写的代码:
Sub IfBlankNext()
Dim i As Integer
For i = 1 To 99
If Worksheets("Sheet1").Cells(i,1).Value <> 0 Then
{code code code}
End If
Next i
End Sub
由于某种原因,此代码仅返回最后一个单元格 - 在本例中,来自Sheet1单元格(99,3)的单元格数据
答案 0 :(得分:0)
在我的代码运行之前:
Sub IfBlankNext()
Dim i As Integer, x As Integer, LastRow As Long, DestLast As Long, HoldVal As String
'Define your last row of data by going to end and moving up:
LastRow = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
If Sheets("Sheet1").Range("C" & i).Value > 0 Then
'Define the LastCol on your destination sheet using similar method as row:
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
在我的代码之后:
更改单元格对齐的代码:
Sheets("Sheet2").Range("Name of Range").HorizontalAlignment = xlLeft OR xlRight