如何仅将宏应用于选定的行

时间:2019-04-14 16:56:05

标签: excel vba row apply selected

我真的是Maccros的新手。 我已经使用自动录制制作了一个,但似乎无法仅将其用于所选的行,它一直在与记录相同的行上进行。 我真的需要您的帮助来解决它,并帮助我更好地了解maccros的工作原理

我的宏如下:

Sub COPIERVALEURS()
'
' COPIERVALEURS Macro
'
' Touche de raccourci du clavier: Ctrl+Shift+V
'

    Range("A34:H34").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks     _
        :=False, Transpose:=False
    Range("M34:N34").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("K34").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks     _
        :=False, Transpose:=False
    Range("S34:T34").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("Q34").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks     _
        :=False, Transpose:=False
    Range("Y34:Z34").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("W34").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks     _
        :=False, Transpose:=False
    Range("AE34:AF34").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("AC34").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks     _
        :=False, Transpose:=False
    ActiveWindow.SmallScroll ToRight:=5
    Range("AI34:AJ34").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("AG34").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks     _
        :=False, Transpose:=False
    Range("AK34").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks     _
        :=False, Transpose:=False
End Sub

2 个答案:

答案 0 :(得分:0)

我试图了解您的逻辑,以尽我所能为您提供帮助。选择您要此代码执行的行,在 With 行中导入一个断点,执行并调试代码以查看其是否符合您的要求。为了选择一行,请按一下左侧每行的编号。

代码:

Option Explicit

Sub test()

    Dim RowNo As Long

    With ThisWorkbook.Worksheets("Sheet1")

        RowNo = Selection.Row '<- Here you get the row number you have select

        .Range("M" & RowNo & ":N" & RowNo).Copy '<- Copy range M:N of the RowNo you have selct
        .Range("K" & RowNo).PasteSpecial Paste:=xlPasteValues '<- Paste in Column K row the one tou have select

        .Range("S" & RowNo & ":T" & RowNo).Copy
        .Range("Q" & RowNo).PasteSpecial Paste:=xlPasteValues

        .Range("Y" & RowNo & ":Z" & RowNo).Copy
        .Range("W" & RowNo).PasteSpecial Paste:=xlPasteValues

        .Range("AE" & RowNo & ":AF" & RowNo).Copy
        .Range("AC" & RowNo).PasteSpecial Paste:=xlPasteValues

        .Range("AI" & RowNo & ":AJ" & RowNo).Copy
        .Range("AG" & RowNo).PasteSpecial Paste:=xlPasteValues

        Application.CutCopyMode = False

    End With

End Sub

答案 1 :(得分:0)

感谢错误1004,它对我有很大帮助,最终代码如下所示,并且可以完美运行,谢谢:

Option Explicit

Sub COPIERVALEURS()

' COPIERVALEURS Macro
' Touche de raccourci du clavier: Ctrl+Shift+V

    Dim RowNo As Long

    With ThisWorkbook.Worksheets("PAQ")

        RowNo = Selection.Row '<- Here you get the row number you have select

        .Range("A" & RowNo & ":H" & RowNo).Copy
        .Range("A" & RowNo & ":H" & RowNo).PasteSpecial Paste:=xlPasteValues
        .Range("M" & RowNo & ":N" & RowNo).Copy
        .Range("K" & RowNo).PasteSpecial Paste:=xlPasteValues
        .Range("S" & RowNo & ":T" & RowNo).Copy
        .Range("Q" & RowNo).PasteSpecial Paste:=xlPasteValues
        .Range("Y" & RowNo & ":Z" & RowNo).Copy
        .Range("W" & RowNo).PasteSpecial Paste:=xlPasteValues
        .Range("AE" & RowNo & ":AF" & RowNo).Copy
        .Range("AC" & RowNo).PasteSpecial Paste:=xlPasteValues
        ActiveWindow.SmallScroll ToRight:=5
        .Range("AI" & RowNo & ":AJ" & RowNo).Copy
        .Range("AG" & RowNo).PasteSpecial Paste:=xlPasteValues
        .Range("AK" & RowNo).Copy
        .Range("AK" & RowNo).PasteSpecial Paste:=xlPasteValues

    End With

End Sub