Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba Excel宏中列中的第一个可用单元格_Vba_Excel_Macros - Fatal编程技术网

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

Vba Excel宏中列中的第一个可用单元格,vba,excel,macros,Vba,Excel,Macros,我正在创建一个宏来获取一个条目并将其放在第一行。这就是我目前所拥有的。它不起作用,但我尝试使用Range函数,使用SelectFirstEmptyCellColumn()宏选择列中的第一行。我怎么会做错这件事?谢谢 Sub SelectFirstEmptyCellColumn() Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select End Sub ' Sub OUT() ' ' OUT Macro ' ' Sheets("Inter

我正在创建一个宏来获取一个条目并将其放在第一行。这就是我目前所拥有的。它不起作用,但我尝试使用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
试试这个

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

避免使用的替代解决方案。选择:

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

您需要使用参数(例如,列字母或数字以及表名)更新
SelectFirstEmptyCellColumn

还要避免使用
。选择
,这样您的代码可以是这样的(这只是一个示例)

也可以是这样的:

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 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
但更好的方法(imho)是将
子选择FirstEmptycellColumn()
替换为
函数,这样您的代码可以如下所示:

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 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和B列C@CollinTodd使用
.select
方法是一种错误的方法practice@Vasily我还应该如何选择所需的单元格并更改工作表?有更好的办法吗?我是个新手VBA@CollinTodd看到我的帖子了吗