VBA用户表单填写整栏

时间:2017-02-10 08:42:11

标签: excel vba excel-vba userform

您好我正在VBA中编写用户表单代码。

代码找到具有给定名称的列,然后它应该填充列,直到包含数据的最后一行和用户表单中的值。

任何想法我该怎么做?

Dim intBB As Integer
Do While Worksheets("Sheet1").Cells(1, intBB) <> ""
        If Worksheets("Sheet1").Cells(1, intBB).Value = "HPL" Then
            With Worksheets("Sheet1")
                Set rngBB = .Range(.Cells(1, intBB), .Cells(1, intBB))                    
             End With
         Exit Do

        End If
          intBB = intBB + 1
    Loop

Cells(2, intBB).Value = HPLBox.Value

2 个答案:

答案 0 :(得分:0)

您必须在找到的列中找到最后使用的行:
LastRow = .Cells(.Rows.Count, intBB).End(xlUp).Row

然后您可以直接将值应用于整个范围:rngBB.value = HPLBox.value

Dim intBB As Long
Dim LastRow As Long
Dim rngBB As Range

With ThisWorkbook.Sheets("Sheet1")
    Do While .Cells(1, intBB) <> vbNullString
        If .Cells(1, intBB).value <> "HPL" Then
        Else
            LastRow = .Cells(.Rows.Count, intBB).End(xlUp).Row
            Set rngBB = .Range(.Cells(1, intBB), _
                                .Cells(LastRow, intBB))
            Exit Do
        End If
        intBB = intBB + 1
    Loop
End With
rngBB.value = HPLBox.value

答案 1 :(得分:0)

我喜欢以下

Dim rngBB As Range

With Worksheets("Sheet1")
    Set rngBB = .Range("A1", .Cells(1, .Columns.count).End(xlToLeft)).Find(what:="HPL", LookIn:=xlValues, lookat:=xlWhole)
    If Not rngBB Is Nothing Then  .Range(rngBB.Offset(1), .Cells(.Rows.count, rngBB.Column).End(xlUp)).Value = HPLBox.Value
End With