Copy & paste from workbook to another workbook that the user selects

Roo2021

New Member
Joined
Feb 10, 2021
Messages
8
Office Version
  1. 2010
Platform
  1. Windows
Hi. I want to copy cells (B1 & A4:C25) from sheet 'Data' & paste them into a sheet in another workbook which the user selects. The other workbook to paste them into would already be open but I can't write the name as the destination workbook is a template & its identification changes each time.

1653663362361.png
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
At first, we don't know how many files are open, so ask the user to specify them.
Since there is no information to paste, the value will be pasted in the same position on the active sheet of the specified file.

VBA Code:
Sub Sample()
    Dim Twb As Workbook, Dws As Worksheet
    Dim n, i As Long
    Dim buf As String
    Dim x()
    
    Set Dws = Sheets("Data")
    
    For i = 1 To Workbooks.Count
        ReDim Preserve x(i - 1)
        x(i - 1) = Workbooks(i).Name
        buf = buf & i & ".   " & Workbooks(i).Name & vbCrLf
    Next
    n = Application.InputBox(Prompt:="Number?" & vbCrLf & vbCrLf & buf, Type:=1)
    If n > UBound(x) + 1 Or n = "False" Then Exit Sub
    Set Twb = Workbooks(x(n - 1))
    
    With Twb.ActiveSheet
        .Range("B1").Value = Dws.Range("B1").Value
        .Range("A4:C25").Value = Dws.Range("A4:C25").Value
    End With
    MsgBox "Done"
End Sub
 
Upvote 0
Solution
At first, we don't know how many files are open, so ask the user to specify them.
Since there is no information to paste, the value will be pasted in the same position on the active sheet of the specified file.

VBA Code:
Sub Sample()
    Dim Twb As Workbook, Dws As Worksheet
    Dim n, i As Long
    Dim buf As String
    Dim x()
   
    Set Dws = Sheets("Data")
   
    For i = 1 To Workbooks.Count
        ReDim Preserve x(i - 1)
        x(i - 1) = Workbooks(i).Name
        buf = buf & i & ".   " & Workbooks(i).Name & vbCrLf
    Next
    n = Application.InputBox(Prompt:="Number?" & vbCrLf & vbCrLf & buf, Type:=1)
    If n > UBound(x) + 1 Or n = "False" Then Exit Sub
    Set Twb = Workbooks(x(n - 1))
   
    With Twb.ActiveSheet
        .Range("B1").Value = Dws.Range("B1").Value
        .Range("A4:C25").Value = Dws.Range("A4:C25").Value
    End With
    MsgBox "Done"
End Sub

Thank you Takae, I will try that out.
 
Upvote 0

Forum statistics

Threads
1,215,764
Messages
6,126,749
Members
449,335
Latest member
Tanne

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