Userform help!!

Young_Money

New Member
Joined
Jul 8, 2011
Messages
23
Hi guys..
I currently have a userform that has buttons. Here is the code for one of the buttons. I want to be able to click the button and have my highlighted data copy and paste into "Sheet2". However, I am getting a run time error "1004"
Application defined or object defined error. Below is my code for that button. When I debug it, it says Set rngPaste = Sheets("Sheet2").Cells(i, 1) is wrong.

Sub CommandButton1_Click()

Dim rngCopy As Range
Dim rngPaste As Range
Dim i As Integer

Set rngCopy = Selection
Set rngPaste = Sheets("Sheet2").Cells(i, 1)
rngCopy.Copy rngPaste

End Sub

Would anybody be able to point out what is wrong please?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
The variable i is zero by default because you don't set a value for it. You can't reference Cells(0, 1)
 
Last edited:
Upvote 0
ohhhhh i see
thanks again alpha frog!

One other question
How come when I use the same code in a sub it works?
like this

Sub Messagebox()


Dim rngCopy As Range
Dim rngPaste As Range

i = msgbox("Format New Orders of Last Week? (Carriers)" & vbCrLf & "You need to highlight the cells you want to format and rerun the program", vbOKCancel)

If i = vbOK Then

Set rngCopy = Selection
Set rngPaste = Sheets("New_Orders").Cells(i, 1)
rngCopy.Copy rngPaste
 
Upvote 0
You set the variable i equal to vbOk. The vb constant vbOk is equal to the value 1. So the variable i equals 1. You can reference Cells(1,1)

The real question is what cell do you want to reference?
 
Upvote 0
One other question
How come when I use the same code in a sub it works?

Because your message box will return integer values. Try Debug.Print i after the MsgBox and see. Although I don't know if you really want to use those for the destination?

HTH,
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,874
Members
452,949
Latest member
Dupuhini

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