2016-11-07 145 views
2

我正在創建一個宏以接收條目並將其放在第一條可用行上。這是迄今爲止我所擁有的。它不起作用,但我想要做的是使用Range函數來使用宏選擇列中的第一個可用行(使用SelectFirstEmptyCellColumn()宏)。我怎麼做這個錯誤?謝謝!!Excel宏中列中的第一個可用單元格

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 

回答

0

你需要更新你的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 
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

是的這工作!但是,「selectFirstEmptyCellColumn(col)」命令只在列A中選擇了第一個單元格,因爲它應該選擇列A中的第一個單元,然後是B,然後是C。你知道如何解決這個問題嗎?所以它把列A中的兩個條目,而不是把它放在B和C –

+0

@CollinTodd使用'.select'方法是一種不好的做法 – Vasily

+0

@Vasily我應該如何選擇我想要的單元格並更改工作表?有沒有更好的辦法?我對VBA相當陌生 –

0

替代的解決方案:

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 
相關問題