Macro Issues

RumMajor

New Member
Joined
Jul 16, 2012
Messages
11
Ok, I have been working on this code for weeks off and on but I'm finally stumped. I have figured everything else out but why this won't work. This code worked perfectly when all you had to do was type the names of the cells. But now, my boss wants you to use the mouse to select them which should be easy. This is what I have so far.

Sub SwapButton()
'
' SwapButton Macro
cel1 = Application.InputBox("Please select the information of the first student.", , , , , , 8)
cel2 = Application.InputBox("Please select the information of the second student.", , , , , , 8)
c1 = Range(cel1).Select
c2 = Range(cel2).Select
Range(cel1).Value = c2
Range(cel2).Value = c1
End Sub

Every time I try and run the code with the button it is assigned to I get a (Run-time error '1004': Method 'Range of object '_Global'failed) on the 3rd line (c1 = Range(cel1).Select) I need it too stop doing that. Any help you guys can offer would be great, there is ice cream at stake. I need this code to operate just like it is because of formatting and such in the whole workbook so I'm trying to avoid a new code. Just the fix to this one if at all possible. Thanks for the help guys.
 
This works for mouse-selected cells:
Code:
Sub SwapButton()
Dim cel1 As Range, cel2 As Range, tmp As Variant
Set cel1 = Application.InputBox(prompt:="Please select the information of the first student.", Type:=8)
Set cel2 = Application.InputBox(prompt:="Please select the information of the second student.", Type:=8)
tmp = cel1.Value
cel1.Value = cel2.Value
cel2.Value = tmp
End Sub
 
Upvote 0
try
Code:
Sub SwapButton()
Dim cel1, cel2
' SwapButton Macro
Set cel1 = Application.InputBox("Please select the information of the first student.", Type:=8)
Set cel2 = Application.InputBox("Please select the information of the second student.", Type:=8)
x = cel1.Value
y = cel2.Value
cel1.Value = y
cel2.Value = x
End Sub
 
Upvote 0

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