这是对你开始的内容的编辑。我把范围变成动态的,因为我不喜欢让excel循环比它更长。这是我个人的偏好。第一个代码块将复制任何不为0或空白的东西,任何负数都将由它们的正值代表。至少我是如何理解你的问题的。
而且,这个代码看起来在山口N个数据(如您在代码中有),并将数据复制到山口L.如果你想A到B那么简单地改变rng
到= ws.Range("A2", ws.Cells(ws.Rows.Count, "A").End(xlUp))
和myCell.Offset()
到(0, 1)
。
Sub Verify()
Dim ws As Worksheet
Dim rng As Range
Set ws = ThisWorkbook.Sheets(1) 'good form to always define the sheet you're working on
Set rng = ws.Range("N2", ws.Cells(ws.Rows.Count, "N").End(xlUp)) 'dynamic range
For Each myCell In rng
If myCell.Value <> "" And myCell.Value <> 0 Then 'If the cell isn't 0 or ""
If myCell.Value < 0 Then 'Then if it's negative, make it positive and copy it over
myCell.Offset(0, -2).Value = myCell.Value * -1
Else: myCell.Offset(0, -2).Value = myCell.Value 'otherwise copy the value over
End If
End If
Next myCell
End Sub
如果你只是想拷贝过来是大于0的值,而忽略0的,空白负值,则使用此代码:
Sub Verify()
Dim ws As Worksheet
Dim rng As Range
Set ws = ThisWorkbook.Sheets(1) 'good form to always define the sheet you're working on
Set rng = ws.Range("N2", ws.Cells(ws.Rows.Count, "N").End(xlUp)) 'dynamic range
For Each myCell In rng
If myCell.Value <> "" And myCell.Value > 0 Then 'If the cell is > 0 and not ""
myCell.Offset(0, -2).Value = myCell.Value 'copy the value over
End If
Next myCell
End Sub
您正在阅读N列或A列吗? – Alsatian
您想将Col A的正值复制到Col B,除非Col A中的值为0?我有这个权利吗? – Tyeler
Plase解释评论''我需要在这里找到第一项数据,例如N2列N?'' – Alsatian