Link to home
Start Free TrialLog in
Avatar of bbarrene
bbarrene

asked on

Assigning Variables to Control TextBox and Column

I have a workbook that has similar worksheets for different categories (Part Type, Vendor, Machine, Station, etc.) to enter new entries.

I am trying to create a Sub that could be used by all of the worksheets when a "Submit" button is pressed within each worksheet. The "Submit" button is a Control Button (not a Form Button). When the button is pressed, the text in the Control Textbook of the worksheet, where the button was pressed, is copied to the next available cell within the specified column of the worksheet.

I am using different names for each textbox in each worksheet because I do not believe I can name them all the same Name. Also, the column to insert the text from the textbox varies as well. I get errors when I assign variables to the Textbox and Column. Does anyone know what I am doing wrong?
Public wks As Worksheet
Public CopyTextBox As TextBox
Public NextCell As Range
Public ColumnLetter As String

'Code in Module
Sub SubmitEntry()
     
    Set NextCell = wks.Cells(Cells.Rows.Count, _
                    Columns(ColumnLetter).Column).End(xlUp).Offset(1, 0)
         
    NextCell.Value = CopyTextBox.Value
    CopyTextBox.Value = ""
    
End Sub

'Code in Part_Type Worksheet
Private Sub SubmitPartTypeButton()
    Set wks = Worksheets("Part_Type")
    Set CopyTextBox = wks.SubmitPartTypeTextbox 'Control Textbox Name in Sheet
    Set ColumnLetter = "D"
    Call SubmitEntry
End Sub

'Code in Vendor Worksheet
Private Sub SubmitVendorButton()
    Set wks = Worksheets("Vendor")
    Set CopyTextBox = wks.SubmitVendorTextbox 'Control Textbox Name in Sheet
    Set ColumnLetter = "E"
    Call SubmitEntry
End Sub

'Code in Machine Worksheet
Private Sub SubmitMachineButton()
    Set wks = Worksheets("Machine")
    Set CopyTextBox = wks.SubmitMachineTextbox 'Control Textbox Name in Sheet
    Set ColumnLetter = "B"
    Call SubmitEntry
End Sub

Open in new window

Avatar of Ivo Stoykov
Ivo Stoykov
Flag of Bulgaria image

change
 wks.Cells(Cells.Rows.Count, _
                    Columns(ColumnLetter).Column).End(xlUp).Offset(1, 0)

to

Set NextCell = Range(wks.Cells(Cells.Rows.Count, _
                    Columns(ColumnLetter).Column).End(xlUp).Offset(1, 0).Address)

HTH

Ivo Stoykov

Hi

Unless you're mixing and matching Forms controls and Controls Toolbox controls, you don't have a Textbox, you have an MSForms.Textbox so i expect you get a type mismatch when you try to run this code.


Public CopyTextBox As MSForms.TextBox

Richard
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial