Copy next Cell when cell is blank

sitzalke

New Member
Joined
May 13, 2019
Messages
11
Hi. I am beginner to excel vba. I would like to ask simple question that I've been stuck for few days.
So far, I have two columns, for example column L and M.
I have to loop all column L and find the blank cell. If there are a blank cell, then, it will copy next value in column M.
Roughly, the column is like this,
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Column L[/TD]
[TD]Column M[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]4[/TD]
[/TR]
</tbody>[/TABLE]

so supposedly second row of column L will be 4. The flow will be until end of the lastrow.
So far, my code is like this,
Code:
  Dim c As Range    Dim searchrange As Range
    Dim i As Long


    Set searchrange = Range("L2", Cells(Rows.Count, 1).End(xlUp))


    For i = searchrange.Cells.Count To 1 Step -1
        Set c = searchrange.Cells(i)
        If c.Value = "" Then
        'c.Value.Copy Range("M2")
       c.Value.Copy Destination:=Range("M")
        End If
    Next i

Thanks in advance
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
looks a little like an assignment. Is that ALL of the question ?
And what happens when M gets to zero, does it go into negative numbers ?
 
Upvote 0
Assuming no blanks in col M over the range of data in col L:
Code:
Sub Sitzalke()
Dim R As Range, Rblank As Range, c As Range
Set R = Range("L2", Cells(Rows.Count, "L").End(xlUp))
On Error Resume Next
Set Rblank = R.SpecialCells(xlCellTypeBlanks)
If Rblank Is Nothing Then
    MsgBox "No blank cells in col L)"
    Exit Sub
End If
Application.ScreenUpdating = False
For Each c In R.SpecialCells(xlCellTypeBlanks)
    c.Value = c.Offset(1, 1).Value
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 0
OR

Code:
Sub MM1()
Dim cell As Range
For Each cell In Range("L2", Cells(Rows.Count, 12).End(xlUp))
    If cell.Value = "" Then cell.Offset(, 1).Value = cell.Offset(-1, 1).Value - 1
    Next cell
End Sub
 
Upvote 0
Assuming no blanks in col M over the range of data in col L:
Code:
Sub Sitzalke()
Dim R As Range, Rblank As Range, c As Range
Set R = Range("L2", Cells(Rows.Count, "L").End(xlUp))
On Error Resume Next
Set Rblank = R.SpecialCells(xlCellTypeBlanks)
If Rblank Is Nothing Then
    MsgBox "No blank cells in col L)"
    Exit Sub
End If
Application.ScreenUpdating = False
For Each c In R.SpecialCells(xlCellTypeBlanks)
    c.Value = c.Offset(1, 1).Value
Next c
Application.ScreenUpdating = True
End Sub

Thanks this is working. A lot. May God Bless you for this kindness. Thanks again
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top