2017-02-10 46 views
0

您好,我正在用VBA編寫用戶表單代碼。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 

回答

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 
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