Macro to move or copy cells when empty

NArawna

New Member
Joined
Dec 5, 2018
Messages
10
[FONT=&quot]Hi, I have a table created from some CSV data that looks like this[/FONT]
hambendave
eggmark
jamessam
cheesefrankrob

<thead style="margin-left: 0px; margin-right: 0px; margin-top: 0px;">
[TH="align: left"]Foo[/TH]
[TH="align: left"]Bar[/TH]
[TH="align: left"]text[/TH]

</thead><tbody style="margin-left: 0px; margin-right: 0px; margin-bottom: 0px;">
</tbody>
[FONT=&quot]I would like to it to look like this[/FONT]
hambendave
eggbenmark
eggjamessam
cheesefrankrob

<thead style="margin-left: 0px; margin-right: 0px; margin-top: 0px;">
[TH="align: left"]Foo[/TH]
[TH="align: left"]Bar[/TH]
[TH="align: left"]text[/TH]

</thead><tbody style="margin-left: 0px; margin-right: 0px; margin-bottom: 0px;">
</tbody>
[FONT=&quot]This seems to be 3 operations that need to occur[/FONT]
[FONT=&quot]1, find all cells in Foo (A) that are empty and COPY the value from the cell above[/FONT]
[FONT=&quot]2, find all cells in text (C) that a empty and MOVE the value from the cell to its left[/FONT]
[FONT=&quot]3, find all cells in Bar (B) that are empty and COPY the value from the cell above[/FONT]
[FONT=&quot]Could someone give me some advice regarding this?[/FONT]
[FONT=&quot]Thank you.[/FONT]
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
This is what I have so far


Code:
Sub ProjectFillDown()Range("a1", Range("a65536").End(xlUp)).Select
Dim xRng As Range
Dim xRows As Long, xCols As Long
Dim xRow As Integer, xCol As Integer
Set xRng = Selection
xCols = xRng.Columns.CountLarge
xRows = xRng.Rows.CountLarge
For xCol = 1 To xCols
  For xRow = 1 To xRows - 1
    If xRng.Cells(xRow, xCol) <> "" Then
      xRng.Cells(xRow, xCol) = xRng.Cells(xRow, xCol).Value
      If xRng.Cells(xRow + 1, xCol) = "" Then
        xRng.Cells(xRow + 1, xCol) = xRng.Cells(xRow, xCol).Value
      End If
    End If
  Next xRow
Next xCol
End Sub
'---------------------

Sub StudentMoveAcross()
Range("b1", Range("b65536").End(xlUp)).Select


End Sub

'---------------------
Sub StaffFillDown()
Range("b1", Range("b65536").End(xlUp)).Select
Dim xRng As Range
Dim xRows As Long, xCols As Long
Dim xRow As Integer, xCol As Integer
Set xRng = Selection
xCols = xRng.Columns.CountLarge
xRows = xRng.Rows.CountLarge
For xCol = 1 To xCols
  For xRow = 1 To xRows - 1
    If xRng.Cells(xRow, xCol) <> "" Then
      xRng.Cells(xRow, xCol) = xRng.Cells(xRow, xCol).Value
      If xRng.Cells(xRow + 1, xCol) = "" Then
        xRng.Cells(xRow + 1, xCol) = xRng.Cells(xRow, xCol).Value
      End If
    End If
  Next xRow
Next xCol
End Sub

So I can move the value of column A down, I am still stuck on moving the value of column B across when C is empty. I can then reuse the first bit of code to finally move column B down.
 
Upvote 0
I cannot edit my posts? So I am sorry for the triple post, this is my progression towards moving of the cells based on code I found elsewhere.

Code:
Sub StudentMoveAcross()
    Dim row As Long


    For row = 2 To 200
        ' Check if "save" appears in the value anywhere.
        If Range("B" & row).Value Like "(B" Then
            ' Copy the value and then blank the source.
            Range("C" & row).Value = Range("B" & row).Value
            Range("B" & row).Value = ""
        End If
    Next


End Sub

I realized that the 2nd step of moving all cells from B to C if C is empty could be simplified,
as any cell in B that contains a "(" will allays have an empty cell next to it that it can be moved into.

I have been unsuccessful in getting the code to work however, it loops but never moves any of the cells.
 
Upvote 0
Try
Code:
Sub NArawna()
   Dim Lr As Long
   Dim Rng As Range
   Lr = Cells.find("*", , , , xlByRows, xlPrevious, , , False).Row
   On Error Resume Next
   With Range("C2:C" & Lr)
      Set Rng = .SpecialCells(xlBlanks)
      Rng.FormulaR1C1 = "=rc[-1]"
      .Value = .Value
      Rng.Offset(, -1).ClearContents
   End With
   With Range("A2:B" & Lr)
      .SpecialCells(xlBlanks).FormulaR1C1 = "=r[-1]c"
      .Value = .Value
   End With
   On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
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