简体   繁体   English

VBA使用今天的日期并粘贴数据,在“月”列中选择最后一个空单元格

[英]VBA Select last empty cell in Month column using today's date and paste data

I'm trying to use VBA script to trigger when checkbox is checked that copies data from one particular cell and pastes in the last empty cell of Month column using Today's date. 我正在尝试使用VBA脚本来触发以下操作:选中复选框以从一个特定单元格复制数据并使用“今天”的日期粘贴到“月”列的最后一个空单元格中。 Here is my code thus far, and I've tested the check box triggering the copy and paste function. 到目前为止,这是我的代码,并且我已经测试了触发复制和粘贴功能的复选框。 What I can't figure out is finding the correct column using today's date and selecting the next empty cell in that column. 我不知道的是使用今天的日期找到正确的列,然后选择该列中的下一个空单元格。 My columns are labeled on a second sheet using long month names (text data). 我的列在第二张工作表上使用长月份名称(文本数据)标记。

Sub CheckBoxUpdated()
Dim Mnth As String
Dim fndrng
Dim cb As CheckBox


Mnth = MonthName(Month(Date))
With Sheet2 'has to be 'with' something to work correctly
    Set fndrng = Cells.Find(What:=Mnth, After:=A1, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=True)
End With

    On Error Resume Next
    Set cb = ActiveSheet.DrawingObjects(Application.Caller)
    On Error GoTo 0

    If Not cb Is Nothing Then
        If cb.Value = 1 Then
            Sheets("Sheet1").Range(cb.LinkedCell).Offset(0, -4).Copy
            Sheets("Sheet2").Activate
            fndrng.Offset(4, 0).Select
            ActiveSheet.Paste
        End If
    End If

End Sub

Any help is much appreciated, thanks!!!! 任何帮助都非常感谢,谢谢!!!

Two things I noticed immediately. 我立即注意到两件事。

  1. Within your first With...End With statement , the Set fndrng = Cells.Find ... is missing the prefix period that assigns the worksheet parent from the With statement. 在您的第一个With ... End With语句中Set fndrng = Cells.Find ...缺少从With语句分配工作表父级的前缀期。 Should be Set fndrng = .Cells.Find... 应该Set fndrng = .Cells.Find...

  2. The close of the With Sheet2 could be extended down to encompass much more of the code, releasing you from dependence on things like ActiveSheet and Select . With Sheet2的关闭可能会扩展到涵盖更多代码,从而使您摆脱对ActiveSheetSelect之类的依赖。

Consider this rewrite. 考虑此重写。

Sub CheckBoxUpdated()
    Dim Mnth As String, fndrng as range, cb As CheckBox

    On Error Resume Next

    Mnth = MonthName(Month(Date))

    With Sheet2 'has to be 'with' something to work correctly
        Set fndrng = Cells.Find(What:=Mnth, After:=A1, LookAt:=xlPart, _
                                SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                MatchCase:=True)

        Set cb = .DrawingObjects(Application.Caller)
        On Error GoTo 0

        If Not cb Is Nothing Then
            If cb.Value = 1 Then
                Sheets("Sheet1").Range(cb.LinkedCell).Offset(0, -4).Copy _
                    Destination:=fndrng.Offset(4, 0)
            End If
        End If

    End With

End Sub

I changed your method of Copy & Paste to a more direct method in keeping with the expansion of the With/End With statement. 为了适应With / End With语句的扩展,我将复制和粘贴方法更改为更直接的方法。

See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals. 有关摆脱依赖于选择和激活来实现目标的更多方法,请参见如何避免在Excel VBA宏中使用“选择”

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

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