代码片段不处理MS Excel VBA

代码片段不处理MS Excel VBA,excel,vba,Excel,Vba,我有一个包含1300多行VBA的宏,可以在电子表格中执行许多操作。下面复制了最后几个命令 比较城市部分始终有效,比较地址部分始终有效。比较状态永远不起作用 只有在单独的宏中运行比较状态时,比较状态才起作用。如果我把它移到代码开始时的另一个部分,或者在代码的中间等,但是它只在我第一次运行时运行。如果我打开一个新的文件,城市,地址,邮政编码,他们做的事情和州完全一样,但在不同的列上。但状态逻辑不起作用 我没有收到错误消息。它只是不将NOTNULL复制到Null单元格 '比较左侧和右侧的状态。如果两者

我有一个包含1300多行VBA的宏,可以在电子表格中执行许多操作。下面复制了最后几个命令

比较城市部分始终有效,比较地址部分始终有效。比较状态永远不起作用

只有在单独的宏中运行比较状态时,比较状态才起作用。如果我把它移到代码开始时的另一个部分,或者在代码的中间等,但是它只在我第一次运行时运行。如果我打开一个新的文件,城市,地址,邮政编码,他们做的事情和州完全一样,但在不同的列上。但状态逻辑不起作用

我没有收到错误消息。它只是不将NOTNULL复制到Null单元格

'比较左侧和右侧的状态。如果两者都为空或已满,则不执行任何操作。如果一个为空,另一个为满,请复制到空

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
'比较左右两侧的城市。如果两者都为空或已满,则不执行任何操作。如果一个为空,另一个为满,请复制到空

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
'比较左右两侧的地址。如果两者都为空或已满,则不执行任何操作。如果一个为空,另一个为满,请复制到空

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值。只需将所有内容嵌套在同一个循环中。 使用工作表限定范围的每个实例。用block结束在这里会有一些好处 您无需选择要移动、修改或删除的单元格。这意味着您也不需要依赖活动或选择。它们的所有实例都已在此处删除 您可以设置彼此相等的值,这将加快速度RowsInFile的值越大,您将看到的好处就越多 如果range=vbNullString Then或range=Then,则只需使用vbNullString或类似方法测试范围是否为空,就可以使这一操作稍微容易一些 Application.CutCopyMode=False只是占用了此处的空间。您可以从代码中删除这一点的所有实例,并在End Sub之前保留这一行 欢迎来到堆栈溢出。感谢您在第一篇文章中分享代码!你有了一个好的开始
没有太多信息,但请尝试类似的方法

如果复制格式不重要:

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”或“工作表名称”的“活动表”

如果代码中的其余行与您在此处共享的行相似,那么您可能至少可以去掉其中的一半。选择是不必要的。CutCopyMode=False不是必需的,您可以在末尾使用它一次。您正在使用一种奇怪的方式在单元格中循环。改进的空间很大:IsEmpty可能没有按照您在这里所想的做-它测试变体类型以确定它是否为VT_EMPTY。如果是空字符串或单元格中有公式,则返回false。非常感谢。我是vba编码新手,在学习的过程中不断学习。您提供的代码非常有效。我已经把每个人的评论牢记在心,并将利用这些教训改进我的工作。仅供参考,我的大多数代码行用于添加列标签和格式。我想说25%的行是自定义代码。我将使用你们所有人共享的内容,并尽可能改进。再次感谢您的帮助!我说得太快了。同样的问题再次出现。更多细节:用户应该在表单左侧的列中输入地址、城市、州和邮政编码。他们输入的是文本,而不是公式。他们应该在电子表格最右侧的列中输入相同的内容。有些人在左侧栏中输入,而忘记了右侧栏。有些人从右边进入,而忘记了左边。他们应该查看代码是否在一侧输入并复制到另一侧。如果两边都是空的或者两边都有东西,代码不需要做任何事情。谢谢。我已经学会了如何使用stackoverflow进行编码。诚然,你们都很有帮助,我将从你们的建议和例子中学习。