Results 1 to 19 of 19

Thread: [RESOLVED] In Excel, select cells using textboxs

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Resolved [RESOLVED] In Excel, select cells using textboxs

    For Excel, I want to make a form in which the user can enter the first and last cell of a range of information into two text boxes, after selecting a cmdbutton a macro will run manipulating the range that the user has just entered.

    For example if a user puts in B3 in one textbox and G6 in another, upon clicking the cmdbutton on the form vb will select the range of B3:G6.

    I can use cmdbuttons and checkboxes, but textboxes still elude me.
    Last edited by Botillier; Sep 13th, 2005 at 05:57 PM. Reason: typo

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: In Excel, select cells using textboxs

    What are you having triuble with cocerning textboxes?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: In Excel, select cells using textboxs

    You can change the inputbox to one that accepts a range by altering its type (8 = range input)
    You will need error handling, etc., but this should give you a start

    VB Code:
    1. Sub TestRange()
    2. Dim MyRange As Range
    3.  
    4. Set MyRange = Application.InputBox(Prompt:="Test Range Input", Title:="Range Input", Type:=8)
    5.  
    6.  
    7. Debug.Print MyRange.Address
    8.  
    9. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: In Excel, select cells using textboxs

    My problem is I don’t know how to transfer the information placed in a text box into a macro. What I would like is for the text to somehow be placed into a range, for example Range (“TextBox1.Text:TextBox2.Text”).Select, so if the user enters in A1 in TextBox1, and G5 in TextBox2 then in the macro that follows will select Range(“A1:G5”). I definitely would like to use two text boxes to serve my purpose.

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: In Excel, select cells using textboxs

    If your doing this on a UserForm, can I suggest that you consider using 2 RefEdit controls rather that 2 TexBox controls.

    The RefEdit is designed to accept a range value and has all the error checking, etc.

    You can write a proc to ensure that each RefEdit only allows the user to select an individual cell (which is still a range).

    Then you can get the range between these two cells as per the following pseudo-code.

    VB Code:
    1. Set MyRange = Range(RefEdit1,Refedit2)
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: In Excel, select cells using textboxs

    Thankyou for your patience, I have the following information attached to a form but when I input information and click my cmdbutton it goes to debug mode and highlights "Set MyRange......" What am I missing?

    VB Code:
    1. Private Sub cmdTestButton_Click()
    2.  
    3. Set MyRange = Range(RefEdit1, RefEdit2)
    4.  
    5. MyRange.Select
    6.  
    7. End Sub
    8.  
    9. Private Sub RefEdit1_BeforeDragOver(Cancel As Boolean, ByVal Data As MSForms.DataObject, ByVal x As stdole.OLE_XPOS_CONTAINER, ByVal y As stdole.OLE_YPOS_CONTAINER, ByVal DragState As MSForms.fmDragState, Effect As MSForms.fmDropEffect, ByVal Shift As Integer)
    10.  
    11. End Sub
    12.  
    13. Private Sub RefEdit2_BeforeDragOver(Cancel As Boolean, ByVal Data As MSForms.DataObject, ByVal x As stdole.OLE_XPOS_CONTAINER, ByVal y As stdole.OLE_YPOS_CONTAINER, ByVal DragState As MSForms.fmDragState, Effect As MSForms.fmDropEffect, ByVal Shift As Integer)
    14.  
    15. End Sub
    16.  
    17. Private Sub UserForm_Click()
    18.  
    19. End Sub

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: In Excel, select cells using textboxs

    Have you selected a range in each RefEdit before setting MyRange?
    You will need to error trap to ensure that both controls have a single cell range before you set the range.

    Have you Declared the MyRange variable? I don't see it declared here.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: In Excel, select cells using textboxs

    I do have cells in each of my RefEdit boxes, I have not declared a variable, I am still very new at VB and am very fuzzy on the appropriate way to use variables. Would I declare it as a string? Otherwise, what you see is what I have. I also do not know how to "error trap." Thank you again for your help.

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: In Excel, select cells using textboxs

    You need to declare it as a Range object.

    You should always use Option Explicit in all your code as it forces you to declare all variables, believe me - it seems like a hassle at first but is well worth it.

    The following should help, let me know if it doesn't.


    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub CommandButton1_Click()
    4. Dim StartRange As Range
    5. Dim EndRange As Range
    6. Dim ResultRange As Range
    7.  
    8.    
    9. On Error GoTo ErrorHandler
    10.  
    11.     Set StartRange = Range(Me.RefEdit1.Value)
    12.     Set EndRange = Range(Me.RefEdit2.Value)
    13.    
    14.     If StartRange.Worksheet <> EndRange.Worksheet Then GoTo ErrorHandler
    15.    
    16.     If StartRange.Cells.Count <> 1 Or EndRange.Cells.Count <> 1 Then GoTo ErrorHandler
    17.    
    18.     Set ResultRange = Range(StartRange, EndRange)
    19.    
    20.     MsgBox ResultRange.Cells.Count 'just to show you that you now have a range
    21.    
    22.     'insert any other code
    23.    
    24.     Unload Me
    25.    
    26. ErrorHandler:
    27.     Exit Sub
    28. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: In Excel, select cells using textboxs

    The code looks great and I have learned a lot from it allready, unfortunately when I hit the cmdbutton it does not respond. I made sure the names are the same and cells are entered into the refedit boxes, but my form does not respond or unload when I hit the cmdbutton

    VB Code:
    1. Option Explicit
    2. Private Sub CommandButton1_Click()
    3.  
    4. Dim StartRange As Range
    5. Dim EndRange As Range
    6. Dim ResultRange As Range
    7.    
    8. On Error GoTo ErrorHandler
    9.  
    10.     Set StartRange = Range(Me.RefEdit1.Value)
    11.     Set EndRange = Range(Me.RefEdit2.Value)
    12.    
    13.     If StartRange.Worksheet <> EndRange.Worksheet Then GoTo ErrorHandler
    14.    
    15.     If StartRange.Cells.Count <> 1 Or EndRange.Cells.Count <> 1 Then GoTo ErrorHandler
    16.    
    17.     Set ResultRange = Range(StartRange, EndRange)
    18.    
    19.     MsgBox ResultRange.Cells.Count 'just to show you that you now have a range
    20.    
    21.     'insert any other code
    22.    
    23.     Unload Me
    24.    
    25. ErrorHandler:
    26.     Exit Sub
    27.  
    28. End Sub
    29. Private Sub RefEdit1_BeforeDragOver(Cancel As Boolean, ByVal Data As MSForms.DataObject, ByVal x As stdole.OLE_XPOS_CONTAINER, ByVal y As stdole.OLE_YPOS_CONTAINER, ByVal DragState As MSForms.fmDragState, Effect As MSForms.fmDropEffect, ByVal Shift As Integer)
    30.  
    31. End Sub
    32. Private Sub RefEdit2_BeforeDragOver(Cancel As Boolean, ByVal Data As MSForms.DataObject, ByVal x As stdole.OLE_XPOS_CONTAINER, ByVal y As stdole.OLE_YPOS_CONTAINER, ByVal DragState As MSForms.fmDragState, Effect As MSForms.fmDropEffect, ByVal Shift As Integer)
    33.  
    34. End Sub
    35. Private Sub UserForm_Click()
    36.  
    37. End Sub

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: In Excel, select cells using textboxs

    CommandButton1_Click is the Click event for the CBtn I created. Does your button have the same name? If not you, you're not calling this sub.

    You shoud also check/change the names of the Refedit objects.

    Also, try commenting out the On Error.. list and see if you are getting any errors.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: In Excel, select cells using textboxs

    What do you mean by commenting out the ON Error...list? How do I do this?
    I triple checked the buttons and refedit boxs and names to make sure that they are the same.

  13. #13
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: In Excel, select cells using textboxs

    Here's my code again, but I've added a quote mark ' in front of the On error line. VB now thinks this is a comment and will not execute this line. This will stop the error trapping, so you shoud be able to see what line in the code is causeing the error.
    Always add error trapping/handling and comment it out will in develeopment but make sure you turn it on before publishing your work.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub CommandButton1_Click()
    4. Dim StartRange As Range
    5. Dim EndRange As Range
    6. Dim ResultRange As Range
    7.  
    8.    
    9. 'On Error GoTo ErrorHandler
    10.  
    11.     Set StartRange = Range(Me.RefEdit1.Value)
    12.     Set EndRange = Range(Me.RefEdit2.Value)
    13.    
    14.     If StartRange.Worksheet <> EndRange.Worksheet Then GoTo ErrorHandler
    15.    
    16.     If StartRange.Cells.Count <> 1 Or EndRange.Cells.Count <> 1 Then GoTo ErrorHandler
    17.    
    18.     Set ResultRange = Range(StartRange, EndRange)
    19.    
    20.     MsgBox ResultRange.Cells.Count 'just to show you that you now have a range
    21.    
    22.     'insert any other code
    23.    
    24.     Unload Me
    25.    
    26. ErrorHandler:
    27.     Exit Sub
    28. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: In Excel, select cells using textboxs

    OK, the debugger highlighted

    VB Code:
    1. If StartRange.Worksheet <> EndRange.Worksheet Then GoTo ErrorHandler

  15. #15
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: In Excel, select cells using textboxs

    Are you selecting cells on different worksheets?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  16. #16

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: In Excel, select cells using textboxs

    NO, a1 and g5, I try to type it, and if that doesn't work I select it.

  17. #17

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: In Excel, select cells using textboxs

    I took out the two lines with the "Then go to error handler lines" and it worked!

  18. #18
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] In Excel, select cells using textboxs

    OK, this might seem like a weird question, but did you add two new RefEdit controls or did you just rename your TextBox controls to RefEdit1 and RefEdit2?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  19. #19
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] In Excel, select cells using textboxs

    My bad

    DKenny.Brain = 404

    The line should read
    VB Code:
    1. If StartRange.Worksheet.Name <> EndRange.Worksheet.Name Then GoTo ErrorHandler
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width