返回列表 上一主題 發帖

[發問] 如何用VBA讓公式複制到工作表有資料的最後一列

回復 1# PJChen

請問一下針對第二個問題:
您是希望E欄一直持續加1,也就是488,489,490...
還是就483~487一直循環?
不是很懂複製的意思?
Excel VBA網頁資料收集教學:
http://forum.twbts.com/thread-20848-1-1.html

TOP

回復 3# PJChen

不知道這樣符不符合您的需求
  1. Sub test()

  2. lastRow1 = Cells(Rows.Count, "F").End(xlUp).Row
  3. lastRow2 = Cells(Rows.Count, "A").End(xlUp).Row
  4. If lastRow2 <> lastRow1 Then
  5. Range("A" & lastRow2 & ":" & "C" & lastRow2).AutoFill Destination:=Range("A" & lastRow2 & ":C" & lastRow1)
  6. For i = lastRow2 To lastRow1 - 1
  7.     Cells(i + 1, "E") = Cells(i, "E") + 1
  8. Next
  9. End If
  10. End Sub
複製代碼
Excel VBA網頁資料收集教學:
http://forum.twbts.com/thread-20848-1-1.html

TOP

本帖最後由 iamaraymond 於 2018-5-14 22:38 編輯

回復 5# PJChen

想先確認一下您的問題:
您是說當執行VBA指令裡的程式時
1.若此時"自動複製"關著,則可以執行往下自動填滿
2.若此時"自動複製"開著,就沒有動作
所以您希望當"自動複製"開著時,也可以往下自動填滿?
不知道我的理解正不正確

您的第二個問題是希望"自動複製"的E2=1,E3=2,一直到目前最後一列E11嗎?
Excel VBA網頁資料收集教學:
http://forum.twbts.com/thread-20848-1-1.html

TOP

回復 7# PJChen

這樣嗎?
  1. With Wb.Sheets("訂單出貨")
  2.    
  3.     lastRow1 = .Cells(Rows.Count, "G").End(xlUp).Row  '以G欄的資料列為基準
  4.     lastRow2 = .Cells(Rows.Count, "A").End(xlUp).Row '以A欄的資料列為基準
  5.     lastRow3 = .Cells(Rows.Count, "BL").End(xlUp).Row '以BL欄的資料列為基準
  6.     lastRow4 = .Cells(Rows.Count, "BP").End(xlUp).Row '以BP欄的資料列為基準
  7.    
  8.     If lastRow2 <> lastRow1 Then
  9.     .Range("A" & lastRow2 & ":" & "C" & lastRow2).AutoFill Destination:=.Range("A" & lastRow2 & ":C" & lastRow1)  '當A欄最後一列不等於G欄時,A:C自動往下填滿

  10.     For i = 2 To lastRow1
  11.     .Cells(i, "E") = i - 1
  12.     Next
  13.     End If
  14.    
  15.     If lastRow3 <> lastRow1 Then
  16.     .Range("BL" & lastRow3 & ":" & "BN" & lastRow3).AutoFill Destination:=.Range("BL" & lastRow3 & ":BN" & lastRow1)  '當BL欄最後一列不等於G欄時,自動往下填滿
  17.    
  18.     End If

  19.     If lastRow4 <> lastRow1 Then
  20.     .Range("BP" & lastRow4 & ":" & "CX" & lastRow4).AutoFill Destination:=.Range("BP" & lastRow4 & ":CX" & lastRow1)  '當BP欄最後一列不等於G欄時,自動往下填滿
  21.    
  22.     End If

  23. End With
複製代碼
Excel VBA網頁資料收集教學:
http://forum.twbts.com/thread-20848-1-1.html

TOP

回復 9# PJChen
其實只是幫您加個"."而已XD
用with的時候要使用"."才可以使用該物件
Excel VBA網頁資料收集教學:
http://forum.twbts.com/thread-20848-1-1.html

TOP

        靜思自在 : 地上種了菜,就不易長草;心中有善,就不易生惡。
返回列表 上一主題