简体   繁体   English

VBA将日期添加到当前选择右侧的每个单元格?

[英]VBA to add date to each cell to the right of current selection?

I require some help with some Excel VBA I am working on, the current script checks a row for a date and if it is in the past it pastes a range to the next empty cell in row A. the range is currently 63 rows but may change. 我需要一些我正在使用的Excel VBA的帮助,当前脚本会检查一行中的日期,如果它是过去的日期,则会将范围粘贴到行A中的下一个空单元格中。该范围当前为63行,但可能更改。

What I need it to do is also add today's date to the cell to the right of each of these entries the script has just pasted. 我需要做的是还将今天的日期添加到脚本刚刚粘贴的每个条目右侧的单元格中。

Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks 谢谢

Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim Adm As Worksheet
Dim rng1 As Range
Dim NextRow As Range

Set ws = Sheets("Booking Count")
Set Adm = Sheets("Admin")
Set rng1 = ws.Columns("B:B").Find("*", ws.[B1], xlValues, , xlByRows, xlPrevious)

If Not rng1 Is Nothing Then
If CDate(rng1) < Date Then
    Set NextRow = ws.Range("A" & ws.UsedRange.Rows.Count - 61)
    Adm.Range("AllStaff").Copy
    ws.Activate
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
    Application.CutCopyMode = False
    Set NextRow = Nothing
    Sheets("Home").Activate
    Else
    MsgBox "The date entered into the TextBox is equal to today or later."
End If
Else
End If
Application.ScreenUpdating = True
End Sub

Needed to name the pasted range before offsetting and adding the date. 需要在偏移和添加日期之前命名粘贴范围。

Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim Adm As Worksheet
Dim rng1 As Range
Dim NextRow As Range
Dim NextRow2 As Range

Set ws = Sheets("Booking Count")
Set Adm = Sheets("Admin")
Set rng1 = ws.Columns("B:B").Find("*", ws.[B1], xlValues, , xlByRows, xlPrevious)

If Not rng1 Is Nothing Then
If CDate(rng1) < Date Then
    Set NextRow = ws.Range("A" & ws.UsedRange.Rows.Count - 61)
    Adm.Range("AllStaff").Copy
    ws.Activate
    NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
    Set NextRow2 = Selection
    Selection.Offset(0, 1).Value = Now()
    Application.CutCopyMode = False
    Set NextRow = Nothing
    Sheets("Home").Activate
    Else
    MsgBox "The date entered into the TextBox is equal to today or later."
End If
Else
End If
Application.ScreenUpdating = True
End Sub

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

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