PDA

View Full Version : How to run macro only selected rows



Anbuselvam
05-10-2018, 05:32 AM
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.

p45cal
05-10-2018, 06:30 AM
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.

p45cal
05-10-2018, 06:39 AM
Grrr.
cross posted without links:
https://www.mrexcel.com/forum/excel-questions/1055076-how-run-macro-only-selected-rows.html
Anbuselvam have a read of http://www.excelguru.ca/content.php?184

Anbuselvam
05-12-2018, 10:53 PM
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.

Anbuselvam
05-13-2018, 12:13 AM
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.