can you help me with code vba to increase the number of months in textboxes according to what is written in Textbox 6?

saftawy1

Board Regular
Joined
Oct 12, 2021
Messages
65
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Please help. I have a form with textbox 3 for the start date of the installments and another textbox 6 for the payment period for one installment per month, and the number of 15 textboxes from H1 to H15. It is required to increase the number of months according to what is written in Textbox 6
userfor22.JPG
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Assuming the textbox names are TextBox3, TextBox6, H1, H2, H3 through H15, then try this:

VBA Code:
Private Sub CommandButton1_Click()
  Dim iDate As Date, nDate As Date
  Dim n As Long, i As Long
  
  With TextBox3
    If .Value = "" Or Not IsDate(.Value) Then
      MsgBox "Enter valid date"
      .SetFocus
      Exit Sub
    End If
    iDate = CDate(.Value)
  End With
  With TextBox6
    If .Value = "" Or Not IsNumeric(.Value) Then
      MsgBox "Enter valid period"
      .SetFocus
      Exit Sub
    End If
    n = Val(.Value)
  End With
  
  For i = 1 To 15
    nDate = DateSerial(Year(iDate), Month(iDate) + (n * i), Day(iDate))
    Me.Controls("H" & i).Value = Format(nDate, "dd/mm/yyyy")
  Next
End Sub
 
Upvote 1
hello @DanteAmor pls
Is it possible to make the code start from the first empty textbox in the range from H1 to H15?
and ty
 
Upvote 0
Is it possible to make the code start from the first empty textbox in the range from H1 to H15?
You don't say if it starts putting dates or if it starts counting, try the following codes to see which one you need:

Option 1:
VBA Code:
Private Sub CommandButton1_Click()
  Dim iDate As Date, nDate As Date
  Dim n As Long, i As Long
  Dim bln As Boolean
  
  With TextBox3
    If .Value = "" Or Not IsDate(.Value) Then
      MsgBox "Enter valid date"
      .SetFocus
      Exit Sub
    End If
    iDate = CDate(.Value)
  End With
  With TextBox6
    If .Value = "" Or Not IsNumeric(.Value) Then
      MsgBox "Enter valid period"
      .SetFocus
      Exit Sub
    End If
    n = Val(.Value)
  End With
  
  For i = 1 To 15
    nDate = DateSerial(Year(iDate), Month(iDate) + (n * i), Day(iDate))
    If Me.Controls("H" & i).Value = "" Then
      bln = True
    End If
    If bln Then
      Me.Controls("H" & i).Value = Format(nDate, "dd/mm/yyyy")
    End If
  Next
End Sub

Option 2:
VBA Code:
Private Sub CommandButton1_Click()
  Dim iDate As Date, nDate As Date
  Dim n As Long, i As Long, j As Long
  Dim bln As Boolean
  
  With TextBox3
    If .Value = "" Or Not IsDate(.Value) Then
      MsgBox "Enter valid date"
      .SetFocus
      Exit Sub
    End If
    iDate = CDate(.Value)
  End With
  With TextBox6
    If .Value = "" Or Not IsNumeric(.Value) Then
      MsgBox "Enter valid period"
      .SetFocus
      Exit Sub
    End If
    n = Val(.Value)
  End With
  
  j = 0
  For i = 1 To 15
    If Me.Controls("H" & i).Value = "" Then
      bln = True
    End If
    If bln Then
      j = j + 1
      nDate = DateSerial(Year(iDate), Month(iDate) + (n * j), Day(iDate))
      Me.Controls("H" & i).Value = Format(nDate, "dd/mm/yyyy")
    End If
  Next
End Sub
 
Upvote 1
Solution
Unable to thank The second option is what is really required. Real, distinguished, and thankful for your response
 
Upvote 1

Forum statistics

Threads
1,216,107
Messages
6,128,866
Members
449,475
Latest member
Parik11

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