运行此代码时,Excel会一直冻结

时间:2016-08-07 11:03:31

标签: excel vba

我正在从命令按钮(ActiveX)运行一个函数,当我运行它时,excel会每隔一段时间冻结一次。谁知道为什么?

代码从空格式化的工作表中复制范围,并将其插入到放置命令按钮的工作表中(以在那里插入新月)。

Function nyMndFunction(navnArk As String)
Dim gammelMnd As String
Dim nyMnd As String
Dim wstEnt As Worksheet
Dim wstMal As Worksheet
Dim insertRange As Range

Set wstMal = Worksheets("Mal")
Set wstEnt = Worksheets(navnArk)

wstMal.Range(wstMal.Cells(1, 1), wstMal.Cells(41, 11)).Copy

gammelMnd = wstEnt.Cells(4, 2).Value

Select Case gammelMnd
    Case "JANUAR"
        nyMnd = "FEBRUAR"
    Case "FEBRUAR"
        nyMnd = "MARS"
    Case "MARS"
        nyMnd = "APRIL"
    Case "APRIL"
        nyMnd = "MAI"
    Case "MAI"
        nyMnd = "JUNI"
    Case "JUNI"
        nyMnd = "JULI"
    Case "JULI"
        nyMnd = "AUGUST"
    Case "AUGUST"
        nyMnd = "SEPTEMBER"
    Case "SEPTEMBER"
        nyMnd = "OKTOBER"
    Case "OKTOBER"
        nyMnd = "NOVEMBER"
    Case "NOVEMBER"
        nyMnd = "DESEMBER"
    Case "DESEMBER"
        nyMnd = "JANUAR"
End Select

wstEnt.Range("B4").Insert Shift:=xlDown
wstEnt.Cells(4, 2).Value = nyMnd
wstEnt.Cells(3, 3).Select
End Function

然后我在8个不同的工作表中用它来调用它

Private Sub cmd_NyMndBravida_Click()
    Dim navnArk As String

    navnArk = ActiveSheet.Name
    nyMndFunction (navnArk)

End Sub

1 个答案:

答案 0 :(得分:2)

我认为CutCopyMode

wstEnt.Range("B4").Insert Shift:=xlDown模式保持有效

所以在该行之后插入Application.CutCopyMode = False语句:

Function nyMndFunction(navnArk As String)
    Dim gammelMnd As String, monthNames As String

    ...

    wstEnt.Range("B4").Insert Shift:=xlDown

    Application.CutCopyMode = False '<-- statement to be inserted

    wstEnt.Cells(4, 2).value = nyMnd
    wstEnt.Cells(3, 3).Select
End Function

此外,您可能需要考虑以下缩短的代码:

附加到按钮的代码

Private Sub cmd_NyMndBravida_Click()

    nyMndFunction ActiveSheet '<--| just pass the worksheet itself, without having to evaluate its name here and then evaluate it back to the worksheet object in 'nyMndFunction'

End Sub

<强> nyMndSub

因为它没有返回任何内容,所以它是Sub,所以让我们把它写下来并命名为它!

Option Explicit

Sub nyMndSub(wstEnt As Worksheet)
    Dim monthNames As String, nyMnd As String
    Dim iMonth As Long
    Dim wstMal As Worksheet

    Set wstMal = Worksheets("Mal")

    monthNames = "JANUAR,FEBRUAR,MARS,APRIL,MAI,JUNI,JULI,AUGUST,SEPTEMBER,OKTOBER,NOVEMBER,DESEMBER" '<--| month names list string

    iMonth = InStr(monthNames, wstEnt.Cells(4, 2).value) '<--| look for the cell content in the month names list
    If iMonth > 0 Then '<--| if found...
        iMonth = Len(Left(monthNames, iMonth)) - Len(Replace(Left(monthNames, iMonth), ",", "")) + 1 '<--| get its "position" inside the list by counting the delimiter occurrences before it and skip to the "next" one
        If iMonth = 12 Then iMonth = 0 '<--| if the "next position" is outside the 12-elements month names list then skip back to the first element
        nyMnd = Split(monthNames, ",")(iMonth) '<--| get the month names in the finally selected "position"

        wstMal.Range(wstMal.Cells(1, 1), wstMal.Cells(41, 11)).Copy '<-- do the copy juts when needed
        With wstEnt
            .Range("B4").Insert Shift:=xlDown
            Application.CutCopyMode = False '<--| exit cutcopymode as soon as possible, i.e. after the clipboard content hs been exploited and no longer needed
            .Cells(4, 2).value = nyMnd
            .Cells(3, 3).Select
        End With
    End If
End Sub

它使用[Split()] / https://msdn.microsoft.com/en-us/library/office/gg278528.aspx)函数从带有分隔符的字符串中返回一个数组

从链接文档中可以看出,它返回一个从零开始的数组,以便上面的代码在处理iMonth索引

时处理它