2017-01-24 79 views
2

我試圖將列H中的內容複製到C列,僅當列H中的單元格爲空時。僅當單元格爲空時,將單元格內容從一列複製到另一列

如果H是空的,我想離開是列C.

這裏的文字是我有什麼,它拷貝H的內容,但如果H是空的,它也是清空列C.

Sub Button1_Click() 
Dim lngRow As Long 
Dim BotRow As Long 

Cells(Rows.Count, "H").Select 
Selection.End(xlUp).Select 
    BotRow = Selection.Row 
    For lngRow = 1 To BotRow 
     If Not IsEmpty(Cells(lngRow, "H")) Then 
      Cells(lngRow, "C") = Cells(lngRow, "H") 
     End If 

    Next 
End Sub 
+0

是「我試圖將列H中的內容複製到列C只有H列中的單元格爲空。」意思是說「我試圖從列H中的內容複製到列C只有當列H中的單元格是** NOT **空的。」 – YowE3K

+0

或者也許你是「試圖將列H中的內容複製到C列,只有當列** C **中的單元格爲空時。」? – YowE3K

回答

1

vbNullString = "",因此這將選自H傳輸數據至C,僅當有數據:

Sub Button1_Click() 
    Dim lngRow As Long 
    Dim BotRow As Long 

    BotRow = Cells(Rows.Count, "H").End(xlUp).Row 
    For lngRow = 1 To BotRow 
     If Cells(lngRow, "H") <> vbNullString Then 
      Cells(lngRow, "C") = Cells(lngRow, "H") 
     Else 
     End If 
    Next lngRow 
End Sub 
+0

謝謝,這工作! –

+0

@JonC:很高興能幫到你! ;) – R3uK

0
Option Explicit 
Dim i As Long 
Dim n As Long 
Sub Copy() 

n = ActiveSheet.Cells(Rows.Count, "H").End(xlUp).Row 

For i = 1 To n 
    If Cells(i, 8).Value <> vbNullString Then 
     Cells(i, 3).Value = Cells(i, 1).Value 
    End If 
Next i 

End Sub 
相關問題