Excel宏中的列中的第一个可用单元格

时间:2016-11-07 21:42:36

标签: excel vba excel-vba macros

我正在创建一个宏来获取一个条目并将其放在第一个可用的行上。这就是我到目前为止所拥有的。它不起作用,但我想要做的是使用Range函数使用SelectFirstEmptyCellColumn()宏选择列中的第一个可用行。我怎么做错了?谢谢!

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

3 个答案:

答案 0 :(得分:0)

试试这个

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

答案 1 :(得分:0)

避免使用.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

答案 2 :(得分:0)

您需要使用参数更新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)是用Sub SelectFirstEmptyCellColumn()替换function,因此您的代码可能如下所示:

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