Cut/Paste values in excel with VBA

nando88

Board Regular
Joined
Jun 22, 2013
Messages
124
I am trying to make a function to cut/paste values from a cell I will to specify in function to another cell I wioll also specify in function.
I tried:
Code:
Function Cortar(a As Rannge, b As Range)
Dim a As Range
Dim b As Range
Selection.Cut
    a.Select
    b.Paste
End Function
But I am getting invalid function.
I know this is simple, but I am a beginner in vba, and can not figure it out.
If someone could please help me, I would really appreciate it.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hello Nando,

It's great that you've dived into the world of VBA :D I hope you stick at it as its a very powerful tool.

I have laid out the general construct of a Sub/Function - Cut/Paste routine.

Code:
Sub Main_Code()
Dim Range1 As Range
Dim Range2 As Range

Set Range1 = ActiveSheet.Range("A1")
Set Range2 = ActiveSheet.Range("A2")

Call Cortar(Range1, Range2)

End Sub



Function Cortar(A As Range, B As Range)
A.Cut B
End Function

Should be fairly self explanatory but let me know if you have any questions
Thanks
Caleeco
 
Last edited:
Upvote 0
Thanks for responding.
I used your code and it gives me an a\error saying invalid function.
Maybe I am not typing it right:
Code:
=cortar(a1,b1)
//a1 contains data and b1 is where I want to paste
 
Upvote 0
Ahhh right! Sorry, I misunderstood.

I thought you just wanted a VBA function and not a Excel Worksheet function. I've not done a copy/paste worksheet function before :confused:

Let me go away and try a couple things
Thanks

Caleeco
 
Upvote 0
I am a bit confused as to what you are trying to accomplish here.
Functions are typically used to return a value, not move things around. You would typically use a Sub Procedure in VBA to move things around.
All that entering formulas in Excel sheets can do is return a value to that particular cell that the formula resides in. You cannot have a formula in an Excel cell return a value to a different cell, nor can it "move" anything.

So what is it that you are really looking for? It sounds to me like you want a Sub Procedure in VBA to move a cell. You would have to feed it the ranges you want to move. You could either do that via another VBA procedure, or you could have it prompt the user to enter the ranges when you call the code to run.
 
Upvote 0

Forum statistics

Threads
1,215,545
Messages
6,125,448
Members
449,227
Latest member
Gina V

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