kanadaaa
Active Member
- Joined
- Dec 29, 2019
- Messages
- 348
- Office Version
- 365
- Platform
- Windows
Hi, I have a question about the code below (not sure if it actually works at all because I'm a beginner):
I have a userform named UserForm1 and it has TextBox1, TextBox2, TextBox3 and CommandButton1 on it.
TextBox1 carries the name of a person, TextBox2 the value of an 8-digit date, and TextBox3 a numeral.
What I want to do is, when CommandButton1 is clicked, find the first empty row in Column A on sheet1 and put the value of TextBox1 into the relevant cell.
Then check if there's the same value in Column A on Sheet2, and if there is, select the cell, and with this position as an anchor, search for the first empty cell in the row and put the value of TextBox2 into it.
I also want this to repeat TextBox3.Value-divided-by-4000 times (e.g. if the value is 8000 it's 8000/4000 and thus twice).
Let's say I have THIS on sheet2 and the following values for the objects:
TextBox1.Value = "John"
TextBox2.Value = "12302018"
TextBox3.Value = "12000"
Then, I want C1 to E1 to be filled with TextBox2.Value because TextBox3.Value is 12000.
If TextBox3.Value were 8000, I'd only want C1 and D1 to be filled.
Is it possible to do this with VBA? I can't do it on my own, so any help would be appreciated.
VBA Code:
Private Sub CommandButton1_Click()
Dim emptyRow As Long
Dim rng As Range
'Make Sheet1 active
Sheet1.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Cells(emptyRow, 1).Value = TextBox1.Value
Set rng = Sheet2.Range("A").Find(TextBox1.Value, LookAt:=xlWhole)
If rng Is Nothing Then
MsgBox "No match"
Else
'// Search for the first empty cell in the row in which rng is located, and input the value of TextBox2 there
'// Then, repeat this event TextBox1.Value-divided-by-4000 times (e.g. if the value is 8000 repeat the event twice)
End If
Unload Me
End Sub
TextBox1 carries the name of a person, TextBox2 the value of an 8-digit date, and TextBox3 a numeral.
What I want to do is, when CommandButton1 is clicked, find the first empty row in Column A on sheet1 and put the value of TextBox1 into the relevant cell.
Then check if there's the same value in Column A on Sheet2, and if there is, select the cell, and with this position as an anchor, search for the first empty cell in the row and put the value of TextBox2 into it.
I also want this to repeat TextBox3.Value-divided-by-4000 times (e.g. if the value is 8000 it's 8000/4000 and thus twice).
Let's say I have THIS on sheet2 and the following values for the objects:
TextBox1.Value = "John"
TextBox2.Value = "12302018"
TextBox3.Value = "12000"
A | B | C | D | E |
John | 01212018 | |||
Mary |
Then, I want C1 to E1 to be filled with TextBox2.Value because TextBox3.Value is 12000.
If TextBox3.Value were 8000, I'd only want C1 and D1 to be filled.
Is it possible to do this with VBA? I can't do it on my own, so any help would be appreciated.