Excelmacros中列中第一个可用的单元格

我正在创build一个macros来接受一个条目,并把它放在第一个可用的行。 这是我迄今为止。 它不起作用,但是我想要做的是使用Range函数来使用SelectFirstEmptyCellColumn()macros来select列中的第一个可用行。 我怎么做这个错误? 谢谢!!

 Sub SelectFirstEmptyCellColumn() Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select End Sub ' Sub OUT() ' ' OUT Macro ' ' Sheets("Interface").Select Range("B2").Select Selection.Cut Sheets("Items out").Select Range("SelectFirstEmptyCellColumn(B)").Select ActiveSheet.Paste Sheets("Interface").Select Range("B3").Select Selection.Cut Sheets("Items out").Select Range("SelectFirstEmptyCellColumn(A)").Select ActiveSheet.Paste Range("SelectFirstEmptyCellColumn(C)").Select ActiveCell.Value = Now End Sub 

你需要用参数更新你的SelectFirstEmptyCellColumn (例如列号或者数字和图纸名称)

也避免使用.select ,所以你的代码可以是这样的(这只是一个例子)

 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 

但更好的方法(imho)是用一个functionreplaceSub 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 

尝试这个

 Sub SelectFirstEmptyCellColumn(col) Cells(Rows.Count, col).End(xlUp).Offset(1, 0).Select End Sub ' Sub OUT() ' ' OUT Macro ' ' Sheets("Interface").Select Range("B2").Select Selection.Cut Sheets("Items out").Select SelectFirstEmptyCellColumn "b" ActiveSheet.Paste Sheets("Interface").Select Range("B3").Select Selection.Cut Sheets("Items out").Select SelectFirstEmptyCellColumn "a" ActiveSheet.Paste SelectFirstEmptyCellColumn "c" ActiveCell.Value = Now End Sub 

替代解决scheme,避免使用。select:

 Sub tgr() Dim wb As Workbook Dim wsData As Worksheet Dim wsDest As Worksheet Dim lRow As Long Set wb = ActiveWorkbook Set wsData = wb.Sheets("Interface") Set wsDest = wb.Sheets("Items out") lRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Row + 1 wsData.Range("B2").Cut wsDest.Cells(lRow, "B") wsData.Range("B3").Cut wsDest.Cells(lRow, "A") wsDest.Cells(lRow, "C").Value = Now End Sub