运行此代码时Excel保持冻结状态

我正在运行一个命令button(ActiveX)的function,当我运行它,excel冻结每隔一段时间左右。 有没有人看到为什么?

该代码复制一个空的格式化的工作表范围,并将其插入放置命令button的工作表(以在那里插入一个新月)。

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 

我相信这是因为CutCopyMode模式在wstEnt.Range("B4").Insert Shift:=xlDown之后保持活动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 

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

代码附加到button

 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, ie 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 )函数从带有分隔符的string中返回一个数组

正如你从链接文档中看到的,它返回一个从零开始的数组,以便上面的代码在处理iMonth索引时能够应付它