首页 文章

用空白单元格填充一列数据

提问于
浏览
1

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

即在sheet1(-blankcell- =空白)

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

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

aaa
bbb
ccc

在sheet1结束END结果

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

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
    

相关问题