我正在尝试使用工作表 1 上的命令按钮在第二张工作表 - 工作表 2 上 B 列的第一个空行上打印一个值(目前,仅使用“现在”作为虚拟值)更名为“日志”。有些东西在代码中不起作用 - 我试过:
Worksheets("Log").Activate
它可以激活第二个工作表,但它在工作表 1的第一个空行中打印了值!
我当前的代码(完整)如下:
Dim ws As Worksheet
Set ws = Worksheets("Log")
With ws
Dim emptyrow As Long
Dim LastRow As Variant
emptyrow = WorksheetFunction.CountA(Range("B:B")) + 1
LastRow = Cells(Rows.Count, 1).End(xlUp).Value
If IsNumeric(LastRow) Then
Else
LastRow = 0
End If
Cells(emptyrow, 2).Value = LastRow + 1 'I want to print an ascending count of the scenarios I have printed
Cells(emptyrow, 3).Value = Now 'This should print the time and date
End With
出于某种原因,这仍然分别在单元格 B2 和 B3 中的工作表 1 中打印“LastRow + 1”和“Now”的值。
Sub PasteDataOnEmptyRow()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = Worksheets("Log")
With ws
LastRow = .Cells(.Rows.count, 1).End(xlUp).row ' Last row keeping values in column A:A
.Cells(LastRow + 1, 2).Value = LastRow + 1 'This will print in column B:B (last empty row of A:A)
.Cells(LastRow + 1, 3).Value = Now 'This will print in column C:C (last empty row of A:A)
.Range("A" & LastRow + 1).Value = "Test in A:A last row"
.Range("A" & LastRow + 1).Offset(1, 0).Value = "Test in A:A the following row"
End With
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句