而不是行号使用第一个单元格编号(Excel VBA)

时间:2016-12-09 09:36:51

标签: excel vba excel-vba

我制作了一个自动添加Textboxes&标签取决于工作表中有多少行/列。

现在我遇到了一个问题,例如,如果我的“Steve”的数字如 4 ,它就不会显示单元格中的确切数字而是会显示行号。

screenhoot1

Excel数据表截图

screenshot2

所以我想要的是修复数字:标签和也使TextBox 现在如果我输入1它将显示行号1的数据我希望能够使用“Steve”编写“4”而不是“2”这是行号。

以下是Userform代码:

Private Sub CommandButton1_Click()
    'Exit Form
    Unload Me
End Sub

Private Sub CommandButton2_Click()
    'Edit
    k = ScrollBar1.Value
    lcol = Sheet1.Range("DX2").End(xlToLeft).Column

    For j = 1 To lcol
        Sheet1.Cells(k + 1, j) = Me.Controls("textbox" & j)
    Next j

End Sub

Private Sub CommandButton3_Click()
    'Delete
    k = ScrollBar1.Value
    Sheet1.Cells(k + 1, 1).EntireRow.Delete
End Sub

Private Sub ScrollBar1_Change()

Dim Rng As Range

    lcol = Sheet1.Range("DX2").End(xlToLeft).Column
    k = ScrollBar1.Value
    k2 = TextBox1000.Value

    label1000.Caption = "Number : " & k

    If k <> 0 And k2 <> 0 Then
        TextBox1000.Value = k
        For j = 1 To lcol
            Me.Controls("textbox" & j).Text = Sheet1.Cells(k, j).Offset(1, 0).Value
        Next j
    End If

End Sub

Private Sub TextBox1000_Change()

    lcol = Sheet1.Range("DX2").End(xlToLeft).Column
    k = ScrollBar1.Value
    k2 = TextBox1000.Value
    label1000.Caption = "Number : " & k2

    If k2 <> "" Then
        ScrollBar1.Value = k2
        For j = 1 To lcol
            Me.Controls("textbox" & j).Text = Sheet1.Cells(k2, j).Offset(1, 0).Value
        Next j
    End If

End Sub

Private Sub UserForm_Initialize()

Dim myLabel As Control
Dim txtbox As Control

    k = ScrollBar1.Value
    label1000.Caption = "Number : " & k
    lcol = Sheet1.Range("DX2").End(xlToLeft).Column

    For i = 1 To lcol

        Set myLabel = Frame1.Controls.Add("Forms.label.1", "label" & i, True)

        myLabel.Left = 250
        myLabel.Top = 12 + (i * 20)
        myLabel.Width = 150
        myLabel.Height = 15

        Set txtbox = Frame1.Controls.Add("Forms.TextBox.1", "TextBox" & i, True)

        txtbox.Left = 50
        txtbox.Top = 10 + (i * 20)
        txtbox.Width = 180
        txtbox.Height = 60

    Next i

    For j = 1 To lcol

        With Frame1.Controls("label" & j)'Formating Labels
            .Caption = Sheet1.Cells(1, j).Value
            .TextAlign = fmTextAlignCenter
            .Font.Bold = True
            .Font.Size = 11
            .FontName = "Times New Roman"
            .ForeColor = vbRed
        End With

        With Frame1.Controls("TextBox" & j) 'Formating TextBoxes
            .Text = Sheet1.Cells(1, j).Offset(1, 0).Value
            .TextAlign = fmTextAlignRight
            .Font.Bold = True
            .Font.Size = 11
            .FontName = "Times New Roman"
        End With

        With Frame1.Controls("TextBox1")'Make textbox1 not editable
            .Enabled = False
        End With

        With Frame1.Controls("TextBox3")'Make textbox3 not editable
            .Enabled = False
        End With

    Next j

End Sub

1 个答案:

答案 0 :(得分:0)

我说

md-checkbox