简体   繁体   中英

assigning value to dynamically built textbox name using excel vba

I am using monthview control as calander in separate userform called dateform .

I have multiple date fields on excel vba userform called Update_Entries_New hence I cannot directly reference date clicked on monthview calander like below (though direct referencing like below works perfectly fine).

Update_Entries_New.Invoice_date.Value = DateClicked

To solve the issue, I am generating Update_Entries_New.Invoice_date.Value dynamically to access monthview calander in all date fields in all userforms I have in project.

When I use dynamically created textbox name (picked from excel sheet cell) date clicked on monthview calander doesn't gets updated on textbox value from where calander got populated. any clue why this is happening.

my current code

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
    Dim test
       'dynamically referencing of "Update_Entries_New.Invoice_date.Value" picked from 'Sheets("Data").Range("m2").Value' doesn't update the value on userform textbox
       controlname = Sheets("Data").Range("m2").Value
       controlname = DateClicked

       'using textbox name directly works fine with below code
       Update_Entries_New.Invoice_date.Value = DateClicked

      Unload dateform
End Sub

i got this working using following code:

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
    test = Sheets("Data").Range("m1").Value
    CB = Sheets("Data").Range("m2").Value
    CBVal = DateClicked

    If test = "Update_Entries_New" Then
        For Each contr In Update_Entries_New.Controls
                If contr.Name = CB Then
                    contr.Value = CBVal
                End If
        Next
    End If

    Unload dateform
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM