cut cells to different file

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I am trying to cut and paste a range. The code below is giving me out of range error mesage. it opens the second file but wont do the cut. Thank you so much
Code:
Sub cutcells()
    Dim x As String
    x = InputBox("enter workbooks name")
    Workbooks.Open Filename:=x
    Workbooks(1).Worksheets(1).Range("a1:a25").Cut Workbooks(x).Worksheets(1).Range("e1")
    Workbooks(x).Save
    Workbooks(x).Close
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
If it won't do the cut, maybe Workbooks(1) is not the workbook you think it is. Do you have any other open workbooks? You can step through the code using the F8 key in the vbEditor mode to check variable values. You can insert message boxes to see what workbook and sheet names are for the indexed values.
 
Last edited:
Upvote 0
Thank you. I actually did before posting
?workbooks(1).name
?workbooks(2).name
in the intermediate windows and everything seems fine but still not able to cut
 
Upvote 0
You may be encountering one or more of these error sources:
1. If you have a PERSONAL.XLSB workbook Excel will open it first, but hidden. That makes it the workbook with index 1 even though its hidden. So, referring to open workbooks by their index values can be problematic.
2. If the user enters a full path for x rather than just the name of the workbook for x, then Workbooks(x) will generate a subscript out of range runtime error.
See if this works for you:
Code:
Sub cutcells()
    Dim WbSource As Workbook, WbDestination As Workbook, x As String
    Set WbSource = ThisWorkbook
    x = InputBox("enter workbooks name")
    Workbooks.Open Filename:=x
    Set WbDestination = ActiveWorkbook
    WbSource.Worksheets(1).Range("a1:a25").Cut WbDestination.Worksheets(1).Range("e1")
    With WbDestination
        .Save
        .Close
    End With
End Sub
 
Upvote 0
Thank you all, it is working now. Both correct, combination of opening many files, protecting etc..
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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