避免在另一个 Excel 文件中使用宏 运行
Avoid macro running in another Excel file
此代码与 .xlsm 文件关联,比方说,X
。当我打开 X
文件然后打开第二个文件 Y
时,宏从 X
运行s 到 Y
以及 "delete" 或在Y
中按下了"backspace"。此外,如果我打开 X
和 Y
然后关闭 X
,但 Y
仍然打开 - 如果我按 "delete" 或 "backspace" Y
、X
文件将自动打开。所以,我想避免这种情况,我希望 X
中的代码只是 X
中的 运行。希望不要太糊涂!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim TestCell
Dim RE As Object
Dim REMatches As Object
Dim Cell1_1 As String
Dim Today As String
Dim Cell As String
ThisRow = Target.Row
With Worksheets("Input")
Application.OnKey "{DELETE}", "CleanCell1_1"
Application.OnKey "{BACKSPACE}", "CleanCell1_1"
End With
If Target.Column = 9 Then
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Columns("I:I").Columns.AutoFit
Sheets("Input").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True
Sheets("Input").EnableSelection = xlUnlockedCells
Sheets("Chart").Unprotect
Sheets("Chart").Columns("B:B").Columns.AutoFit
Sheets("Chart").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("Chart").EnableSelection = xlNoRestrictions
Application.ScreenUpdating = True
End If
If Target.Column = 10 Then
Set RE = CreateObject("vbscript.regexp")
With RE
.MultiLine = False
.Global = False
.IgnoreCase = True
.Pattern = "[G,g,Y,y,R,r]"
End With
For Each TestCell In Target.Cells
Set REMatches = RE.Execute(TestCell.Value)
If REMatches.Count > 0 And Len(Target.Value) = 1 Then
If Len(Cells(1, 1).Value) = 1 Then
Today = Now()
Cell1_1 = Sheets("Input").Cells(1, 1).Value
Range("K" & ThisRow) = Cell1_1 + ": " + Format(Today, "ddmmmyy")
End If
ElseIf Target.Value <> vbNullString Then
Row = Target.Row
Cells(Row, 10).Value = vbNullString
MsgBox "Please, type only:" & vbNewLine & vbNewLine & "G for Green" & vbNewLine & "Y for Yellow" & vbNewLine & "R for Red"
End If
Next
End If
End Sub
当 X 工作簿 active/not 处于活动状态时 activate/deactivate Application.OnKey
像这样:
在本工作簿中:
Private Sub Workbook_Activate()
Application.OnKey "{DELETE}", "CleanCell1_1"
Application.OnKey "{BACKSPACE}", "CleanCell1_1"
End Sub
Private Sub Workbook_Deactivate()
Application.OnKey "{DELETE}", ""
Application.OnKey "{BACKSPACE}", ""
End Sub
此代码与 .xlsm 文件关联,比方说,X
。当我打开 X
文件然后打开第二个文件 Y
时,宏从 X
运行s 到 Y
以及 "delete" 或在Y
中按下了"backspace"。此外,如果我打开 X
和 Y
然后关闭 X
,但 Y
仍然打开 - 如果我按 "delete" 或 "backspace" Y
、X
文件将自动打开。所以,我想避免这种情况,我希望 X
中的代码只是 X
中的 运行。希望不要太糊涂!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim TestCell
Dim RE As Object
Dim REMatches As Object
Dim Cell1_1 As String
Dim Today As String
Dim Cell As String
ThisRow = Target.Row
With Worksheets("Input")
Application.OnKey "{DELETE}", "CleanCell1_1"
Application.OnKey "{BACKSPACE}", "CleanCell1_1"
End With
If Target.Column = 9 Then
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Columns("I:I").Columns.AutoFit
Sheets("Input").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True
Sheets("Input").EnableSelection = xlUnlockedCells
Sheets("Chart").Unprotect
Sheets("Chart").Columns("B:B").Columns.AutoFit
Sheets("Chart").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("Chart").EnableSelection = xlNoRestrictions
Application.ScreenUpdating = True
End If
If Target.Column = 10 Then
Set RE = CreateObject("vbscript.regexp")
With RE
.MultiLine = False
.Global = False
.IgnoreCase = True
.Pattern = "[G,g,Y,y,R,r]"
End With
For Each TestCell In Target.Cells
Set REMatches = RE.Execute(TestCell.Value)
If REMatches.Count > 0 And Len(Target.Value) = 1 Then
If Len(Cells(1, 1).Value) = 1 Then
Today = Now()
Cell1_1 = Sheets("Input").Cells(1, 1).Value
Range("K" & ThisRow) = Cell1_1 + ": " + Format(Today, "ddmmmyy")
End If
ElseIf Target.Value <> vbNullString Then
Row = Target.Row
Cells(Row, 10).Value = vbNullString
MsgBox "Please, type only:" & vbNewLine & vbNewLine & "G for Green" & vbNewLine & "Y for Yellow" & vbNewLine & "R for Red"
End If
Next
End If
End Sub
当 X 工作簿 active/not 处于活动状态时 activate/deactivate Application.OnKey
像这样:
在本工作簿中:
Private Sub Workbook_Activate()
Application.OnKey "{DELETE}", "CleanCell1_1"
Application.OnKey "{BACKSPACE}", "CleanCell1_1"
End Sub
Private Sub Workbook_Deactivate()
Application.OnKey "{DELETE}", ""
Application.OnKey "{BACKSPACE}", ""
End Sub