如果用户选择了错误的起始列并且给出消息,则将代码添加到宏的开头#34;从列中选择一个单元格" E"

时间:2016-06-02 18:46:16

标签: vba excel-vba excel

Sub TryAgainPrint3()
'
' TryAgainPrint3 Macro
'

'
    ActiveCell.Range("A1:H1").Select
    Selection.Copy
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Print").Select
    ActiveCell.Offset(-3, -2).Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Sheets("Problems").Select
    ActiveCell.Offset(0, 8).Range("A1:I1").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Print").Select
    ActiveCell.Offset(2, 0).Range("A1:I1").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Sheets("Problems").Select
    Range("V5").Select
    ActiveCell.Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Print").Select
    ActiveCell.Offset(1, 2).Range("A1").Select
    Range("C5").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
End Sub

1 个答案:

答案 0 :(得分:0)

如果用户未在E列中选择单元格,则要退出子例程,可以将以下If语句添加到代码顶部:

If ActiveCell.Column <> 5 Then
    MsgBox ("Select a cell in Column E")
    Exit Sub
Else:
End If

这能满足您的需求吗?您可以指定任何列,请记住您需要使用列索引,而不是列本身。 E栏=第5栏