Consulting

Results 1 to 5 of 5

Thread: How to run macro only selected rows

  1. #1

    How to run macro only selected rows

    Hi

    I have recorded the macro to run the calculation steps as below. Also, Attached a sheet for better understanding.


    Sub Run_Calc()
    '
    ' Run_Calc Macro
    '
    ' Keyboard Shortcut: Ctrl+g
    '
    Range("S2:K2").Select
    Selection.Copy
    Range("S8:K39").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Calculate
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.CutCopyMode = False
    Calculate
    Range("C7").Select
    Selection.End(xlDown).Select
    End Sub
    By running the above macro, sheet got calculated S8 to DK39.

    My desired result is,

    The above function should run only the number of rows I have selected instead of S8 to DK39.

    For Example, If I run the macro while selecting the Row number 10 to 15, Then it should calculate S10 to DK15 only.

    How do I change them to get desired results?

    Expecting positive reply. Thanks in Advance.
    Attached Files Attached Files
    Last edited by Paul_Hossler; 05-10-2018 at 06:36 AM. Reason: Added CODE tags and removed emojies

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    try:
    Sub Run_Calc()
    Set myrng = Intersect(Selection.Areas(1).EntireRow, Range("S:DK"))
    Range("S2:DK2").Copy myrng
    myrng.Value = myrng.Value
    Range(Range("C7"), Range("C7").End(xlDown)).Select
    End Sub
    The file you attached has Calculation set to automatic, so there should be no need for the Calculate lines.
    Last edited by p45cal; 05-10-2018 at 06:41 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    Grrr.
    cross posted without links:
    https://www.mrexcel.com/forum/excel-...cted-rows.html
    Anbuselvam have a read of http://www.excelguru.ca/content.php?184

  4. #4
    Quote Originally Posted by p45cal View Post
    try:
    Sub Run_Calc()
    Set myrng = Intersect(Selection.Areas(1).EntireRow, Range("S:DK"))
    Range("S2:DK2").Copy myrng
    myrng.Value = myrng.Value
    Range(Range("C7"), Range("C7").End(xlDown)).Select
    End Sub
    The file you attached has Calculation set to automatic, so there should be no need for the Calculate lines.
    Please check the attached file with your suggested macro code. After running the macro it is pop up Go To menu. Please check and modify the code.
    Attached Files Attached Files

  5. #5
    Quote Originally Posted by p45cal View Post
    try:
    Sub Run_Calc()
    Set myrng = Intersect(Selection.Areas(1).EntireRow, Range("S:DK"))
    Range("S2:DK2").Copy myrng
    myrng.Value = myrng.Value
    Range(Range("C7"), Range("C7").End(xlDown)).Select
    End Sub
    The file you attached has Calculation set to automatic, so there should be no need for the Calculate lines.
    I have created new macro and copy paste your code. It is working perfectly. Thanks a lot for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •