Use VBA to copy cell into next blank cell continuously

GMC The Macro Man

Board Regular
Joined
Mar 23, 2023
Messages
64
Office Version
  1. 2021
Platform
  1. Windows
Hi there
I have a daily report that I need to run a Macro on however some of the detail in Column D (Ticket) is missing. What I need is the Macro to look at Column D only and if there is a BLANK cell then to copy the Cell above that has Data.
So Cells D3 & D4 will become Ticket -806, Cells D6 & D7 will become Ticket -804, D9 will become Ticket -802, D14 & D15 will become Ticket -803 and so on until the end of the column. Once that is done I can then continue with my Macro.
Any help would be appreciated
Thanks in Advance

1683032965383.png
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Sorry I should have added one caveat to this. If Cell D3 is empty, then it has to check if Cell C3 has data, if so then copy the Ticket Number in to D3. If Cell C3 was BLANK, then the MACRO should stop. This would be important when it came to the last cell in column D with data so it would stop if there was no data on the cell to its immediate left
 
Upvote 0
Try this code:
VBA Code:
Sub MyFillBlanks()

    Dim lr As Long
    
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Assign formulas to blank cells in column D
    Range("D1:D" & lr).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    
'   Convert formulas to hard-coded values
    Range("D1:D" & lr).Value = Range("D1:D" & lr).Value
    
End Sub
 
Upvote 0
Use a macro to merge cells and set a shortcut to be faster
I would HIGHLY recommend NOT doing that!
Merge cells are an absolute disaster and should be avoided whenever possible!
Most experienced users will never use them, due to all of the issues that they cause for things like VBA, sorting, etc.
 
Upvote 0
Here is an updated version of the code that makes sure that there is actual data on the sheet, and ignores any errors if there are not any blanks:
VBA Code:
Sub MyFillBlanks()

    Dim lr As Long
   
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
   
'   Confirm that there is data
    If lr < 2 Then Exit Sub
   
    On Error Resume Next
'   Assign formulas to blank cells in column D
    Range("D2:D" & lr).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
   
'   Convert formulas to hard-coded values
    Range("D2:D" & lr).Value = Range("D1:D" & lr).Value
    On Error GoTo 0
   
End Sub
 
Last edited:
Upvote 1
Solution
I would HIGHLY recommend NOT doing that!
Merge cells are an absolute disaster and should be avoided whenever possible!
Most experienced users will never use them, due to all of the issues that they cause for things like VBA, sorting, etc.
What happens its the tickets does reference for more than one line, this is the reason I recommended that.
 
Upvote 0
Merged cells are an abomination & should be avoided like the plague. Recommending people use them is madness.
 
Upvote 0
Upvote 0
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,068
Members
449,091
Latest member
remmuS24

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