Autofil直到列结束

我原来的程序能够find我需要基于月和周的单元格,现在我需要修改程序来复制第5行中最后使用的单元格并粘贴到列的末尾。

防爆。 如果11月份的月份是第4周,程序就知道去那里填写信息。 我无法弄清楚如何将Nov wk 4的值粘贴到列的其余部分。 此外,我的范围可能是H5:BA5或BC5:DB5取决于月份和周开始的位置。

我添加了一张图片,显示我的数据是如何设置的,突出显示的单元格需要填入直到结束

在这里输入图像说明

With ThisWorkbook.Sheets(SheetName) Dim c2 As Integer Dim LastCol2 As Integer c2 = 2 LastCol2 = .Cells(4, .Columns.Count).End(xlToLeft).Column Do Until .Cells(1, c2).Value = "Sept" And .Cells(4, c2).Value = "Wk 5" If .Cells(1, c).Value = MonthSel And .Cells(4, c).Value = WkSel Then .Cells(5, c2).Select Selection.copy ActiveCell.Offset(0, 1).Select Selection.Paste Application.CutCopyMode = False Selection.AutoFill Destination:=Range("H5:BA5"), Type:=xlFillDefault Range("H5:BA5").Select // need to change this range to reach the end of column 5 End If Loop End With 

一个更简单的解决scheme(如果我们假设在你的单元格中有某种查找公式)就是抓取所有的B列并自动填充所有的52周:

 Sub TestingIt() Dim LastRow As Long With ThisWorkbook.Sheets(SheetName) LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row Range("B5:B" & LastRow).AutoFill Destination:=Range("B5:BA" & LastRow), Type:=xlFillCopy End With End Sub 

首先你需要停止使用。 .SELECT

其次,你已经想出了如何find代码中最后使用的列。 为什么不使用相同的逻辑来查找最后使用的行?

 With ThisWorkbook.Sheets(SheetName) Dim c2 As Long, LastCol2 As Long, LastRow As Long c2 = 2 LastCol2 = .Cells(4, .Columns.Count).End(xlToLeft).Column Do Until .Cells(1, c2).Value = "Sept" And .Cells(4, c2).Value = "Wk 5" If .Cells(1, c).Value = MonthSel And .Cells(4, c).Value = WkSel Then .Cells(5, c2).copy .Cells(5, c2).Offset(0, 1).Paste Application.CutCopyMode = False LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row .Cells(5, c2).Offset(0, 1).AutoFill Destination:=Range("H5:BA" & LastRow), Type:=xlFillDefault End If Loop End With