VBA to pass textbox value to two ranges

kim_lerrie

New Member
Joined
May 25, 2022
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
Hello. I'm a new member here and currently working on a system for school project. The system can be accessed through userform. It is also my first time working with Excel VBA. Please excuse me if my code is not great.

Worksheets:
Sheet1 (2021)
Sheet2 (2022)
Sheet3 (Receipt)
Sheet4 (Database)

The problem I encountered:
1. For the COUNT COMMAND BUTTON, the code for counting the customers when the date (from sheet1 and sheet2) is typed in DateTextBox (with format mm/dd/yyyy) and displayed in CountTextBox is running smoothly. However, I want the value displayed in CountTextBox to be passed on sheet4. I add some code and successfully did it for the year 2021 but when I copied this code for year 2022, the value for year 2022 continued in same column with the value for year 2021.

Any assistance you can provide would be greatly appreciated.

Image named:
Capture is the successful code for year 2021.
Capture1 is the code I copied for year 2022 but passed the value on same column (result should be in column G)

Here is my whole code for the COUNT COMMAND BUTTON:
Private Sub CountButton_Click()
Dim myCell As range
Dim myRange As range
Dim count As Integer

Set myRange = Worksheets("2021").range("A1:J2188")
total = 0

For Each myCell In myRange
If myCell.Value = Me.DateTextBox.Value Then
count = count + 1
End If
Next myCell

Set myRange = Worksheets("2022").range("A1:J10000")
total = 0

For Each myCell In myRange
If myCell.Value = Me.DateTextBox.Value Then
count = count + 1
End If
Next myCell

Me.CountTextBox.Value = count

Dim SearchTerm As String
Dim SearchColumn As String
Dim Sheet As Worksheet

Set Sheet = ThisWorkbook.Sheets("Database")

Dim emptyRow As Long

ThisWorkbook.Sheets("Database").Activate

emptyRow = WorksheetFunction.CountA(range("B:B"))
If Me.DateTextBox.Value <> "" Then
SearchTerm = Me.DateTextBox.Value
SearchColumn = "2021"
Sheet.range("B" & emptyRow + 1).Value = Me.CountTextBox.Value
End If

ThisWorkbook.Sheets("Database").Activate

emptyRow = WorksheetFunction.CountA(range("F:F"))
If Me.DateTextBox.Value <> "" Then
SearchTerm = Me.DateTextBox.Value
SearchColumn = "2022"
Sheet.range("G" & emptyRow + 1).Value = Me.CountTextBox.Value
End If
End Sub
 

Attachments

  • Capture.JPG
    Capture.JPG
    40.8 KB · Views: 8
  • Capture1.JPG
    Capture1.JPG
    23.7 KB · Views: 7

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Welcome to the MrExcel Message Board! :)

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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