VBA Code to fill group of data

NDMDRB

Board Regular
Joined
Jun 20, 2016
Messages
164
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a userform "PC" to manage the PCs usage and timing
I have 8 PCs in each room, sometimes each one (or some of them) used for different type/time... and sometimes all of them for the same type/time...

In the userform "PC" I have the following controls
1- Combo Box "cmb_Num" (List from 1 to 8, to determine the number of users)
2- Text Boxes "txt_Name1 / txt_Name2 / txt_Name3.... until 8" (to fill the user's names)
3- Combo Box "cmb_Type" (list of usage types: research, Data Entry, Printing...)
4- Text Box "txt_Time" (to determine how long the user will use the PC: 30 Min, 1 Hour...)
5- There are many other textboxes, but no need to add all of them
6- Command Button "cmd_Add"

For now, If I will book for one user only, I have the following code

Code:
Private Sub cmd_Add_Click()
Dim ws As Worksheet
Set ws = Sheet15

If cmb_Num = "1" Then
nr = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
ws.Cells(nr, "A") = CDbl(ws.Cells(Rows.Count, 1).End(xlUp).value + 1)
ws.Cells(nr, "B") = Me.txt_Name1
ws.Cells(nr, "C") = Me.cmb_Type
ws.Cells(nr, "C") = Me.txt_Time
'And the rest of values
End If
End Sub

So, the above code is works great for one user booking

But, let's say 8 users will use 8 PCs for the same type and the same time....
I know the hard way that will be so difficult and huge code, which is:

If cmb_Num = "1" Then
'Enter the above code

If cmb_Num = "2" Then
'Enter the above code
'And again the same code (but instead of [ws.Cells(nr, "B") = Me.txt_Name1] use this [ws.Cells(nr, "B") = Me.txt_Name2]

If cmb_Num = "3" Then
'Enter the above code
'And again the same code (but instead of [ws.Cells(nr, "B") = Me.txt_Name1] use this [ws.Cells(nr, "B") = Me.txt_Name2]
'And again the same code (but instead of [ws.Cells(nr, "B") = Me.txt_Name1] use this [ws.Cells(nr, "B") = Me.txt_Name3]

.... Until 8

Is there any easy way to manage this:
  • If there is one user, just fill the first empty row by the given data
  • If there are 2 users, fill the first empty row by the given data, and the next row with same data unless the name (use txt_Name2)
  • Same for 3 users, 4, 5...8

Thanks in advanced
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,214,626
Messages
6,120,602
Members
448,974
Latest member
ChristineC

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