VBA TextBox将colunm中的值填充到特定范围

时间:2017-12-10 22:21:20

标签: excel-vba vba excel

我的工作簿有两张:一张"数据"和一个" Kiert"。我解决了根据" data"中的特定属性复制行的问题。到" Kiert"使用UserForm,但我添加了ti用户表单四个文本框(TextBox1,TextBox2等),我想在粘贴数据后用空白列中的一个命令按钮在文本框中添加常量值来填充数据库。 我有额外的textbox5,它表明副本是否成功(" SIKERES"),这部分工作正常......

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Dim Drng As Range, c As Range
    Dim i As Long
    Dim lastRow As Long
    Dim srcRange As Range, fillRange As Range
    Set a = TextBox5
    Set d = TextBox1
    Set ws = Sheets("Data")
    Set Drng = ws.Columns("A:A").SpecialCells(xlCellTypeConstants, 23)
    For Each c In Drng.Cells
        If c = ListBox1 Then
            c.EntireRow.Copy 
            Sheets("Kiert").Range("A1000000").End(xlUp).Offset(1, 0)
            Range("F:F" & lastRow).Formula = TextBox1.Value
        If c.Value = ListBox1.Value Then
            a.Value = "SIKERES"
        End If
        End If
    Next c 
End Sub

我在这里插入一个例子:

enter image description here

我的主要问题是我无法描述文本框的正确范围和描述,而且我也不知道我可以将它放在我的代码中以便正确运行它。

我试过了:

    For Each c In Drng.Cells
    If c = ListBox1 Then
        c.EntireRow.Copy Sheets("Summary").Range("A1048576").End(xlUp).Offset(1, 0)
        Sheets("Kiert").Range("A:A" & lasrRow).Value = TextBox1.Text
        If c.Value = ListBox1.Value Then
            A.Value = "SIKERES"
        End If
    End If
Next c

......但是它超出了范围。

1 个答案:

答案 0 :(得分:0)

目前还不是很清楚你要做什么,但下面的代码可以帮助你将文本框的值粘贴到相关列:

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Dim Drng As Range, c As Range
    Dim i As Long
    Dim NextFreeRow As Long
    Dim srcRange As Range, fillRange As Range
    Set Drng = Sheets("Data").Columns("A:A").SpecialCells(xlCellTypeConstants, 23)
    For Each c In Drng.Cells 'loop through Column A on Sheet Data
        If c = ListBox1.Value Then 'If the cells in Column A Sheet Data matches the selection on your Listbox1 then
            NextFreeRow = Sheets("Kiert").Cells(Rows.Count, "A").End(xlUp).Row + 1 'Check the next free row on Sheet Kiert
            c.EntireRow.Copy Desination:=Sheets("Kiert").Range("A" & NextFreeRow) 'Paste the entire row from Sheet Data to Sheet Kiert
            Range("F" & NextFreeRow).Value = TextBox1.Text 'Copy the contents of TextBox1 to column F
            'Add more lines like the one above to copy the values from your Textboxes to the relevant column
            TextBox5.Text = "SIKERES"
        End If
    Next c
End Sub