简体   繁体   English

从Excel工作表中读取一列并显示在文本框VBA上

[英]read a column from a excel sheet & display on textbox vba

Read all the rows for one column & display on textbox(s) which are created at run time.I am turning over to seek you assistance for below after several attempts which didn't help. 阅读一列的所有行并在运行时创建的文本框上显示。经过几次无济于事的尝试,我正在下面寻求您的帮助。

Name

ABC

AB

ABCEF

GHFD

I want all the rows from excel sheet to be shown in textbox just as they appear in sheet for a particular column.I am creating 4 textbox(s) that represent columns & 11 textbox(s) that represent rows. 我希望excel表格中的所有行都显示在文本框中,就像它们出现在特定列的表格中一样。我正在创建代表列的4个文本框和代表行的11个文本框。 For the first column, I want the data to be displayed as it is in sheet. 对于第一列,我希望数据按表格形式显示。 I am successful in creating text box as per my requirement but not able to display data as per needs. 我可以根据需要成功创建文本框,但无法根据需要显示数据。 Thanks a ton for help 多谢帮助

Public Sub UserForm_Initialize()

Set sh = ThisWorkbook.Sheets("Testing")

sh.Range("F21").Activate

With sh
    fatalcount = WorksheetFunction.CountIf(Range("F:F"), "Fatal")
    Majorcount = WorksheetFunction.CountIf(Range("F:F"), "Major")
    Minorcount = WorksheetFunction.CountIf(Range("F:F"), "Minor")
End With

For jrow = 1 To 11
For i = 0 To 4
    Set txtB1 = WtmsFrm.Controls.Add("Forms.TextBox.1")
    With txtB1
    .Name = "chkDemo" & i
    .Height = 20
    .Width = 5 + 50 + 5
    .Left = 10 + 50 * i + 2
    .Top = 15 * jrow + 10
    .ControlTipText = "Type of Bug"
    End With
 Next i
Next jrow

For Each tbox In  Frm.Controls
   ' For counter = 2 To 11

    If tbox.Name = "chkDemo" Then

        tbox.Value = Sheets("sheet1").Cells(counter, 2).Value  ' failing code
        tbox.ControlTipText = "Name"

    ElseIf tbox.Name = "chkDemo1" Then
        tbox.Value = 1

    ElseIf tbox.Name = "chkDemo2" Then
        tbox.Value = 2
    ElseIf tbox.Name = "chkDemo3" Then
        tbox.Value = 3
     ElseIf tbox.Name = "chkDemo4" Then
        tbox.Value = 4
    End If
   ' Next counter

Next


' Initialise the followings
 Frm.txtFatal.Value = fatalcount

 Frm.txtMajor.Value = Majorcount

 Frm.txtMinor.Value = Minorcount

 Frm.txtTotoal.Value = fatalcount + Majorcount + Minorcount

End Sub

I don't see you create a textbox named "chkDemo" . 我看不到您创建一个名为"chkDemo"的文本框。 They all have a number appended. 它们都有一个附加的数字。 As you take tbox from all controls, there might be a control on your sheet somewhere named "chkDemo" but which is not a textbox. 当您从所有控件中获取tbox ,工作表上可能有一个名为"chkDemo"控件,但该控件不是文本框。 Neither do I see counter declared or initialized. 我没有看到counter声明或初始化。 That might also let the code fail. 这也可能会使代码失败。

Note also that with .Name = "chkDemo" & i you give the same name to textboxes because you are also in a For jrow loop. 还要注意,使用.Name = "chkDemo" & i您为文本框指定了相同的名称,因为您也处于For jrow循环中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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