簡體   English   中英

Excel VBA自動填充列

[英]Excel VBA To AutoFill Column

在AI列中有5個名稱的列表-我想在B列中創建不同的團隊並將其命名為Team_1,Team_2等

這是我嘗試過的語法...

Function AutoFill()
  Dim KCLR As Long
  KCLR = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
  Range("B2").Select
  ActiveCell.FormulaR1C1 = "Team_1"
  Selection.AutoFill Destination:=Range("B2:" & KCLR)
End Function

但是,在Selection.AutoFill Destination:=Range("B2:" & KCLR) ,出現錯誤

Range(“ B2:”&KCLR)=對象'_Global'的方法'Range'失敗

我需要做些什么才能使其成功完成AutoFill

Selection.AutoFill Destination:=Range("B2:" & KCLR)應該為Selection.AutoFill Destination:=Range("B2:B" & KCLR)因為KCLR返回數字。

遵循@ harun24hr答案之后,為了將來更好的編碼實踐,請避免使用SelectActiveCellSelection ,而應使用完全合格的Range

請參見下面的示例:

Function AutoFill()

  Dim KCLR As Long

  KCLR = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
  Range("B2").FormulaR1C1 = "Team_1"
  Range("B2").AutoFill Destination:=Range("B2:B" & KCLR)

End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM