Skip non empty cells to paste special data
I want to copy data from range (a3:M3) in worksheet "SL" to range (a3:m3) in worksheet "EL" only if range (a3:M3) is empty. Else to copy the selected data to the next row (a4:m4).
Below is the code i tried to work out ..but its not working...plz help
Range("C9:G10").Select
Selection.Copy
Sheets("EL").Select
For n = 1 To n = 100
If Cells(n, 2).Value <> "" Then
Cells(n + 1, 2).Select
Selecti开发者_开发百科on.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next n
There are some points in your code I do not understand:
- why does it refer to
C9:G10
? - Why is there a loop from
n=1 to 100
? - The syntax
For n = 1 To n = 100
does not work as you might expect -> replace it withFor n = 1 To 100
.)
Here is my solution to your problem:
Sub copyRange()
' Look if destination cells are empty
Dim isempty As Boolean
isempty = True
For Each cell In Sheets("EL").Range("a3:m3").Cells
If cell.Value! = "" Then isempty = False
Next
' Copy content from SL to EL into the correct line
Sheets("SL").Range("a3:m3").Select
Selection.Copy
If isempty Then
Sheets("EL").Range("a3:m3").PasteSpecial Paste:=xlPasteValues
Else
Sheets("EL").Range("a4:m4").PasteSpecial Paste:=xlPasteValues
End If
End Sub
精彩评论