VBA multiple textbox to cell value

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
624
Office Version
  1. 2016
Platform
  1. Windows
Just need a better way to input 112 textboxes from column D - DN

instead of using this below for 112 textboxes
VBA Code:
         Cells(Rg.Row, "D") = TextBox3
         Cells(Rg.Row, "F") = TextBox4
         Cells(Rg.Row, "E") = TextBox5
         Cells(Rg.Row, "G") = TextBox6

VBA Code:
Private Sub CommandButton1_Click()
Dim EmpID$, dDate$, Mth$, Yr$, Rg, Dd As Range, EmpCells&, i&, x&, Emp1Name As String, Emp2Name As String
EmpID = TextBox1.Value

If TextBox1.Value = "" Or TextBox2.Value = "" Then
   MsgBox "PLEASE ENTER THE EMPLOYEE NUMBER AND EFFECTIVE DATE", vbExclamation: Exit Sub
End If

If Not IsNumeric(EmpID) Then
MsgBox "INVALID EMPLOYEE NUMBER ENTER NUMBER WITHOUT THE E", vbExclamation: Exit Sub
End If

With Sheets("PERMANENT BID")
         Set Rg = .Columns(5).Find(EmpID, lookat:=xlWhole)
         If Rg Is Nothing Then
            MsgBox "EMPLOYEE NUMBER DOES NOT EXIST PLEASE TRY AGAIN", vbExclamation: Exit Sub
         Else
         Cells(Rg.Row, "D") = TextBox3
         Cells(Rg.Row, "F") = TextBox4
         Cells(Rg.Row, "E") = TextBox5
         Cells(Rg.Row, "G") = TextBox6
End Sub


is there a way to use a shortcut like below
VBA Code:
 'Set Rg = (.Cells(Rg.Row, "D").Resize(, 112))
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You can resize the range but you still can't load 100 textboxes all in one go.

Are your textboxes in a UserForm, or on a worksheet? Let's try this for a UserForm:

VBA Code:
   For i = 1 To 112
      Cells(Rg.Row, i+3) = Controls("TextBox" & i+2).Value
   Next i

If your textboxes are on a worksheet, are they Forms controls or ActiveX?
 
Upvote 0
Solution
They are in userforms. I will give that a try when I get back on the computer. Thanks
 
Upvote 0
In order to use a loop as suggested by 6String, your textboxes & cells must be in order. At the moment you are going columns D,F,E,G which are not in order & will cause problems.
 
Upvote 0
At the moment you are going columns D,F,E,G which are not in order
Oh, I missed that. Hopefully it is just a typo since the conceptual solution was to resize the cell range for all cells.
 
Upvote 0
Oh, I missed that. Hopefully it is just a typo since the conceptual solution was to resize the cell range for all cells.
Yes thank you but this was a typo on my end. The textbox and Cells are in order. Thanks
 
Upvote 0
You can resize the range but you still can't load 100 textboxes all in one go.

Are your textboxes in a UserForm, or on a worksheet? Let's try this for a UserForm:

VBA Code:
   For i = 1 To 112
      Cells(Rg.Row, i+3) = Controls("TextBox" & i+2).Value
   Next i

If your textboxes are on a worksheet, are they Forms controls or ActiveX?
this works great. However is there a way to skip any textbox that are blank. For example out of 112 textbox if 6 are numbers just run only "i" with text. Currently its running though all 112 and really slow
 
Upvote 0
Make these two changes, should speed up:

VBA Code:
   Application.ScreenUpdating = False
   For i = 1 To 112
      If Controls("TextBox" & i+2).Value <> "" Then
         Cells(Rg.Row, i+3) = Controls("TextBox" & i+2).Value
      End If
   Next i
   Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,684
Members
449,048
Latest member
81jamesacct

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