TextBox value to worksheet cell info

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,806
Office Version
  1. 2007
Platform
  1. Windows
Hi,

Code in use is shown below.

I am trying to place the value into my worksheet at A34 BUT the valueskeeps going to cell E1

VBA Code:
Private Sub TransferButton_Click()
    Dim i As Integer
    Dim ControlsArr As Variant, ctrl As Variant
    Dim x As Long
    For i = 1 To 2
       With Me.Controls("ComboBox" & i)
            If .ListIndex = -1 Then
                MsgBox "MUST SELECT ALL OPTIONS", vbCritical, "MONTH,YEAR & MILEAGE TRANSFER MESSAGE"
                .SetFocus
                Exit Sub
            End If
        End With
    Next i
    
    ControlsArr = Array(Me.ComboBox1, Me.ComboBox2, Me.TextBox1)
    
    With ThisWorkbook.Worksheets("MILEAGE")
        For i = 0 To UBound(ControlsArr)
         Select Case i
            Case 1, 2, 4
               .Cells(1, i + 3) = IIf(IsNumeric(ControlsArr(i)), Val(ControlsArr(i)), ControlsArr(i))
            Case Else
               .Cells(1, i + 2) = ControlsArr(i)
               ControlsArr(i).Text = ""
         End Select
    Next i
    
    End With
    [COLOR=rgb(184, 49, 47)]TextBox1.Value = Worksheets("MILEAGE").Cells("34,1")[/COLOR]
    ActiveWorkbook.Save
    Application.ScreenUpdating = True
    
    MsgBox "Month,Year & Mileage Have Been Updated", vbInformation, "SUCCESSFUL MILEAGE MESSAGE"
    Unload MYFMILEAGE
    
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
the value in the 'Cells' section shouldn't be a string:

VBA Code:
.Cells(34, 1)
 
Upvote 0
HI,
No matter what i enter 34, 1 22,5 8, 3 the value always goes into cell E1 ?
 
Upvote 0
Apologies, the line I mentioned doesn't write info to the cells it gets data from.

This code below is the code that writes information to the worksheet and it seems muddled. At least it's difficult to read.


The Ubound of ControlsArr wil always be 2 (0, 1 and 2 to represent the 3 controls added to the array) so why loop from 0 to the ubound?

Go through each line here and write in english (comment) what each one should do and I'm sure you'll see the issue

VBA Code:
 ControlsArr = Array(Me.ComboBox1, Me.ComboBox2, Me.TextBox1)
    With ThisWorkbook.Worksheets("MILEAGE")
        For i = 0 To UBound(ControlsArr)
         Select Case i
            Case 1, 2, 4
               .Cells(1, i + 3) = IIf(IsNumeric(ControlsArr(i)), Val(ControlsArr(i)), ControlsArr(i))
            Case Else
               .Cells(1, i + 2) = ControlsArr(i)
               ControlsArr(i).Text = ""
         End Select
    Next i
 
Upvote 0
Also none of those lines will write to A34 ever....

Is this line the wrong way round?
VBA Code:
    'this line writes the value FROM cells A34 to the text box. _
    Should it be Worksheets("MILEAGE").Cells("34,1") = TextBox1.Value?
    TextBox1.Value = Worksheets("MILEAGE").Cells("34,1")
 
Upvote 0
Hi,
The Combobox from userform to worksheet work fine.

I now need to get value from textbox1 on userform to worksheet cell A34
 
Upvote 0
HI,

Please use this code and forget the above.

VBA Code:
Private Sub TransferButton_Click()
    Dim i As Integer
    Dim ControlsArr As Variant, ctrl As Variant
    Dim x As Long
    For i = 1 To 2
       With Me.Controls("ComboBox" & i)
            If .ListIndex = -1 Then
                MsgBox "MUST SELECT BOTH OPTIONS", 48, "MONTH & YEAR TRANSFER MESSAGE"
                .SetFocus
                Exit Sub
            End If
        End With
    Next i
    
    ControlsArr = Array(Me.ComboBox1, Me.ComboBox2)
    
    With ThisWorkbook.Worksheets("MILEAGE")
        For i = 0 To UBound(ControlsArr)
         Select Case i
            Case 1, 2, 4
               .Cells(1, i + 3) = IIf(IsNumeric(ControlsArr(i)), Val(ControlsArr(i)), ControlsArr(i))
            Case Else
               .Cells(1, i + 2) = ControlsArr(i)
               ControlsArr(i).Text = ""
         End Select
             Next i
    End With

    ActiveWorkbook.Save
    Application.ScreenUpdating = True
    
    MsgBox "Month & Year Have Been Updated", vbInformation, "SUCCESSFUL MESSAGE MILEAGE"
    Unload MYFMILEAGE
    
End Sub


Now we need to take value from userform Textbox1 to worksheet cell A34
 
Upvote 0
So wouldn't just adding this line before the code ends do as you ask:

VBA Code:
ThisWorkbook.Worksheets("MILEAGE").Range("A34") = Me.Textbox1.Text
 
Upvote 0
Show me where in my code to insert it as it didn’t work for me
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

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