+ Reply to Thread
Results 1 to 7 of 7

How to use Text Boxes values/text as Variabels in VBA code of UserForms

  1. #1
    Registered User
    Join Date
    07-30-2015
    Location
    Porto, Portugal
    MS-Off Ver
    2010
    Posts
    12

    Red face How to use Text Boxes values/text as Variabels in VBA code of UserForms

    Hi, everyone!

    I'm struggling to create a UserForm to eliminate rows based on the content of cells that match the input of that UserForm,
    and another UserForm to add rows with specific cells automatically filled with the information that is inputed in the UserForm.

    For example, I call the UserForm to Eliminate rows, and it asks as input the content of the cells of the rows I wish to eliminate.
    If I write "Bob", it should eliminate all rows that have cells with the content "Bob".

    For example, I call the UserForm to Add rows, and it askes me the name, age, weight and row number as input.
    If I write "Alice", "20", "60", "2" it should add in position 2 a row with the contents "Alice", "20", "60" in certain cells.

    I believe I could just record a macro adding and removing rows and copy paste it to the code of the TextBoxes of the UserForm
    but I need to use the TextBoxes values/Text as Variables in order to make this usable.

    Any help will be apreciated :P

    Thank you for your time!

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How to use Text Boxes values/text as Variabels in VBA code of UserForms

    Can you attach a sample with your UserForm and any code you have?
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Registered User
    Join Date
    07-30-2015
    Location
    Porto, Portugal
    MS-Off Ver
    2010
    Posts
    12

    Re: How to use Text Boxes values/text as Variabels in VBA code of UserForms

    I wrote this code to adress the UserForm using a button that has it assigned:

    "Sub EliminateEquip()
    Dim smessage As String
    smessage = "You sure you want to eliminate equipment?"
    If MsgBox(smessage, vbQuestion + vbYesNo, "Confirm Delete") = vbYes Then
    UserForm1.Show
    End If
    End Sub"

    And the next code is the code for the UserForm, which has one text box, TextBox1, labeled has "equipment name":

    "Private Sub CommandButton1_Click()

    ' Here should be the code lines that looped through the Workbook sheets
    ' removing all the rows that contain cells with the content of the TextBox1

    MsgBox ("The Equipment " & x & " was removed.")
    End Sub"

    This is as far as I went...

  4. #4
    Registered User
    Join Date
    07-30-2015
    Location
    Porto, Portugal
    MS-Off Ver
    2010
    Posts
    12

    Re: How to use Text Boxes values/text as Variabels in VBA code of UserForms

    The x in the second code is supposed to be TextBox1.Value or TextBox1.Text

  5. #5
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How to use Text Boxes values/text as Variabels in VBA code of UserForms

    The 1st part of your code can be downsized to
    Please Login or Register  to view this content.
    Still working on the 2nd code.

  6. #6
    Registered User
    Join Date
    07-30-2015
    Location
    Porto, Portugal
    MS-Off Ver
    2010
    Posts
    12

    Re: How to use Text Boxes values/text as Variabels in VBA code of UserForms

    Thanks, didn't think of that!

    For what concerns the 2nd code, I already managed to make it delete all the pretended rows in the first sheet,
    but by the time it ends the first sheet, the cells.find function returns an error window and the programm is interrupted...

    This is what I've done so far:

    "Private Sub CommandButton1_Click()
    ' This code deletes all the rows in the first sheet that have cells with the content equal to TextBox1.Text
    ' Declare Current as a worksheet object variable.
    Dim Current As Worksheet
    ' Loop through all of the worksheets in the active workbook.
    For Each Current In Worksheets

    Current.Select

    While Cells.Find(What:=TextBox1.Text, After:=Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Select = True
    Range(Selection, Selection.End(xlToRight)).EntireRow.Delete
    Wend
    Next
    MsgBox ("The Equipment " & TextBox1.Value & " was removed.")
    End Sub
    Private Sub CommandButton2_Click()
    Unload Me
    End Sub

  7. #7
    Registered User
    Join Date
    07-30-2015
    Location
    Porto, Portugal
    MS-Off Ver
    2010
    Posts
    12

    Re: How to use Text Boxes values/text as Variabels in VBA code of UserForms

    Private Sub CommandButton1_Click()
    ' esta função já elimina a primeira linha que encontra com TextBox1.Text
    ' e fá-lo para cada página do Workbook
    ' Declare Current as a worksheet object variable.
    Dim Current As Worksheet
    ' Loop through all of the worksheets in the active workbook.
    For Each Current In Worksheets

    Current.Select

    While Cells.Find(What:=TextBox1.Text, After:=Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows,
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Select = True
    Range(Selection, Selection.End(xlToRight)).EntireRow.Delete
    Wend
    Next
    MsgBox ("The Equipment " & TextBox1.Value & " was removed.")
    End Sub
    Private Sub CommandButton2_Click()
    Unload Me
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help with code for userform text boxes, combo boxes and excel
    By innerise in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-20-2014, 09:07 AM
  2. I need a code for presence check to check multiple text boxes and combo boxes
    By Lee_wwfc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2013, 01:53 PM
  3. [SOLVED] Text Boxes: Insert/Text Box vs. Developer Text Boxes
    By snapfade in forum Excel General
    Replies: 5
    Last Post: 10-09-2012, 01:45 AM
  4. [SOLVED] Need code to add values from 2 text boxes and place result in a label.
    By egavasrg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2012, 08:12 PM
  5. UserForms: Creating variable number of text boxes
    By seckela in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-11-2012, 05:46 PM
  6. Cycle throught Text Boxes in UserForms
    By tom.hogan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-16-2011, 11:44 AM
  7. Replies: 2
    Last Post: 02-19-2010, 03:55 PM
  8. retrieving values from text boxes
    By medicenpringles in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-11-2005, 04:03 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1