VBA to fill many ranges of empty cells in columns A and B with different cell values

Armien7

New Member
Joined
Nov 18, 2021
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I hope everyone is having a wonderful new year so far. I'm really struggling with a macro I'm trying to build and I hope I can get some advice here :)

I have a sheet (called: "Motors") with 24720 rows. I'm trying to fill empty ranges with different intervals in column A and B with the values of the last filled cells of each range in column A & B. So for example: Range (A16, B16 and C16 : A27, B27 and C27) is empty, and I want to have it filled with the cell values of A15, B15 and C15, etc. Since I have 20.000 rows+, it takes a lot of time to do it manually.

The big problem I have is that each empty range differs from the previous one (one may containt more empty rows than the other one). For clarification I have made the following screenshots:

Hopefully I can get some tips and new insights :)

Best regards,
Armien
 

Attachments

  • Filling empty ranges in Excell.png
    Filling empty ranges in Excell.png
    28.9 KB · Views: 25
  • Filling empty ranges 2.png
    Filling empty ranges 2.png
    37.5 KB · Views: 25

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
How about
VBA Code:
Sub Armien()
   With Sheets("Motors")
      With .Range("A1" & .Range("C" & Rows.Count).End(xlUp))
         .SpecialCells(xlBlanks).FormulaR1C1 = "=r[-1]c"
         .Value = .Value
      End With
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub Armien()
   With Sheets("Motors")
      With .Range("A1" & .Range("C" & Rows.Count).End(xlUp))
         .SpecialCells(xlBlanks).FormulaR1C1 = "=r[-1]c"
         .Value = .Value
      End With
   End With
End Sub
Hi Fluff! Thank you so much for your fast response and idea. Sadly I get the following error: With .Range("A1" & .Range("C" & Rows.Count).End(xlUp))
 

Attachments

  • Error message 1.png
    Error message 1.png
    142.8 KB · Views: 18
  • Error message 2.png
    Error message 2.png
    21.9 KB · Views: 19
Upvote 0
How about
VBA Code:
Sub Armien()
   With Sheets("Motors")
      With .Range("A1" & .Range("C" & Rows.Count).End(xlUp))
         .SpecialCells(xlBlanks).FormulaR1C1 = "=r[-1]c"
         .Value = .Value
      End With
   End With
End Sub

[/QUOTE]

Oops, replace the & with a comma.
Once again many thanks Fluff! :)

The macro does the first empty range (AC15 : AC26) but unfortunately stops there. When I click on the run button again, I get the following error: .SpecialCells(xlBlanks).FormulaR1C1 = "=r[-1]c"
 

Attachments

  • After running the macro.png
    After running the macro.png
    66.3 KB · Views: 16
  • Before running the macro.png
    Before running the macro.png
    47.5 KB · Views: 18
  • Error after succesful first run.png
    Error after succesful first run.png
    22 KB · Views: 18
Upvote 0
It stops at row 27 because that's the last row in the sheet. If you want that row copied down, how far down should it be copied?
 
Upvote 0
It stops at row 27 because that's the last row in the sheet. If you want that row copied down, how far down should it be copied?
The last row where it should be copied to is row: 24720.
 

Attachments

  • Macro last row, 24720.png
    Macro last row, 24720.png
    100.2 KB · Views: 14
Upvote 0
Ok, how about
VBA Code:
Sub Armien()
   With Sheets("Motors").Range("A1:C24720")
      On Error Resume Next
      .SpecialCells(xlBlanks).FormulaR1C1 = "=r[-1]c"
      On Error GoTo 0
      .Value = .Value
   End With
End Sub
 
Upvote 0
Thank you so much for your inputs Fluff! This time I never get an error when running the macro (even when spam cliking my macro button). But just as before it stops at row 27 (which is correct), but then it does't copy row 28 and past it to the next empty range. I hope I'm clear with my explanation, one again many thanks for helping me :)
 

Attachments

  • After running the macro.png
    After running the macro.png
    66.3 KB · Views: 13
Upvote 0
Ok, how about
VBA Code:
Sub Armien()
   With Sheets("Motors").Range("A1:C24720")
      .Offset(24719).Resize(1, 1).Value = 1
      .Offset(24719).Resize(1, 1).Value = ""
      On Error Resume Next
      .SpecialCells(xlBlanks).FormulaR1C1 = "=r[-1]c"
      On Error GoTo 0
      .Value = .Value
   End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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