你需要更新你的SelectFirstEmptyCellColumn
與參數(例如列字母或數字,和SHEETNAME )
也避免使用.select
,所以你的代碼ca n爲這樣的事情(這只是一個例子)
Public cl As Range
Sub GetFirstEmptyCellColumn(ShName$, col$)
Set cl = Sheets(ShName).Cells(Rows.Count, col).End(xlUp).Offset(1, 0)
End Sub
Sub OUT()
With Sheets("Interface")
GetFirstEmptyCellColumn "Items out", "B": cl.Value = .[B2].Value
GetFirstEmptyCellColumn "Items out", "A": cl.Value = .[B3].Value
GetFirstEmptyCellColumn "Items out", "C": cl.Value = Now
.[B2,B3].ClearContents
End With
End Sub
,也可以是這樣的:
Sub InsertIntoFirstEmptyCellColumn(shSource$, clSource$, shDest$, col$)
Sheets(shDest).Cells(Rows.Count, col).End(xlUp).Offset(1, 0).Value = Sheets(shSource).Range(clSource).Value
Sheets(shSource).Range(clSource).ClearContents
End Sub
Sub OUT()
InsertIntoFirstEmptyCellColumn "Interface", "B2", "Items out", "B"
InsertIntoFirstEmptyCellColumn "Interface", "B3", "Items out", "A"
Sheets("Items out").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = Now
End Sub
,但更好的辦法(恕我直言)是一個function
更換Sub SelectFirstEmptyCellColumn()
,所以你代碼可以看起來像這樣:
Function GetFirstEmptyCellColumn(ShName$, col$) As Range
Set GetFirstEmptyCellColumn = Sheets(ShName).Cells(Rows.Count, col).End(xlUp).Offset(1, 0)
End Function
Sub OUT()
With Sheets("Interface")
GetFirstEmptyCellColumn("Items out", "B").Value = .[B2].Value
GetFirstEmptyCellColumn("Items out", "A").Value = .[B3].Value
GetFirstEmptyCellColumn("Items out", "C").Value = Now
.[B2,B3].ClearContents
End With
End Sub
是的這工作!但是,「selectFirstEmptyCellColumn(col)」命令只在列A中選擇了第一個單元格,因爲它應該選擇列A中的第一個單元,然後是B,然後是C。你知道如何解決這個問題嗎?所以它把列A中的兩個條目,而不是把它放在B和C –
@CollinTodd使用'.select'方法是一種不好的做法 – Vasily
@Vasily我應該如何選擇我想要的單元格並更改工作表?有沒有更好的辦法?我對VBA相當陌生 –