如何将B1向下复制到数据集中的最后一列中

问题描述 投票:0回答:1

我正在尝试将列从B1向下拖动到数据集中的最后一行。 B1将始终填充值,我需要复制该值并将[x“的次数粘贴到B列的下方,具体取决于工作表上的行数。

Sub Button1test_Click()

Set src = Sheets("Sheet1")
Dim Lastrow As Long

Lastrow = Range("B" & Rows.Count).End(xlUp).Row

src.Range("B1:B" & Lastrow).Value = "B1"

End Sub

B应填充'lucord'x次数。

excel vba
1个回答
0
投票

"B1"是文本“ B1”,而不是对范围B1的引用。

[这也是With...End With块限定Range调用的好用例-否则Lastrow是从活动工作表中确定的,不一定是src

更改

Lastrow = Range("B" & Rows.Count).End(xlUp).Row

src.Range("B1:B" & Lastrow).Value = "B1"

to

With src
    Lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
    .Range("B1:B" & Lastrow).Value = .Range("B1").Value
End With

EDIT:如果仅填充B1,则Lastrow = 1。根据填充的列确定Lastrow,例如列A:

Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
© www.soinside.com 2019 - 2024. All rights reserved.