I’m really hoping someone may be able to help me with the following VBA problem:
I have a data entry Userform with 21 textboxes which I’m using to collect the user’s details.
In the first instance I would like the values (data) from all of the textboxes to be transferred to my workbook ‘database’ (Sheet6).
Note. I have managed to find some code on the internet to do this and it works fine on its own.
However - I also need to get selected data (8 to be precise) from the Userform textboxes and transfer those to a second worksheet (Sheet5).
It is at this point that I’m stuck as when I try to modify the code to add the selected data to the second sheet it doesn’t work. To add to my problem the data being transferred to Sheet5 has to start in column: E skip 3 columns (F – H) and then continue from columns: I to O
I’d be grateful if anyone can help out on this one as I’ve tried various ways to modify the code without any success. Unfortunately I’m a VBA novice so I’m hoping it’s just something simple that’s missing and someone’s got the answer.
As usual thank you in advance
Note: the 21 Textboxes are named: “Reg1” to “Reg21”.
The data I need to transfer to Sheet5 (“2nd Sheet”) is:
Reg17 to (Sheet5) column E
Reg4 to (Sheet5) column I
Reg1 to (Sheet5) column J
Reg12 to (Sheet5) column K
Reg13 to (Sheet5) column L
Reg14 to (Sheet5) column M
Reg9 to (Sheet5) column N
Reg16 to (Sheet5) column 0
The code below (I have tried to modified for the two worksheets) doesn’t work:
Private Sub cmdAdd_Click()
Dim nextrow As Range
Dim x As Integer
'error handler
On Error GoTo errHandler:
'set the next row in the database
Set nextrow = Sheet6.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)
'check for values in the first 4 controls
For x = 1 To 4
If Me.Controls("Reg" & x).Value = "" Then
MsgBox "You must add all data"
Exit Sub
End If
Next
'check for duplicate payroll numbers
If WorksheetFunction.CountIf(Sheet6.Range("F:F"), Me.Reg4.Value) > 0 Then
MsgBox "This staff member already exists"
Exit Sub
End If
''''''''''''''''''''''''''''''' below example of my attempt to modify/adapt the code to transfer the selected data to second worksheet (Sheet5) ''''''''''''''''''''
'number of controls to loop through
cNum = 8
'add the data to the database
For x = 1 To cNum
If x = Me.Reg17.Value Then
nextrow = Me.Controls("Reg" & x).Value
Set nextrow = nextrow.Offset(0, 1)
End If
If x = Me.Reg4.Value Then
nextrow = Me.Controls("Reg" & x).Value
Set nextrow = nextrow.Offset(0, 4)
End If
Next
'clear the controls
For x = 1 To cNum
Me.Controls("Reg" & x).Value = ""
Next
'sort the database
Sortit
'error block
On Error GoTo 0
Exit Sub
errHandler::
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
& Err.Number & vbCrLf & Err.Description & vbCrLf & _
"Please notify the administrator"
End Sub
Thank again in advance
I have a data entry Userform with 21 textboxes which I’m using to collect the user’s details.
In the first instance I would like the values (data) from all of the textboxes to be transferred to my workbook ‘database’ (Sheet6).
Note. I have managed to find some code on the internet to do this and it works fine on its own.
However - I also need to get selected data (8 to be precise) from the Userform textboxes and transfer those to a second worksheet (Sheet5).
It is at this point that I’m stuck as when I try to modify the code to add the selected data to the second sheet it doesn’t work. To add to my problem the data being transferred to Sheet5 has to start in column: E skip 3 columns (F – H) and then continue from columns: I to O
I’d be grateful if anyone can help out on this one as I’ve tried various ways to modify the code without any success. Unfortunately I’m a VBA novice so I’m hoping it’s just something simple that’s missing and someone’s got the answer.
As usual thank you in advance
Note: the 21 Textboxes are named: “Reg1” to “Reg21”.
The data I need to transfer to Sheet5 (“2nd Sheet”) is:
Reg17 to (Sheet5) column E
Reg4 to (Sheet5) column I
Reg1 to (Sheet5) column J
Reg12 to (Sheet5) column K
Reg13 to (Sheet5) column L
Reg14 to (Sheet5) column M
Reg9 to (Sheet5) column N
Reg16 to (Sheet5) column 0
The code below (I have tried to modified for the two worksheets) doesn’t work:
Private Sub cmdAdd_Click()
Dim nextrow As Range
Dim x As Integer
'error handler
On Error GoTo errHandler:
'set the next row in the database
Set nextrow = Sheet6.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)
'check for values in the first 4 controls
For x = 1 To 4
If Me.Controls("Reg" & x).Value = "" Then
MsgBox "You must add all data"
Exit Sub
End If
Next
'check for duplicate payroll numbers
If WorksheetFunction.CountIf(Sheet6.Range("F:F"), Me.Reg4.Value) > 0 Then
MsgBox "This staff member already exists"
Exit Sub
End If
''''''''''''''''''''''''''''''' below example of my attempt to modify/adapt the code to transfer the selected data to second worksheet (Sheet5) ''''''''''''''''''''
'number of controls to loop through
cNum = 8
'add the data to the database
For x = 1 To cNum
If x = Me.Reg17.Value Then
nextrow = Me.Controls("Reg" & x).Value
Set nextrow = nextrow.Offset(0, 1)
End If
If x = Me.Reg4.Value Then
nextrow = Me.Controls("Reg" & x).Value
Set nextrow = nextrow.Offset(0, 4)
End If
Next
'clear the controls
For x = 1 To cNum
Me.Controls("Reg" & x).Value = ""
Next
'sort the database
Sortit
'error block
On Error GoTo 0
Exit Sub
errHandler::
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
& Err.Number & vbCrLf & Err.Description & vbCrLf & _
"Please notify the administrator"
End Sub
Thank again in advance