>我有一个包含 1,300+ 行 VBA 的宏,可以在电子表格中执行许多操作。下面复制了最后几个命令。
"比较城市"部分始终有效,"比较地址"部分始终有效。"比较状态"永远不会起作用。
"比较状态"仅在我在单独的宏中运行它时才有效。(本身(或者,如果我将其移动到其他部分(在代码的开头或代码的中间等(,它会运行,但它仅在我第一次运行它时才有效。如果我打开一个新文件,城市,地址,Zip工作(它们与"STATE"完全相同,但在不同的列上(但STATE逻辑不起作用。
我没有收到错误消息。它根本不会将"非空"复制到"空"单元格。
' 比较左侧和右侧的状态。如果既为空又已满,则不执行任何操作。如果一个空另一个已满,则复制到空。
For x = 2 To RowsInFile
LeftCell = "G" & x
RightCell = "FN" & x
If IsEmpty(Range(LeftCell)) = True And IsEmpty(Range(RightCell)) = False Then
Range(RightCell).Select
Application.CutCopyMode = False
Selection.Copy
Range(LeftCell).Select
ActiveSheet.Paste
ElseIf IsEmpty(Range(LeftCell)) = False And IsEmpty(Range(RightCell)) = True Then
Range(LeftCell).Select
Application.CutCopyMode = False
Selection.Copy
Range(RightCell).Select
ActiveSheet.Paste
End If
Next x
' 比较左右两边的城市。如果既为空又已满,则不执行任何操作。如果一个空另一个已满,则复制到空。
For x = 2 To RowsInFile
LeftCell = "F" & x
RightCell = "FM" & x
If IsEmpty(Range(LeftCell)) = True And IsEmpty(Range(RightCell)) = False Then
Range(RightCell).Select
Application.CutCopyMode = False
Selection.Copy
Range(LeftCell).Select
ActiveSheet.Paste
ElseIf IsEmpty(Range(LeftCell)) = False And IsEmpty(Range(RightCell)) = True Then
Range(LeftCell).Select
Application.CutCopyMode = False
Selection.Copy
Range(RightCell).Select
ActiveSheet.Paste
End If
Next x
' 比较左右的地址。如果既为空又已满,则不执行任何操作。如果一个空另一个已满,则复制到空。
For x = 2 To RowsInFile
LeftCell = "D" & x
RightCell = "FL" & x
If IsEmpty(Range(LeftCell)) = True And IsEmpty(Range(RightCell)) = False Then
Range(RightCell).Select
Application.CutCopyMode = False
Selection.Copy
Range(LeftCell).Select
ActiveSheet.Paste
ElseIf IsEmpty(Range(LeftCell)) = False And IsEmpty(Range(RightCell)) = True Then
Range(LeftCell).Select
Application.CutCopyMode = False
Selection.Copy
Range(RightCell).Select
ActiveSheet.Paste
End If
Next x
- 无需多次遍历相同的
x
值。只需将所有内容嵌套在同一循环中即可。 - 使用工作表限定
Range
的每个实例。With
/End With
块在这里会伸张正义 - 您无需
Select
单元格即可移动、修改或删除它。这意味着您也不需要依赖Active
或Selection
.它们的所有实例都已在此处删除 - 您可以设置彼此相等的值,这将加快速度(
RowsInFile
的值越大,您看到的好处就越多( - 您可以通过测试范围是否为空白并带有
vbNullString
或类似""
(If Range(?) = vbNullString Then
或If Range(?) = "" Then
Application.CutCopyMode = False
只是在这里占用空间。您可以从代码中删除此内容的所有实例,并在End Sub
之前保留此行一次
For x = 2 To RowsInFile
If IsEmpty(Range("G" & x)) = True And IsEmpty(Range("FN" & x)) = False Then
Range("G" & x).Value = Range("FN" & x).Value
ElseIf IsEmpty(Range("G" & x)) = False And IsEmpty(Range("FN" & x)) = True Then
Range("FN" & x).Value = Range("G" & x).Value
End If
If IsEmpty(Range("F" & x)) = True And IsEmpty(Range("FM" & x)) = False Then
Range("F" & x).Value = Range("FM" & x).Value
ElseIf IsEmpty(Range("F" & x)) = False And IsEmpty(Range("FM" & x)) = True Then
Range("FM" & x).Value = Range("F" & x).Value
End If
If IsEmpty(Range("D" & x)) = True And IsEmpty(Range("FL" & x)) = False Then
Range("FL" & x).Value = Range("D" & x).Value
ElseIf IsEmpty(Range("D" & x)) = False And IsEmpty(Range("FL" & x)) = True Then
Range("FL" & x).Value = Range("D" & x).Value
End If
Next x
- 欢迎来到堆栈溢出。感谢您在第一篇文章中分享代码!您有一个良好的开端
没有太多的信息,但请尝试这样的事情。
如果复制格式并不重要:
Dim x As Integer
Dim LeftCellColumn As Integer
Dim RightCellColumn As Integer
LeftCellColumn = Range("G" & 1).Column
RightCellColumn = Range("FN" & 1).Column
For x = 2 To RowsInFile
With ActiveSheet
If IsEmpty(.Cells(x, LeftCellColumn)) = True And IsEmpty(.Cells(x, RightCellColumn)) = False Then
.Cells(x, LeftCellColumn).Value = .Cells(x, RightCellColumn).Value
ElseIf IsEmpty(.Cells(x, LeftCellColumn)) = False And IsEmpty(.Cells(x, RightCellColumn)) = True Then
.Cells(x, RightCellColumn).Value = .Cells(x, LeftCellColumn).Value
End If
End With
Next x
或者,如果您想要该格式,则可以这样做:
Dim x As Integer
Dim LeftCellColumn As Integer
Dim RightCellColumn As Integer
LeftCellColumn = Range("G" & 1).Column
RightCellColumn = Range("FN" & 1).Column
For x = 2 To RowsInFile
With ActiveSheet
If IsEmpty(.Cells(x, LeftCellColumn)) = True And IsEmpty(.Cells(x, RightCellColumn)) = False Then
.Range(.Cells(x, RightCellColumn), .Cells(x, RightCellColumn)).Copy .Range(.Cells(x, LeftCellColumn), .Cells(x, LeftCellColumn))
ElseIf IsEmpty(.Cells(x, LeftCellColumn)) = False And IsEmpty(.Cells(x, RightCellColumn)) = True Then
.Range(.Cells(x, LeftCellColumn), .Cells(x, LeftCellColumn)).Copy .Range(.Cells(x, RightCellColumn), .Cells(x, RightCellColumn))
End If
End With
Next x
如果这不能解决您的问题,向我们提供您放入此公式的一些数据会有所帮助。
顺便说一下,在第一个代码中,"使用ActiveSheet"是不必要的,在第二个代码中,它不想在没有这个的情况下运行。您可以将"活动表"更改为"工作表(1("或"工作表("名称"(。