简体   繁体   中英

How do I use the Inputbox in VBA to obtain a range from a user from a already open Excel workbook and paste it into another, standardized workbook?

I am trying to prompt a user when pressing a button in Excel Workbook A, to go to whatever workbook they want on their computer that is already open, select a range of text (reguardless of user size). Then, I want that range to be pasted in Excel Workbook A, beginning with Cell A9 of Workbook A. The goal is to Essentially grab a bunch of names from one already open, standardized excel sheet and put it into one master excel sheet.

I have the following code:

 Option Explicit

Sub selectRange()

  Dim rng As Range
  
  Set rng = Application.InputBox("Select your range", , , , , , , 8)
  
  
  If rng Is Nothing Then
  MsgBox "Please Select the Range:"
  End If
  Range(rng).Copy
  
  Range("a9").Select



     Range(rng).Paste

  

 

End Sub

Let me know if you need any clarifications!

A problem I have is it will prompt the user the select the data but the data doesn't display in Workbook A nor paste anywhere.

I think something like this should get you close

    Dim lRange As Range
    On Error Resume Next
        Set lRange = Application.InputBox(Title:="Select Range", Prompt:="Select a Range", Type:=8)
        'Type 8 is range, see details here: https://docs.microsoft.com/en-us/office/vba/api/excel.application.inputbox
    On Error GoTo 0
    If Not lRange Is Nothing Then
        Set lRange = lRange.Areas(1) 'Just keep the first area if more than 1 is selected
        Workbooks("A").Worksheets("CollectionSheet").Range("A9").Resize(lRange.Rows.Count, lRange.Columns.Count).Value2 = lRange.Value2
    End If

If I understand what you are trying to do, you'll want to make the "A9" dynamic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM