简体   繁体   English

Excel VBA-生成下一个空行号并复制到电子表格

[英]Excel VBA - generate next empty row number and copy to spreadsheet

VBA Issue. VBA问题。

I'm a VBA novice, and I'm trying to see if I can get my code to do just a little more for me. 我是VBA的新手,我想看看是否可以让我的代码为我做更多的事情。 Currently, the code opens a spreadsheet on my sharedrive, checks for the next empty row, and provides me with row number. 当前,该代码在我的共享驱动器上打开一个电子表格,检查下一个空行,并向我提供行号。 For example, I have 39 rows of data, and the 40th row is empty, it give me the number 40. 例如,我有39行数据,而第40行为空,它的编号为40。

It currently provides me this number in a msgbox. 目前,它在msgbox中为我提供了此号码。 What I would like it to do is input that number in to a cell on my open worksheet. 我想要做的是将该数字输入到打开的工作表中的单元格中。

The number essentially works as an ID, which also shows us how many 'complaints' have been filed. 该号码实际上是一个ID,也可以向我们显示已提交了多少“投诉”。

Any Ideas? 有任何想法吗?

Here is my code currently: 这是我目前的代码:

Sub LastRowWithData()

Dim lastRow As Long
Dim ConcernMemosMaster As Workbook

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set ConcernMemosMaster = Workbooks.Open("N:\myfilepath")
lastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1

ConcernMemosMaster.Close

Application.DisplayAlerts = True

MsgBox lastRow

End Sub

The below code will stick the number of rows + 1 ( your lastRow variable ) in the active sheet cell A1 . 以下代码将在活动工作表单元格A1 lastRow行数+ 1( 您的lastRow变量 )。

If you wanted it in a different cell than modify the line 如果要在其他单元格中而不是修改行

currentSheet.Range("A1") = lastRow

and replace A1 with a different cell reference. 并用其他单元格引用替换A1

Your macro: 您的宏:

Sub LastRowWithData()

    Dim currentSheet As Worksheet
    Set currentSheet = ActiveSheet

    Dim lastRow As Long
    Dim ConcernMemosMaster As Workbook

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Set ConcernMemosMaster = Workbooks.Open("N:\myfilepath")
    lastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1

    ConcernMemosMaster.Close

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    currentSheet.Range("A1") = lastRow

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM