Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

populate QTY for ID in DATE column by using userform

0

Hello

I have project to sum or subtract QTY  based on selected optionbuttons  after match selected ID from combobox and fill QTY in textbox1  . it will change QTY in column D , but  I want show change QTY in column contains DATE (today) based on prevoius column .

for  instance first  will show the result in column E  after sum or subtract from column D  and when date (today) to becom 25/01/2024 then will sum or subtract from column 24/01/2024  and show in column 25/01/2024 and  so  on every  day will show in column contains DATE  after calculation from precede column .

every time I will add date (today)in new column .

thanks

Answer
Discuss

Answers

0
Selected Answer

Hi again Malkal

In the attached revised file, I've used your code but modified as in bold.

Essentially the ID is found then it looks for the last value in that row (which may not be in yesterday's column e.g. if a daily operation was missed) and adds or subtracts the UserForm quantity then puts that result in the column for today:

Private Sub CommandButton1_Click()
  Dim f As Range
  Dim LstCol As Long, Tdy As Range

  'Validations
  With ComboBox1
    If .Value = "" Then
      MsgBox "Enter ID"
      .SetFocus
      Exit Sub
    End If
    Set f = Range("C:C").Find(.Value, , xlValues, xlWhole, , , False)
    If f Is Nothing Then
      MsgBox "ID does not exists"
      .SetFocus
      Exit Sub
    End If
  End With
  With TextBox1
    If .Value = "" Or Not IsNumeric(.Value) Then
      MsgBox "Enter QTY"
      .SetFocus
      Exit Sub
    End If
  End With

  'find last value in ID row
  LstCol = Cells(f.Row, Columns.Count).End(xlToLeft).Column

  'find today's date in row 1
  Set Tdy = Rows(1).Find(Date, , xlValues, xlWhole, , , False)
    If f Is Nothing Then
      MsgBox "Needs a column for today!"
      Exit Sub
    End If

  ' quit if last value is in today's column
  If LstCol = Tdy.Column Then
    MsgBox "Quantity already exits for today"
    Exit Sub
  End If

  'Sum or subtract
  With Cells(f.Row, Tdy.Column)
    Select Case True
      Case OptionButton1 Or OptionButton3
        ' add qty to last value + put today's column
        .Value = Cells(f.Row, LstCol).Value + Val(TextBox1.Value)
      Case OptionButton2 Or OptionButton4
      ' or subtract
        .Value = Cells(f.Row, LstCol).Value - Val(TextBox1.Value)
      Case Else
        MsgBox "No option was selected"
    End Select
  End With

  ' empty ID and qty
  ComboBox1.Value = ""
  TextBox1.Value = ""

End Sub

You might want to allow the value in the Today column to be adjusted by the UserForm but I assumed it was a one-off operation per day.

Hope this is what you meant. If so, please remember to mark this Answer as Selected. 

Discuss

Discussion

perfect , thank you so much.
Malkal (rep: 22) Jan 29, '24 at 7:00 am
Glad that helped. Thanks for selecting my Answer, Malkal.
John_Ru (rep: 6142) Jan 29, '24 at 7:02 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login