Home > Back-end >  How do I use the Inputbox in VBA to obtain a range from a user from a already open Excel workbook an
How do I use the Inputbox in VBA to obtain a range from a user from a already open Excel workbook an

Time:07-20

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.

CodePudding user response:

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.

  • Related