Excel VBA 合并两个 SUB
Excel VBA combine two SUBs
是否有机会结合以下两个代码获得帮助?
我会尝试自学如何将这些东西结合起来,因为我确信它并没有那么复杂,但现在我将不胜感激任何帮助。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Application.CutCopyMode = False Then
Application.Calculate
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim UndoList As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error GoTo ErrExit
UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Left(UndoList, 5) = "Paste" Or UndoList = "Auto Fill" Then
MsgBox "Copy / paste is not permitted" & vbCr & _
"- Creator"
With Application
.Undo
.CutCopyMode = False
End With
Target.Select
End If
'The UperCase part______________________________________________
If Not (Application.Intersect(Target, Range("E8:OF57")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
'_______________________________________________________________
ErrExit:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
我正在努力使我的工作簿尽可能易于使用,并避免用户犯错而弄乱公式等等。
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Dim MyPicture As Object
Dim MyTop As Double
Dim MyLeft As Double
Dim TopRightCell As Range
'-----------------------------------------------------------
'- top right cell
With ActiveWindow.VisibleRange
r = 1
c = .Columns.Count
Set TopRightCell = .Cells(r, c)
End With
'------------------------------------------------------------
'- position picture
Set MyPicture = ActiveSheet.Pictures(1)
MyLeft = TopRightCell.Left - MyPicture.Width - 200
With MyPicture
.Left = MyLeft
End With
End Sub
以Private Sub
或Sub
开头的行是宏的开始,End Sub
行是宏的结尾。
在您粘贴的两个代码块中,顶部包含两个宏(一个 Worksheet_SelectionChange
和一个 Worksheet_Change
),第二个块仅包含一个 SelectionChange
。
根据您希望合并的那些,只需 cut-paste 一个子内部的代码(即不包括开始和结束行 Private Sub
和 End Sub
)到另一个子中,以制作一个包含两组代码的合并子。您可能希望合并所有三个,但我猜您只是想合并两个 SelectionChange subs。
是否有机会结合以下两个代码获得帮助?
我会尝试自学如何将这些东西结合起来,因为我确信它并没有那么复杂,但现在我将不胜感激任何帮助。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Application.CutCopyMode = False Then
Application.Calculate
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim UndoList As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error GoTo ErrExit
UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Left(UndoList, 5) = "Paste" Or UndoList = "Auto Fill" Then
MsgBox "Copy / paste is not permitted" & vbCr & _
"- Creator"
With Application
.Undo
.CutCopyMode = False
End With
Target.Select
End If
'The UperCase part______________________________________________
If Not (Application.Intersect(Target, Range("E8:OF57")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
'_______________________________________________________________
ErrExit:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
我正在努力使我的工作簿尽可能易于使用,并避免用户犯错而弄乱公式等等。
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Dim MyPicture As Object
Dim MyTop As Double
Dim MyLeft As Double
Dim TopRightCell As Range
'-----------------------------------------------------------
'- top right cell
With ActiveWindow.VisibleRange
r = 1
c = .Columns.Count
Set TopRightCell = .Cells(r, c)
End With
'------------------------------------------------------------
'- position picture
Set MyPicture = ActiveSheet.Pictures(1)
MyLeft = TopRightCell.Left - MyPicture.Width - 200
With MyPicture
.Left = MyLeft
End With
End Sub
以Private Sub
或Sub
开头的行是宏的开始,End Sub
行是宏的结尾。
在您粘贴的两个代码块中,顶部包含两个宏(一个 Worksheet_SelectionChange
和一个 Worksheet_Change
),第二个块仅包含一个 SelectionChange
。
根据您希望合并的那些,只需 cut-paste 一个子内部的代码(即不包括开始和结束行 Private Sub
和 End Sub
)到另一个子中,以制作一个包含两组代码的合并子。您可能希望合并所有三个,但我猜您只是想合并两个 SelectionChange subs。