خرید بک لینک

Vote count: 0

I came across this while answering this question How to access textBox placed on sheet in VBA module?

So if I add a TextBox from Developer tab on a worksheet and try to access that control in VBA using a Worksheet type variable, VBA fails to discover that control. But If I use the worksheet's CodeName it works fine. Also if I use a Variant type to assign the worksheet in it, the code works fine.

Just trying to understand if I am doing something wrong or it's a bug.


If you use a Worksheet type variable then Excel fails to discover the control on that sheet inVBA. So if you declare your sheet holding variable as Object / Variant , the code will work fine.

Other alteative is to directly use the Worksheet's CodeName, so if you set the Worksheet's name as wksDBSheet in the VBA IDE's property grid and use that in your code, it will discover the TextBox

Sub test()
    Dim objDBsheet As Object 'As Worksheet  // Making the 0bjDBSheet type as Object or Variant
                                           '// Allows the discovery of the TextBox on the sheet.
                                           '// Most Likely its a bug.
    Dim objSQL As Range

    Dim tbSQL As MSForms.TextBox
    Set objDBsheet = Application.Worksheets("Database Info.")
    Set tbSQL = objDBsheet.tbSQL
    tbSQL.Text = "Bug"


    '/  Other Alteative is to directly use the CddeName of the sheet.
    Set tbSQL = wksDBsheet.tbSQL
    tbSQL.Text = "Code Name used"

End Sub

asked 31 secs ago

- - , .
.

برچسب: نویسنده: استخدام کار تاريخ: جمعه 25 تير 1395 ساعت: 3:13

صفحه بندی