在最近添加的行上运行macros

我正在使用一个macros来生成基于已经input到工作簿中的数据的Outlook模板。

在工作簿中,我有100行数据和7张表格。

我需要运行最近一行数据的macros(点击button)并生成模板。

我的行包含时间数据(例如13:37,下一行14:02等),所以我认为这可能是识别最新行的好方法。

我正在使用这个代码。 我正在使用A203:G203select该行

Sub NonConformanceGenerator() ActiveSheet.Range("A203:G203").Select Const HEADER_ROW As Long = 202 '<< the row with column headers Const NUM_COLS As Long = 7 '<< how many columns of data Const olMailItem = 0 Const olFolderInbox = 6 Dim ol As Object, fldr, ns, msg Dim html As String, c As Range, colReq As Long, hdr As Range Dim rw As Range On Error Resume Next Set ol = GetObject(, "outlook.application") On Error GoTo 0 If ol Is Nothing Then On Error Resume Next Set ol = CreateObject("outlook.application") Set ns = ol.GetNamespace("MAPI") Set fldr = ns.GetDefaultFolder(olFolderInbox) fldr.display On Error GoTo 0 End If If ol Is Nothing Then MsgBox "Couldn't start Outlook to compose mail!", vbExclamation Exit Sub End If Set msg = ol.CreateItem(olMailItem) Set rw = Selection.Cells(1).EntireRow msg.Subject = "" html = "<style type='text/css'>" html = html & "body, p {font:11pt calibri;padding:40px;}" html = html & "table {border-collapse:collapse}" html = html & "td {border:1px solid #000;padding:8px;}" html = html & "</style>" html = html & "<p>Hello,</p>" html = html & "<table>" For Each c In rw.Cells(1).Resize(1, NUM_COLS).Cells If c.Column <> 0 Then '<<< This removes the 4th column if you type number 4 after the <> symbols Set hdr = rw.Parent.Cells(HEADER_ROW, c.Column) '<< get the header text for this cell html = html & "<tr><td style='background-color:#FFF;width:200px;'>" & _ hdr.Value & _ "</td><td style='width:400px;'>" & Trim(c.Value) & "</td></tr>" End If 'we want this cell Next c html = html & "</table>" msg.HTMLBody = html msg.display ActiveSheet.Range("A15").Select End Sub 

最新的一行总是在电子表格的底部? 如果是这样的话,可以使用Cells(Rows.Count, "A").End(xlUp).Row来返回最后一行,例如列“A”中的数据。

你可以做这样的事情在你的例子中使用。

 With ActiveSheet .Range("A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Resize(1, 7).Select End With