如何仅将宏应用于选定行

How to apply a macro to selected row only

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

我的宏如下:

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

我试着理解你的逻辑,尽我所能帮助你。 Select你想让这段代码执行的行,在With行打断点,执行并调试代码,看是否符合你的要求。为了 select 一行,请按您左侧每行的编号。

代码:

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

感谢 Error 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