Consulting

Results 1 to 4 of 4

Thread: Interactive selection of worksheet and range

  1. #1

    Interactive selection of worksheet and range

    Hi All,

    I am working on a script which compares the cell values of two ranges in different workbooks. I could work out the code with the actual workbooks, worksheets and range names manually written in the code before running the script. Now I want to generalize this script to select the workbooks, worksheets and the ranges while running the script. I could select the workbooks by the GetOpenFileName function. Once the workbook is open, I want to scroll to the worksheet, range and then import the information (like "Sheet2" and "A4 D65") into the script just by selecting them. The program has to wait until I select the parameters and then start running again by capturing the selected information.
    Please advise how I can do it.
    Thanks in advance

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    [VBA]Sub Test()

    Dim Selectedarea As Range
    Dim MyErr As Long

    On Error Resume Next
    Set Selectedarea = Application.InputBox(prompt:="Select the cell/range...", _
    Title:="SWITCH CELL SELECTION", Default:=Selection.Address, Type:=8)
    MyErr = Err
    On Error GoTo 0
    If MyErr <> 0 Then
    MsgBox "You press Cancel, this procedure will now terminate."
    Exit Sub
    End If

    MsgBox Selectedarea.Cells.Count 'Change to do what you want if Cells.Count = 1

    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Hi Lucas,

    Thanks for the advice. That worked very well. I had to modify slightly to get the worksheet name into the code. Since I am not proficient in VBA, will you please explain me what is the purpose of the following: ( I am sure that there is a good reason)

    Dim MyErr As Long
    MyErr = Err
    On Error GoTo 0
    If MyErr <> 0 Then
    MsgBox "You press Cancel, this procedure will now terminate."
    Exit Function
    End If

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    run the sub I gave you and hit cancel instead of selecting a range....
    simple error handler with msgbox for cance.....use or discard as you wish.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •