添加日期列vba
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了添加日期列vba相关的知识,希望对你有一定的参考价值。
我想找到一种方法来自动添加当前日期列一次我将我选择的数据粘贴到工作表中。我当前的VBA代码采用结束日交易数据,嵌入到电子邮件正文中,并在发送给相关组之前将其作为excel文件附加。我希望VBA能够在运行宏后将日期列添加到活动工作表。代码如下:
Sub Mail_Sheet_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rng = Nothing
Set rng = ActiveSheet.UsedRange
'You can also use a sheet name
'Set rng = Sheets("YourSheet").UsedRange
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "Nabil.Hure@dnb.no"
.CC = ""
.BCC = ""
.Subject = "Today's Trades" & Date
.htmlBody = RangetoHTML(rng)
.Attachments.Add ActiveWorkbook.FullName
.Send 'or use .Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
有关如何执行此操作或将其放在当前VBA代码中的任何建议都将非常有用。
答案
如果要将当前日期/时间添加到Active Worksheet中的一系列单元格,可以使用以下代码:
ActiveSheet.Range(TopCornerCell, BottomCornerCell).Value = Now()
对于日期(而不是时间),您可以使用:
ActiveSheet.Range(TopCornerCell, BottomCornerCell).Value = Int(Now())
了解TopCornerCell
和BottomCornerCell
将取决于您的要求和数据。
最后,您可以使用Range.NumberFormat
更改范围的格式,并使用Range.EntireColumn.Autofit
自动调整列宽
另一答案
当您在UsedRange
变量中持有rng
时,您可以在Set rng = ActiveSheet.UsedRange
之后添加几行:
Set rng = ActiveSheet.UsedRange
rng.Offset(, rng.Columns.Count).Resize(, 1) = Date
Set rng = rng.Resize(, rng.Columns.Count + 1)
第二行设置对范围旁边的列的引用,并使用今天的日期填充它。
最后一行然后将新列添加到rng
变量。您可以再次使用UsedRange
,但在测试中我发现这不可靠 - 通常最好不要使用UsedRange
。
要在不使用UsedRange
的情况下查找最后一个单元格,可以使用FIND
查找最后一个非空单元格。
Sub Test()
Dim rng As Range
Set rng = ActiveSheet.Range("A1", LastCell(ActiveSheet))
'...
'...
'...
End Sub
Public Function LastCell(wrkSht As Worksheet) As Range
Dim lLastCol As Long, lLastRow As Long
On Error Resume Next
With wrkSht
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
If lLastCol = 0 Then lLastCol = 1
If lLastRow = 0 Then lLastRow = 1
Set LastCell = wrkSht.Cells(lLastRow, lLastCol)
End With
On Error GoTo 0
End Function
以上是关于添加日期列vba的主要内容,如果未能解决你的问题,请参考以下文章
用于比较具有包含日期的单元格的列并将特定文本粘贴到另一列的 Vba 代码