VBA inserting copied cells using inputbox

dannyh

Board Regular
Joined
Oct 15, 2007
Messages
156
Hi All

Been trying to figure this out all afternoon had to give up !

I have a range of cells that have been selected and copied via my macro

I am trying to get an input box to allow the user to select the location where the copied cells should be inserted.

workbook has number of sheets PN1252, PN1356, PN1458, ect ect and a summary sheet.

the summary sheet has the sheet names listed in col A, each separated by 3 rows.

The ideal senario would be that each of the copied selections on the various sheets (PN) would be inserted into the summary sheet under the relevant headings automatically, failing that a user input box where the correct location is able to be set would be ok.

Code:
For i = 1 To ws_num
    ActiveWorkbook.Worksheets(i + 2).Activate
    
    'Subtotaling data, copying visible cells only and pasting to new location ready to be inserted
    
    Dim lastRow As Integer
lastRow = Range("d5000").End(xlUp).row
Rows(lastRow & ":" & lastRow).Delete shift:=xlUp

ActiveSheet.Outline.ShowLevels RowLevels:=2

numRows = Cells(Rows.Count, "f").End(xlUp).row - 2
numCols = 2

Range("d1", Cells(Rows.Count, "d").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Select

Dim newRange As Range
Set newRange = Range(ActiveCell, ActiveCell.Offset(numRows - 1, numCols))
'MsgBox newRange.Address
newRange.Select
Selection.SpecialCells(xlCellTypeVisible).Copy
    
ActiveCell.Offset(0, 4).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

'Trying to use inputbox to set insert area !!! code has been edited and changed quite a bit so is probably quite wrong !

Dim N As Range
Dim sR As String, sS As String, sTemp As String

sR = Selection.Address
sS = ActiveSheet.Name
sTemp = "'" & sS & "'!" & sR

starting_ws.Activate 'goes to summary sheet

Set N = Application.InputBox(Prompt:= _
                "Please select a range with your Mouse to be bolded.", _
                    Title:="SPECIFY RANGE", Type:=8)
Application.GoTo N

Range(sR).Copy Destination:=Range(sTemp)

Range(sR).Cut Destination:=N
Range(sTemp).Range("A1").Resize(Selection.Rows.Count, Selection.Columns.Count).Copy Destination
'Range(sTemp).Insert shift:=xlDown


Next i

All help appreciated.

Dan
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Dan

What problems are you having with the posted code?
 
Upvote 0
Dan

What problems are you having with the posted code?

Hi Norie

Run-time error 1004
Copy method of range class failed

on this line of code

Code:
Range(sTemp).Range("A1").Resize(Selection.Rows.Count, Selection.Columns.Count).Copy Destination

Like I say I probably haven't got the best solution for the problem but I just need it working now as have about 10 jobs that are waiting for me to look at !

Thanks Dan
 
Upvote 0
Dan

Is this the range of cells you want to copy?
Code:
Range("d1", Cells(Rows.Count, "d").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Select
And this is where the user selects where to copy to?
Code:
Set N = Application.InputBox(Prompt:= _
                "Please select a range with your Mouse to be bolded.", _
                    Title:="SPECIFY RANGE", Type:=8)
 
Upvote 0
Dan

Is this the range of cells you want to copy?
Code:
Range("d1", Cells(Rows.Count, "d").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Select


That selects 1 cell Norie, this code then chooses the range that cell is attached to and selects it

Code:
Set newRange = Range(ActiveCell, ActiveCell.Offset(numRows - 1, numCols))
newRange.Select
[\code]


And this is where the user selects where to copy to?
[code]
Set N = Application.InputBox(Prompt:= _
                "Please select a range with your Mouse to be bolded.", _
                    Title:="SPECIFY RANGE", Type:=8)

Yes the user picks the location via that code

[\QUOTE]

Thanks Dan
 
Upvote 0
If you want to copy newRange to N all you should need is this.
Code:
newRange.Copy N
 
Upvote 0

Forum statistics

Threads
1,223,934
Messages
6,175,485
Members
452,647
Latest member
MatthewBiersay

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