1

如何使用VBA从即sheet2复制数据,在sheet1的空白单元格上填充一个范围(1列)

即在 sheet1(-blankcell- = 空白)

-blankcell-  
-blankcell-  
-blankcell-
ahjefe
-blankcell-
-blankcell-
23234
***fg4
-blankcell-
8569
-blankcell-

在 sheet2(要在 sheet1 空白单元格上使用的数据)

aaa
bbb
ccc

表 1 的 END 结果

aaa
bbb
ccc
ahjefe
aaa
bbb
23234
ccc
aaa
8569
bbb <- stopped at before last blank row
4

1 回答 1

2

我有一些空闲时间,所以我在下面为您编写了这段代码。但是,最好在提出问题时始终表现出您的努力。通常人们不只是为你编写代码。

Sub FillBlanks()

Dim wks1 As Worksheet, wks2 As Worksheet

Set wks1 = Sheets(1) 'change to your needs; could also be Sheets("Sheet1") format
Set wks2 = Sheets(2) 'change to your needs; could also be Sheets("Sheet1") format

Dim rngLookup As Range

'assume data on sheet2 is on column A
With wk2
    Set rngLookup = .Range(.Range("A1"), .Range("A" & .Rows.Count).End(xlUp))
End With

With wks1

    Dim rngSearch As Range
    'assume blank cells are in column A on sheet 1
    Set rngSearch = .Range(.Range("A1"), .Range("A" & .Rows.Count).End(xlUp))
    Set rngSearch = rngSearch.SpecialCells(xlCellTypeBlanks)

    Dim cel As Range, i As Integer

    i = 1

    For Each cel In rngSearch

        cel.Value = rngLookup.Cells(i, 1)
        If i = rngLookup.Rows.Count Then i = 1 Else: i = i + 1

    Next


End With

End Sub
于 2012-10-23T15:49:07.993 回答