vba - 我的代码有效,但 Excel 停止响应。代码中没有错误消息

标签 vba excel

我需要一些帮助才能使我的 conde 更简单。
我开始在 VBA 上编写代码并构建自己的脚本,有时它们可​​以正常工作。
但它们总是太大而且比它可能的复杂得多。

这是每次我运行脚本时 Excel 崩溃的一种情况。
有人可以帮助我使这段代码更简单吗?

    Sub Cleaning_Mirexs()

    Application.ScreenUpdating = False

    Dim UltCel As Range
    Dim Mirex As String
    Dim Glip As String

Mirex = "S"
Glip = "UP"

Set UltCel = Cells(Rows.Count, 2).End(xlUp)

' Moving Data for treatment

    Range("R2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("X2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Selection.TextToColumns Destination:=Range("X2"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="-", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

' Mirex Formicide Data

Range("$Y2").Select

    Do While ActiveCell <> UltCel
    If InStr(1, ActiveCell.Text, Mirex) Then
    ActiveCell.FormulaR1C1 = ""
    ActiveCell.Offset(0, -1).Select
    ActiveCell.Clear
    ActiveCell.FormulaR1C1 = "IS FORMICIDA MIREX-S" & ActiveCell.Value
    ActiveCell.Offset(1, 1).Select

    ElseIf ActiveCell.Offset(xlDown) Then

    End If

    Loop

' Glip Herbicide Data

Range("Y2").Select

    Do While ActiveCell <> UltCel
    If InStr(1, ActiveCell.Text, Glip) Then
    ActiveCell.Formula = ""
    ActiveCell.Offset(0, -1).Select
    ActiveCell.Clear
    ActiveCell.FormulaR1C1 = "HB GLIP-UP" & ActiveCell.Value
    ActiveCell.Offset(1, 1).Select

    ElseIf ActiveCell.Offset(1, 0).Select Then

    End If

    Loop

' Light Tractor Data
Range("X2").Select

    Do While ActiveCell <> UltCel
    If InStr(1, ActiveCell.Text, "Tratores leves") Then
    ActiveCell.Clear
    ActiveCell.FormulaR1C1 = "Tratores leves" & ActiveCell.Value
    ActiveCell.Offset(1, 0).Select

    ElseIf ActiveCell.Offset(1, 0).Select Then

    End If

    Loop

' Heavy Tractor Data
Range("X2").Select

    Do While ActiveCell <> UltCel
    If InStr(1, ActiveCell.Text, "Tratores pesados") Then
    ActiveCell.Clear
    ActiveCell.FormulaR1C1 = "Tratores pesados" & ActiveCell.Value
    ActiveCell.Offset(1, 0).Select

    ElseIf ActiveCell.Offset(1, 0).Select Then

    End If

    Loop

' back to original place after data treatment
Range("X2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("X2").Select
    Selection.PasteSpecial Paste:=xlPasteValues


Application.ScreenUpdating = True

MsgBox "Success!"

End Sub

我希望代码可以一次运行所有内容,但是我编写脚本的方式就像是为每个数据集单独运行一样。

好吧,就在这里!让我们找点乐子 :)

谢谢!

玛丽亚

enter image description here

最佳答案

好的,我试图解决这个问题,但我有几个关于你在这里想要完成的问题......例如:

ActiveCell.Clear
ActiveCell.FormulaR1C1 = "Tratores pesados" & ActiveCell.Value

在这里你只是清除你的ActiveCell ,然后添加一些文本,后跟 ActiveCell.Value现在什么都不是,因为您刚刚清除了它。我不确定你的意图是什么。

你也有
ElseIf ActiveCell.Offset(1, 0).Select Then
End If

我认为它没有任何功能,我很困惑只是试图理解为什么这是必要的,所以我省略了它。

我也摆脱了你的Do/Loop s 并将它们替换为 For循环,更可靠。我也摆脱了Select/Activate在大多数情况下,因为那些效率低下。

我也改了UltCelLong对于For循环。

如果其他人想要编辑这个继续,我确定我错过了一些东西(比如我不确定 .TextToColumns 位:
Sub Cleaning_Mirexs()

Application.ScreenUpdating = False

Dim UltCel As Long
Dim Mirex As String, Glip As String
Dim i As Long

Mirex = "S"
Glip = "UP"

UltCel = Cells(Rows.Count, 2).End(xlUp)

'Moving Data for treatment
Range("X2:X" & UltCel).Value = Range("R2:R" & UltCel).Value
Range("X2:X" & UltCel).TextToColumns Destination:=Range("X2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="-", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

For i = 2 To UltCel
    If InStr(Range("X" & i).Value, Mirex) Then
        Range("X" & i).Value1 = "IS FORMICIDA MIREX-S"
    ElseIf InStr(Range("X" & i).Value, Glip) Then
        Range("X" & i).Value = "HB GLIP-UP"
    ElseIf InStr(Range("X" & i).Value, "Tratores leves") Then
        Range("X" & i).Value = "Tratores leves"
    ElseIf InStr(Range("X" & i).Value, "Tratores pesados") Then
        Range("X" & i).Value = "Tratores pesados"
    End If
Next i

For i = 2 To UltCel
    If InStr(Range("Y" & i).Value, Mirex) Then
        Range("Y" & i).Value1 = "IS FORMICIDA MIREX-S"
    ElseIf InStr(Range("Y" & i).Value, Glip) Then
        Range("Y" & i).Value = "HB GLIP-UP"
    ElseIf InStr(Range("Y" & i).Value, "Tratores leves") Then
        Range("Y" & i).Value = "Tratores leves"
    ElseIf InStr(Range("Y" & i).Value, "Tratores pesados") Then
        Range("Y" & i).Value = "Tratores pesados"
    End If
Next i

'back to original place after data treatment
Range("X2:X" & UltCel).Value = Range("X2:X" & UltCel).Value
Range("Y2:Y" & UltCel).Value = Range("Y2:Y" & UltCel).Value

Application.ScreenUpdating = True

MsgBox "Success!"

End Sub

关于vba - 我的代码有效,但 Excel 停止响应。代码中没有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50156709/

相关文章:

vba - 将数字添加到文本时出错

excel - 从 VBA 中的类模块中提取特定变量到标准模块

excel - 用于比较两列并用颜色突出显示单元格差异的 VBA 宏

javascript - 使用 sheetJS 在单个工作表中的多个表

vba - 如果文件名包含特定文本则执行

r - 如何从一个数据框中的字符中提取字符串并放入新表中

vba - 从一个工作簿复制选定的单元格并复制到另一个

使用动态范围时出现 VBA 错误

javascript - Javascript 是否有类似于 VBA 的 DoEvents 的东西?

vba - 选择案例循环