我正在為提交表單撰寫 Excel VBA 宏。我的目標是點擊提交按鈕并將輸入的資訊發送到資料庫表“shTaskDB”。該串列有 15 行可用,但很可能不會填寫所有這些行。我創建了一個直到回圈來傳輸輸入的資料,直到描述欄位為空。這是可行的,但問題是代碼只回傳提交表單中的最后一項,而不是每個行項。任何有關如何將每行條目傳輸到資料庫或如何清理代碼的幫助將不勝感激。 代碼和表單的影像
代碼:“任務記錄的開始代碼”將 shTaskDB 調暗為作業表集 shTaskDB = ThisWorkbook.Sheets("Task DB")
Dim TaskCurrentRow As Integer
TaskCurrentRow = shTaskDB.Range("A" & Application.Rows.Count).End(xlUp).row 1
使用 shTaskDB
shPMPlan.Range("L4").Select
' Set Do loop to stop when an empty cell is reached.
'Do Until IsEmpty(ActiveCell) = True
Do Until ActiveCell = ""
.Cells(TaskCurrentRow, 1) = shPMPlan.Range("C4")
.Cells(TaskCurrentRow, 2) = shPMPlan.Cells(ActiveCell.row,"K")
.Cells(TaskCurrentRow, 3) = shPMPlan.Cells(ActiveCell.row,"L")
.Cells(TaskCurrentRow, 4) = shPMPlan.Cells(ActiveCell.row,"M")
.Cells(TaskCurrentRow, 5) = shPMPlan.Cells(ActiveCell.row,"N")
.Cells(TaskCurrentRow, 6) = shPMPlan.Cells(ActiveCell.row,"O")
.Cells(TaskCurrentRow, 7) = shPMPlan.Cells(ActiveCell.row,"P")
ActiveCell.Offset(1, 0).Select
Loop
結束于
MsgBox "專案計劃已記錄"
uj5u.com熱心網友回復:
您的代碼從中逐行讀取,但只寫入sheetshPMPlan中的單行。所以你的回圈作業正常,但只有最后一個值被保留,因為每次迭代都會覆寫前一個。TaskCurrentRowshTaskDBshPMPlan
請考慮如下所示的模式。
Do Until ActiveCell = ""
'Write to TaskCurrentRow a row offset that we will increment each loop
.Cells(TaskCurrentRow TaskCurrentRowOffset, 1) = shPMPlan.Range("C4")
.Cells(TaskCurrentRow TaskCurrentRowOffset, 2) = shPMPlan.Cells(ActiveCell.row,"K")
.Cells(TaskCurrentRow TaskCurrentRowOffset, 3) = shPMPlan.Cells(ActiveCell.row,"L")
.Cells(TaskCurrentRow TaskCurrentRowOffset, 4) = shPMPlan.Cells(ActiveCell.row,"M")
.Cells(TaskCurrentRow TaskCurrentRowOffset, 5) = shPMPlan.Cells(ActiveCell.row,"N")
.Cells(TaskCurrentRow TaskCurrentRowOffset, 6) = shPMPlan.Cells(ActiveCell.row,"O")
.Cells(TaskCurrentRow TaskCurrentRowOffset, 7) = shPMPlan.Cells(ActiveCell.row,"P")
ActiveCell.Offset(1, 0).Select
'Increment the target row offset for next iteration
TaskCurrentRowOffset = 1 TaskCurrentRowOffset
Loop
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/471471.html
上一篇:在大范圍內修改(TextToDisplay)超鏈接的更快方法
下一篇:慢,嵌套的For回圈
