Transfer Textbox to Sheet based on Value of 2 Other Textboxes

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

Trying to Transfer a Textbox to a Sheet based on its Value and the Value of 2 Other Textboxes (1 for Row and 2 for Column)

I have 3 Textboxes Textbox1 and Textbox2 and Textbox3

Textbox1 contains a Value to be Searched in Column D
Textbox2 Contains a Value which will be one of the Headings in Row 1
Textbox3 is the Value to be transferred

So If Textbox 1 is Cat and the Value in Textbox 2 is Colour and the Value in Textbox3 is Yellow
I want Yellow to go into G4 (G is where the Colour Heading is and 4 is the Row where Cat is

ABCDEFGHIJK
1​
AnimalCountryVegetableColour
2​
AardvarkAngolaAppleAmber
3​
BearBarbadosBananaBlue
4​
CatChileCherryCerise
5​
DogDenmarkDateDark Blue
6​
ElephantEnglandEggplantEmerald Green
7​
FoxFranceFigFuschia
8​
GoatGreeceGrapeGold
9​
10​
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
See if this works for you. Code to be pasted in the module of the worksheet involved (right click on worksheet tab, click View Code to open the VBE, paste code in right hand pane and close VBE).

EDIT:
If your textboxes are on a userform code can be pasted in its code behind module. Note that the code then will target te active worksheet.

VBA Code:
Option Explicit

Private Type TLocals
    AnimalRow       As Long
    CategoryColumn  As Long
End Type
Private this As TLocals

Private Sub TextBox1_Change()
    ' Animal in column D
    Dim d As Range, r As Range
    this.AnimalRow = 0
    Set d = Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row)
    Set r = d.Find(Trim$(Me.TextBox1.Value), , , xlWhole, , , False)
    If Not r Is Nothing Then
        this.AnimalRow = r.Row
    End If
    TextBox3_Change     ' <<<< remove this line if a transfer should only take place when box 3 is changed.
End Sub

Private Sub TextBox2_Change()
    ' category in row 1
    Dim d As Range, r As Range
    this.CategoryColumn = 0
    Set d = Range(Cells(1, "E"), Cells(1, Columns.Count).End(xlToLeft))
    Set r = d.Find(Trim$(Me.TextBox2.Value), , , xlWhole, , , False)
    If Not r Is Nothing Then
        this.CategoryColumn = r.Column
    End If
    TextBox3_Change     ' <<<< remove this line if a transfer should only take place when box 3 is changed.
End Sub

Private Sub TextBox3_Change()
    ' value to be copied
    If this.AnimalRow <> 0 And this.CategoryColumn <> 0 Then
        Cells(this.AnimalRow, this.CategoryColumn).Value = TextBox3.Value
    End If
End Sub
 
Last edited:
Upvote 0
Thanks. I'll have a look later

Forgot to mention it's on a userform and wanted the transfer to happen when a command button is pressed
 
Upvote 0
Also textbox 1 and 2 will already filled in based on users previous selections within the form

Effectively on command button I need

VBA Code:
Worksheets("Sheet2").range("G4") = .Value = TextBox3.Value

Just need to get the G4 based on what's is in textbox1 for the column(g) and what's in textbox2 for the row (4) as in example in post 1

Textbox 1 is Cat, Textbox 2 is Colour and the Value in Textbox3 is Yellow so G4 will become yellow

Apologies for any confusion
 
Upvote 0
No confusion on this side, just wanted to be clear.

Effectively on command button I need

VBA Code:
Worksheets("Sheet2").range("G4") = .Value = TextBox3.Value
The code above wil give you a TRUE or a FALSE in G4, provided there's a preceding With statement to qualify the .Value (otherwise it will error ...), so I followed your explanation.
Made a few amendments to suit use on a userform. Please note the procedure names to be renamed.

VBA Code:
Option Explicit

Private Type TLocals
    TargetSheet     As Worksheet
    AnimalRow       As Long
    CategoryColumn  As Long
End Type
Private this As TLocals

Private Sub TextBox1_Change()
    ' Animal in column D
    Dim d As Range, r As Range
    If Not this.TargetSheet Is Nothing Then
        this.AnimalRow = 0
        Set d = this.TargetSheet.Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row)
        Set r = d.Find(Trim$(Me.TextBox1.Value), , , xlWhole, , , False)
        If Not r Is Nothing Then
            this.AnimalRow = r.Row
        End If
    End If
End Sub

Private Sub TextBox2_Change()
    ' category in row 1
    Dim d As Range, r As Range
    If Not this.TargetSheet Is Nothing Then
        this.CategoryColumn = 0
        Set d = this.TargetSheet.Range(Cells(1, "E"), Cells(1, Columns.Count).End(xlToLeft))
        Set r = d.Find(Trim$(Me.TextBox2.Value), , , xlWhole, , , False)
        If Not r Is Nothing Then
            this.CategoryColumn = r.Column
        End If
    End If
End Sub

Private Sub Commandbutton_Click()                               ' <<<< rename to suit
    ' value to be copied
    If Not this.TargetSheet Is Nothing Then
        If this.AnimalRow <> 0 And this.CategoryColumn <> 0 Then
            this.TargetSheet.Cells(this.AnimalRow, this.CategoryColumn).Value = Me.TextBox3.Value
        End If
    End If
End Sub

Private Sub Userform_Initialize()                               ' <<<< rename to suit
    Set this.TargetSheet = ThisWorkbook.Worksheets("Sheet2")    ' <<<< change sheet to suit
End Sub
 
Upvote 0
Worksheets("Sheet2").range("G4") = .Value = TextBox3.Value was a typo should have been

Worksheets("Sheet2").range("G4").Value
VBA Code:
 = TextBox3.Value

Is it possible to get all the code in the command button using value of textbox1 as the column and textbox2 as the row

Thanks
 
Upvote 0
Is it possible to get all the code in the command button using value of textbox1 as the column and textbox2 as the row
I'm not sure what you mean by this, but if you would mean "Is it possible to put all the code in the command button's click event handler" then the answer is yes. But before we go any further with this, does the following still apply?
So If Textbox 1 is Cat and the Value in Textbox 2 is Colour and the Value in Textbox3 is Yellow
I want Yellow to go into G4 (G is where the Colour Heading is and 4 is the Row where Cat is
 
Upvote 0
Yes the above applies.

When a command button is clicked the value in textbox3 will go into a cell on Sheet2

The column is based on value in textbox1 and row based on textbox2 which will already be filled in
 
Upvote 0
Ok, how about ...
VBA Code:
Option Explicit

Private Type TLocals
    TargetSheet     As Worksheet
    AnimalRow       As Long
    CategoryColumn  As Long
End Type
Private this As TLocals

Private Sub Userform_Initialize()                               ' <<<< rename to suit
    Set this.TargetSheet = ThisWorkbook.Worksheets("Sheet2")    ' <<<< change sheet to suit
End Sub

Private Sub Commandbutton_Click()                               ' <<<< rename to suit
    Dim d As Range, r As Range
    If Not this.TargetSheet Is Nothing Then
        With this.TargetSheet
            this.AnimalRow = 0
            this.CategoryColumn = 0
            ' Animal in column D (Tbx 1)
            Set d = .Range("D2:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
            Set r = d.Find(Trim$(Me.TextBox1.Value), , , xlWhole, , , False)
            If Not r Is Nothing Then
                this.AnimalRow = r.Row
            End If
            ' Category in row 1 (Tbx 2)
            Set r = Nothing
            Set d = .Range(.Cells(1, "E"), .Cells(1, .Columns.Count).End(xlToLeft))
            Set r = d.Find(Trim$(Me.TextBox2.Value), , , xlWhole, , , False)
            If Not r Is Nothing Then
                this.CategoryColumn = r.Column
            End If
            ' copy to sheet (Tbx 3)
            If this.AnimalRow <> 0 And this.CategoryColumn <> 0 Then
                .Cells(this.AnimalRow, this.CategoryColumn).Value = Me.TextBox3.Value
            End If
        End With
    End If
End Sub
 
Upvote 0
Solution
Thanks, all versions above worked and the last one as needed via command button
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,101
Members
449,205
Latest member
ralemanygarcia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top