2012-10-23 34 views
1

如何使用VBA複製從即Sheet 2中的數據,在工作表Sheet用數據填滿一列上的空白單元格

即填補空白單元格的範圍(1列)在工作表Sheet(-blankcell- =空白)

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

在Sheet 2中

aaa 
bbb 
ccc 

END結果在工作表Sheet

aaa 
bbb 
ccc 
ahjefe 
aaa 
bbb 
23234 
ccc 
aaa 
8569 
bbb <- stopped at before last blank row 
(數據到Sheet 1上的空白小區中使用)
+3

你能告訴我們你到目前爲止所嘗試過的嗎? – PowerUser

+0

@ PowerUser的評論+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 
相關問題