simplify vba code after macro record few minor task together :)

DavidRoger

Board Regular
Joined
Oct 2, 2011
Messages
135
Hi all,

Need to simplify a few minor task together.

First is copy paste value the selected cell. Then move 10 cells to the right and copy paste value. Last move 6 cells to the left.

Below is macro recorded:

Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues
    ActiveCell.Offset(0, 10).Range("A1").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues
    ActiveCell.Offset(0, -6).Range("A1").Select
End Sub


Thanks. :)
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Maybe this is what you are asking for?

Code:
Sub Macro1()
    
    ' copy paste selection as value
    Selection.Value = Selection.Value
    
    '  move 10 cells to the right and copy paste value
    Selection.Offset(0, 10).Value = Selection.Offset(0, 10).Value
    
    ' move 6 cells to the left
    Selection.Offset(0, 4).Select
    
End Sub
 
Upvote 0
If it works, why bother changing it ??
This may be an option, though
Code:
Sub Macro1()
With Selection
    .Value = .Value
    .Offset(0, 10).Value = .Offset(0, 10).Value
    .Offset(0, -6).Select
End With
End Sub
 
Upvote 0
Maybe this is what you are asking for?

Code:
Sub Macro1()
    
    ' copy paste selection as value
    Selection.Value = Selection.Value
    
    '  move 10 cells to the right and copy paste value
    Selection.Offset(0, 10).Value = Selection.Offset(0, 10).Value
    
    ' move 6 cells to the left
    Selection.Offset(0, 4).Select
    
End Sub

Hi Boswell,

That's what I am looking.
For the move 10 cells to the right and copy paste value it doesn't work.
There is formula in that cell.

Rephrase my task below:

Say selected cell is A3.

Task 1 - copy and paste value in cell A3; formula 1+1, after paste = 2
task 2 - move 10 cells to right, J3
task3 - copy and paste value in cell J3; formula 1+2, after paste = 3
task 4- move 6 cells to left, D3
 
Upvote 0
Hi Micheal and Boswell,

Work it out and the final look like this:

Code:
Sub Macro1()
    
    With Selection
    .Value = .Value
    .Offset(0, 9).Value = .Offset(0, 9).Value
    .Offset(0, 3).Select
    End With
    
End Sub[code]

Thank you Boswell for working code.
Thank you Micheal for better coding.
 
Upvote 0
Hi,

Need to reopen the question.

What if I need to copy paste value for a row of cells instead of a cells?

Says 17 cells to right.

Need to replace this part.

Code:
.Offset(0, 9).Value = .Offset(0, 9).Value
 
Upvote 0

Forum statistics

Threads
1,214,634
Messages
6,120,659
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top