Copy paste from variable source and target to NONE continious range

Enrix

New Member
Joined
Jun 24, 2011
Messages
4
I have created a Userform, where you can enter a Operand (into a text box) and direction (Vertical or Horizontal) option buttons. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
When activating the macro (Command button) the user is prompted to select a range (only within one row or one column) from an open workbook (Inputbox type:=8), which is the source and then the user is prompted to select (Inputbox type:=8) the start cell for paste (target).<o:p></o:p>
<o:p></o:p>
If Operand is 2, the cells, within the source is pasted, starting with the target cell, skipping every second column or row depending if vertical or horizontal was selected.<o:p></o:p>
<o:p></o:p>
This works PERFECTLY as long as I am working within the same workbook. However, if my source is from another workbook I am NOT able to identify the workbook name from where I copied the source.<o:p></o:p>
<o:p></o:p>
In this code I have hardcoded the workbook names but I need a solution to capture the name, when the range and target is selected.<o:p></o:p>
<o:p></o:p>
This is NOT the entire code but just the part to show where the "challenge" is.<o:p></o:p>
<o:p></o:p>
Dim UserRange As Range
Dim UserStart As Range<o:p></o:p>
<o:p></o:p>
Set UserRange = Application.InputBox(Prompt:= _
"Please Select a Range", _
Title:="InputBox Method", Type:=8) ' & ActiveWorkbook.FullName


Set UserStart = Application.InputBox(Prompt:= _
"Please Select a start cell for paste", _
Title:="InputBox Method", Type:=8)<o:p></o:p>
<o:p></o:p>
Select Case True

Case obVertical

For Each r In WorkRange ' to find the first cell in selection and activate so paste start in this cell
Windows("Workbook name source").Activate ' activate the workbook from where you copy
FirstValue = r.Address
Range(FirstValue).Select
Selection.Copy
Range("workbook name target").Select
ActiveCell.Offset(rowOffset:=OffsetValueV, columnOffset:=0).Activate 'enter offset from start point to paste into second row
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

OffsetValueV = OffsetValueV + tbOperand

Next<o:p></o:p>
<o:p></o:p>
etc.<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
So the problem is how to capture the workbook name since RefEdit cannot be used and the range object returned by using Inputbox do not include the workbook name ?<o:p></o:p>
<o:p></o:p>
Windows("Workbook name source").Activate <o:p></o:p>
and <o:p></o:p>
Windows("Workbook name target").Activate <o:p></o:p>
<o:p></o:p>
I appreciate any support possible.<o:p></o:p>
<o:p></o:p>
Regards<o:p></o:p>
Enrix, big Mr Excel fan<o:p></o:p>
<o:p></o:p>
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Possibly:

Code:
Dim UserRange As Range
Dim UserStart As Range
Dim wbFrom As String, wbTo As String

Set UserRange = Application.InputBox(Prompt:= _
"Please Select a Range", _
Title:="InputBox Method", Type:=8) ' & ActiveWorkbook.FullName
[B]wbFrom = UserRange.Application.ActiveWorkbook.Name & ".xls"[/B]


Set UserStart = Application.InputBox(Prompt:= _
"Please Select a start cell for paste", _
Title:="InputBox Method", Type:=8)
[B]wbTo = UserStart.Application.ActiveWorkbook.Name & ".xls"[/B]
Select Case True

Forget that, looked at it again and it didnt work
 
Last edited:
Upvote 0
When you select range with Inputbox the active workbook, is the workbook from where you have activated your macro (in this case userform)

So no matter where I select source and target the workbook name is the same. regards Enrix

Possibly:


Code:
Dim UserRange As Range
Dim UserStart As Range
Dim wbFrom As String, wbTo As String
 
Set UserRange = Application.InputBox(Prompt:= _
"Please Select a Range", _
Title:="InputBox Method", Type:=8) ' & ActiveWorkbook.FullName
[B]wbFrom = UserRange.Application.ActiveWorkbook.Name & ".xls"[/B]
 
 
Set UserStart = Application.InputBox(Prompt:= _
"Please Select a start cell for paste", _
Title:="InputBox Method", Type:=8)
[B]wbTo = UserStart.Application.ActiveWorkbook.Name & ".xls"[/B]
Select Case True

Forget that, looked at it again and it didnt work
 
Upvote 0
could I use range.parent.parent.name to capture the workbook name ?

When you select range with Inputbox the active workbook, is the workbook from where you have activated your macro (in this case userform)

So no matter where I select source and target the workbook name is the same. regards Enrix
 
Upvote 0

Forum statistics

Threads
1,224,513
Messages
6,179,214
Members
452,895
Latest member
BILLING GUY

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