Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将数据附加到工作表的按钮_Excel_Vba - Fatal编程技术网

Excel 将数据附加到工作表的按钮

Excel 将数据附加到工作表的按钮,excel,vba,Excel,Vba,我正在寻找创建一个excel工作簿,在那里我将有一个按钮-当点击它时,它应该写上日期,它在那一天点击的次数 我能够根据宏来写日期,但是我在两种情况下都很挣扎。目前,我只能更新同一单元格中的日期。 1.我想在下一行添加它,而不是删除同一单元格并更新它。 2.用户当天单击的次数应位于其旁边的列上,应动态更新,并且应与其旁边的日期列匹配 任何关于逻辑的想法都会有帮助 这是结果,我希望得到,这是两列 7/29/19 5 7/30/19 9 7/31/19 12 如果我明白你的意

我正在寻找创建一个excel工作簿,在那里我将有一个按钮-当点击它时,它应该写上日期,它在那一天点击的次数

我能够根据宏来写日期,但是我在两种情况下都很挣扎。目前,我只能更新同一单元格中的日期。 1.我想在下一行添加它,而不是删除同一单元格并更新它。 2.用户当天单击的次数应位于其旁边的列上,应动态更新,并且应与其旁边的日期列匹配

任何关于逻辑的想法都会有帮助

这是结果,我希望得到,这是两列

7/29/19     5
7/30/19     9
7/31/19     12

如果我明白你的意思,我就试试看

Sub stuff()
    Dim dateCol As Range
    Dim countCol As Range

    Set dateCol = ThisWorkbook.Worksheets("Sheet2").Range("A:A") ' Change to proper sheet
    Set countCol = ThisWorkbook.Worksheets("Sheet2").Range("B:B") ' Change to proper sheet

    Dim foundDate As Range
    Dim searchDate As String
    searchDate = Format(Now(), "m/dd/yyyy")
    Set foundDate = dateCol.Find(what:=searchDate, lookat:=xlWhole)

    If Not foundDate Is Nothing Then
        foundDate.Offset(0, 1).Value = foundDate.Offset(0, 1).Value + 1
    Else
        Dim newEntry As Range
        If dateCol.Cells(1, 1).Value <> "" Then
            Set newEntry = dateCol.Cells(dateCol.Cells.Count, 1).End(xlUp).Offset(1, 0)
        Else
            Set newEntry = dateCol.Cells(1, 1)
        End If
        newEntry.Value = searchDate
        newEntry.Offset(0, 1).Value = 1
    End If

End Sub

因此,首先,我们必须让我们的程序计算出列表中哪个单元格包含最近的日期。一旦我们得到这个单元格,我们就可以将它的值与当前日期进行比较,以确定是否需要在当天添加+1或开始一个新行

我在下面提供了一个示例子程序,您必须替换方括号内的内容,以匹配您正在使用的工作表

Sub buttonPressed()
    Dim lastRow, dateColumn As Long
    Dim lastDate As Date
    Dim clickCtr As Long

    With Sheets([sheetName])
        'Set the column containing dates.
        dateColumn = [column number where dates are stored]

        'Get the last row containing a date and save it into lastRow
        lastRow = .Cells(.Rows.Count, dateColumn).End(xlUp).row

        'Convert value of lastRow into a date
        lastDate = DateValue(.Cells(lastRow, dateColumn).Value)

        'Compare last date to current date
        If (lastDate = Date) Then
            'Get the current button press count and add 1 to it
            clickCtr = .Cells(lastRow, dateColumn + 1).Value + 1
            'Replace contents of cell with new click amount
            .Cells(lastRow, dateColumn + 1).Value = clickCtr
        Else
            'Add the new date to the next row
            .Cells(lastRow + 1, dateColumn).Value = Date
            'Start the counter next to the new date
            .Cells(lastRow + 1, dateColumn + 1).Value = 1
        End If
    End With
End Sub

我想确保我理解你的问题。因此,单击此按钮时,如果是新日期,则会将当前日期添加到下一行,并启动一个新计数器。当按下按钮时,如果当前日期与列表中最近的日期匹配,那么它只需将当天的计数加上+1即可。汤姆-你说得对。只有一件事——下一行应该在列的末尾,以确保我们在同一页上