2014-01-23 37 views
0

我需要下面的代碼才能正常工作,因爲它目前沒有任何幫助,我們將不勝感激 我一直在嘗試使用少數變體的年齡,但是隻是似乎無法得到它的工作如果activecell.row小於某個數字,則停止宏工作

Sub INCOMENEWLINE() 
' 
' INCOMENEWLINE Macro 
' 

' 
If ActiveCell.Row < 74 Then 
    MsgBox "You cannot insert a new line here" 
    Exit Sub 
End If 

If ActiveCell.Row > 73 Then 
    ActiveSheet.Unprotect Password:="PB2014" 
    Range("SAFILTER").AutoFilter 
    Range("INCOMENEWLINE").Copy 
    ActiveCell.EntireRow.Insert 
    Selection.RowHeight = 13.5 
    Range("SAFILTER").AutoFilter Field:=1, Criteria1:="O" 
    Dim c As Range 
    For Each c In Range("I5:J5") 
     c.EntireColumn.Hidden = (c.Value = 0) 
    Next c 
Application.ScreenUpdating = True 
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _ 
     , Password:="PB2014" 
    Exit Sub 
End If 

End Sub 
+0

它應該實現什麼功能?你嘗試過調試和步進嗎?什麼不按預期工作? – 2014-01-23 16:33:32

+0

我想在工作表中插入名爲INCOMENEWLINE的特定2行,但不希望人們能夠在第73行上方插入這些行,因爲工作表頂部有標題和計算,我不想讓人能夠在這個工作表輸入到另一個工作簿之間時插入,並且如果行在第73行之前改變,它會將錯誤的信息發送到鏈接文件 – user3228638

+0

我不想將計算放在同一個單獨的工作表中工作簿,然後通過任何一個飼料(我欣賞將是一種解決方法,但在其他原因在這種情況下不起作用) – user3228638

回答

1

使用此代碼VBAProject-> Microsoft Excel中Objects(對象)>工作表Sheet1(Sheet1中)或任何表你想。

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    If ActiveCell.Row < 74 Then 
     MsgBox "You cannot insert a new line here" 
    Else 
     'Insert the rest of your code here 
    End If 
End Sub 

它會自動運行,只要你選擇的變化。所以是的,把它放在你的表單代碼中,而不是模塊或用戶表單。

您可能必須分離,他的宏弄成這個樣子太:

聲明公共變量:

Public StopMacro as Boolean 

然後有這樣的:

Sub SetStopMacro() 

StopMacro = True 

End Sub 

最後:

Sub Macro1() 
    While StopMacro is not False 
     ''Do Stuff 
     ''Do more stuff 
     Exit sub ''This is if you don't want this code running over and over again 
    Wend 
End Sub 

然後您將在Worksheet_SelectionChange子項中調用SetStopMacro。

0

嘗試將activecell.row更改爲selection.row

相關問題