已编辑-- if 语句 vba -- 如果单元格有值,如何跳过该单元格
edited-- if statement vba -- how to skip a cell if it has a value
我编辑了我的原文 post 并为您提供了更多详细信息。基本上我有这个代码:
For x = 1 To 1000
If track.Cells(x, 1) = Date Then
If track.Cells(x, 2) = "EU" Then
If track.Cells(x, 3) = "ABCDE" Then
ST.Range("ABCDE").Copy
EU.Cells(41, 2).PasteSpecial
Call ActivateSheet
track.Range(Cells(x, 4), Cells(x, 9)).Copy
**For i = 42 To 1000
If EU.Cells(42 + i, 2) Then
EU.Cells(42 + 1, 2).PasteSpecial
End If
Next i**
End If
End If
End If Next x
我在代码的这个特定部分遇到了问题:
For i = 42 To 1000
If EU.Cells(42 + i, 2) Then
EU.Cells(42 + 1, 2).PasteSpecial
End If
Next i
这基本上是从同一工作簿的另一个工作表复制一系列单元格,并将其放在另一个工作表上。现在,我遇到问题的代码发生了什么,我希望它在粘贴第一个代码后粘贴到下面的行。还有其他方法可以实现这一目标吗?我才刚刚开始自学 VBA 编码。任何建议将不胜感激。
如果您的意思是只想更改 空 的单元格,您可以使用 IsEmpty()
,例如:
For Each cell In rng
If IsEmpty(cell) Then 'To alter cells containing value use If Not IsEmpty...
' Do something
End If
Next
或者如果 单元格值 包含 空格 的可能性,您可以使用
If trim(cell & vbnullstring) = vbnullstring Then
' Do something
End If
要跳过 value 类型为 Text 的单元格,请使用 IsText
If Not Application.IsText(cell) Then
' Do Something
End If
要仅更改 值 仅包含数字,请使用 IsNumeric
If IsNumeric(cell) Then
' Do something
End If
您无需循环查找最后一行的值,只需一次从下往上即可。
替换为:
**For i = 42 To 1000
If EU.Cells(42 + i, 2) Then
EU.Cells(42 + 1, 2).PasteSpecial
End If
Next i**
有了这个:
Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial
工作原理:
Cells(rows.count,2) 是 sheet 和第 2 列中的最后一行(现在是 B1048576,过去是 B65536)
.End(XLup) 告诉它上升直到数据,就像 Excel
中的 CTRL-UP
这告诉我们包含数据的最后一行,我们不想粘贴到这里,因为您会覆盖最后一行,所以:
.offset(1,0) 告诉它离开它所在的位置一行零列。
这就算出了你需要粘贴的地址,那我们就特殊粘贴吧。
希望对您有所帮助
要实际查看其工作原理,请使用 CTRL-G 在 VB 编辑器中直接 window 并粘贴以下代码:
?Cells(Rows.Count, 2).address
?Cells(Rows.Count, 2).End(xlUp).address
?Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).address
在每一行之后按回车键,您将看到向该行添加的每个代码部分如何改变结果
我编辑了我的原文 post 并为您提供了更多详细信息。基本上我有这个代码:
For x = 1 To 1000
If track.Cells(x, 1) = Date Then
If track.Cells(x, 2) = "EU" Then
If track.Cells(x, 3) = "ABCDE" Then
ST.Range("ABCDE").Copy
EU.Cells(41, 2).PasteSpecial
Call ActivateSheet
track.Range(Cells(x, 4), Cells(x, 9)).Copy
**For i = 42 To 1000
If EU.Cells(42 + i, 2) Then
EU.Cells(42 + 1, 2).PasteSpecial
End If
Next i**
End If
End If
End If Next x
我在代码的这个特定部分遇到了问题:
For i = 42 To 1000
If EU.Cells(42 + i, 2) Then
EU.Cells(42 + 1, 2).PasteSpecial
End If
Next i
这基本上是从同一工作簿的另一个工作表复制一系列单元格,并将其放在另一个工作表上。现在,我遇到问题的代码发生了什么,我希望它在粘贴第一个代码后粘贴到下面的行。还有其他方法可以实现这一目标吗?我才刚刚开始自学 VBA 编码。任何建议将不胜感激。
如果您的意思是只想更改 空 的单元格,您可以使用 IsEmpty()
,例如:
For Each cell In rng
If IsEmpty(cell) Then 'To alter cells containing value use If Not IsEmpty...
' Do something
End If
Next
或者如果 单元格值 包含 空格 的可能性,您可以使用
If trim(cell & vbnullstring) = vbnullstring Then
' Do something
End If
要跳过 value 类型为 Text 的单元格,请使用 IsText
If Not Application.IsText(cell) Then
' Do Something
End If
要仅更改 值 仅包含数字,请使用 IsNumeric
If IsNumeric(cell) Then
' Do something
End If
您无需循环查找最后一行的值,只需一次从下往上即可。
替换为:
**For i = 42 To 1000
If EU.Cells(42 + i, 2) Then
EU.Cells(42 + 1, 2).PasteSpecial
End If
Next i**
有了这个:
Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial
工作原理:
Cells(rows.count,2) 是 sheet 和第 2 列中的最后一行(现在是 B1048576,过去是 B65536)
.End(XLup) 告诉它上升直到数据,就像 Excel
中的 CTRL-UP这告诉我们包含数据的最后一行,我们不想粘贴到这里,因为您会覆盖最后一行,所以:
.offset(1,0) 告诉它离开它所在的位置一行零列。
这就算出了你需要粘贴的地址,那我们就特殊粘贴吧。
希望对您有所帮助
要实际查看其工作原理,请使用 CTRL-G 在 VB 编辑器中直接 window 并粘贴以下代码:
?Cells(Rows.Count, 2).address
?Cells(Rows.Count, 2).End(xlUp).address
?Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).address
在每一行之后按回车键,您将看到向该行添加的每个代码部分如何改变结果