|
-
Sep 13th, 2005, 05:49 PM
#1
Thread Starter
Member
[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
-
Sep 13th, 2005, 06:06 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 13th, 2005, 06:13 PM
#3
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:
Sub TestRange()
Dim MyRange As Range
Set MyRange = Application.InputBox(Prompt:="Test Range Input", Title:="Range Input", Type:=8)
Debug.Print MyRange.Address
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Sep 14th, 2005, 09:43 AM
#4
Thread Starter
Member
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.
-
Sep 14th, 2005, 10:00 AM
#5
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:
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 
-
Sep 14th, 2005, 10:22 AM
#6
Thread Starter
Member
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:
Private Sub cmdTestButton_Click()
Set MyRange = Range(RefEdit1, RefEdit2)
MyRange.Select
End Sub
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)
End Sub
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)
End Sub
Private Sub UserForm_Click()
End Sub
-
Sep 14th, 2005, 10:26 AM
#7
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 
-
Sep 14th, 2005, 10:37 AM
#8
Thread Starter
Member
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.
-
Sep 14th, 2005, 10:46 AM
#9
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:
Option Explicit
Private Sub CommandButton1_Click()
Dim StartRange As Range
Dim EndRange As Range
Dim ResultRange As Range
On Error GoTo ErrorHandler
Set StartRange = Range(Me.RefEdit1.Value)
Set EndRange = Range(Me.RefEdit2.Value)
If StartRange.Worksheet <> EndRange.Worksheet Then GoTo ErrorHandler
If StartRange.Cells.Count <> 1 Or EndRange.Cells.Count <> 1 Then GoTo ErrorHandler
Set ResultRange = Range(StartRange, EndRange)
MsgBox ResultRange.Cells.Count 'just to show you that you now have a range
'insert any other code
Unload Me
ErrorHandler:
Exit Sub
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Sep 14th, 2005, 11:08 AM
#10
Thread Starter
Member
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:
Option Explicit
Private Sub CommandButton1_Click()
Dim StartRange As Range
Dim EndRange As Range
Dim ResultRange As Range
On Error GoTo ErrorHandler
Set StartRange = Range(Me.RefEdit1.Value)
Set EndRange = Range(Me.RefEdit2.Value)
If StartRange.Worksheet <> EndRange.Worksheet Then GoTo ErrorHandler
If StartRange.Cells.Count <> 1 Or EndRange.Cells.Count <> 1 Then GoTo ErrorHandler
Set ResultRange = Range(StartRange, EndRange)
MsgBox ResultRange.Cells.Count 'just to show you that you now have a range
'insert any other code
Unload Me
ErrorHandler:
Exit Sub
End Sub
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)
End Sub
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)
End Sub
Private Sub UserForm_Click()
End Sub
-
Sep 14th, 2005, 11:10 AM
#11
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 
-
Sep 14th, 2005, 11:17 AM
#12
Thread Starter
Member
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.
-
Sep 14th, 2005, 11:20 AM
#13
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:
Option Explicit
Private Sub CommandButton1_Click()
Dim StartRange As Range
Dim EndRange As Range
Dim ResultRange As Range
'On Error GoTo ErrorHandler
Set StartRange = Range(Me.RefEdit1.Value)
Set EndRange = Range(Me.RefEdit2.Value)
If StartRange.Worksheet <> EndRange.Worksheet Then GoTo ErrorHandler
If StartRange.Cells.Count <> 1 Or EndRange.Cells.Count <> 1 Then GoTo ErrorHandler
Set ResultRange = Range(StartRange, EndRange)
MsgBox ResultRange.Cells.Count 'just to show you that you now have a range
'insert any other code
Unload Me
ErrorHandler:
Exit Sub
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Sep 14th, 2005, 11:25 AM
#14
Thread Starter
Member
Re: In Excel, select cells using textboxs
OK, the debugger highlighted
VB Code:
If StartRange.Worksheet <> EndRange.Worksheet Then GoTo ErrorHandler
-
Sep 14th, 2005, 11:26 AM
#15
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 
-
Sep 14th, 2005, 11:29 AM
#16
Thread Starter
Member
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.
-
Sep 14th, 2005, 11:31 AM
#17
Thread Starter
Member
Re: In Excel, select cells using textboxs
I took out the two lines with the "Then go to error handler lines" and it worked!
-
Sep 14th, 2005, 12:19 PM
#18
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 
-
Sep 14th, 2005, 12:50 PM
#19
Re: [RESOLVED] In Excel, select cells using textboxs
My bad
DKenny.Brain = 404
The line should read
VB Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|