filling of cells using vba with simple criteria

rehanemis

Board Regular
Joined
Aug 15, 2016
Messages
50
Hi,

I have an sheet that contains 1000 records. In the sheet I would like to fill the empty cell using criteria.

For example: A4 contains value B and A10 contains Value C. So I would like to put H from A4 to A10, similarly A34 contains value B and A50 contains C so I would like to fill between cells with H.

The total records are more than 1000,

Any suggestion?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this:
Code:
Sub Fill_Blanks_In_Column_A()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 4 To Lastrow
        If Cells(i, 1).Value = "" Then Cells(i, 1).Value = "H"
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Yes, H or any letter in blank cells between range(as I mentioned in my post)
Give this macro a try...
Code:
Sub FillBlanksWithLetterH()
  Dim LastRow As Long
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  Range("A[B][COLOR="#FF0000"]2[/COLOR][/B]:A" & LastRow).SpecialCells(xlBlanks).Value = "H"
End Sub
Note: I assumed Row 1 was a header and your data started in Row 2.
 
Last edited:
Upvote 0
Thanks, but it filling all empty cells which i don't want. I only want cells to be filled between B and C value. like A7 contains B and A34 Contains C so I would like to fill empty cells from A7 to A34 and so on for others.
 
Upvote 0
Thanks, but it filling all empty cells which i don't want. I only want cells to be filled between B and C value. like A7 contains B and A34 Contains C so I would like to fill empty cells from A7 to A34 and so on for others.
Is your B and C values the actual letters "B" and "C" or are they just stand-in letters for some other value (such a number or text phrase)?
 
Upvote 0
these are stand alone values "B" and "C"
Okay, does this macro do what you want...
Code:
[table="width: 500"]
[tr]
	[td]Sub FillBlanksWithLetterH()
  Dim R As Long, OnOff As Boolean, Data As Variant
  Data = Range("A1", Columns("A").Find("C", , xlValues, xlWhole, xlRows, xlPrevious, False, , False))
  For R = 1 To UBound(Data)
    If InStr(1, " B C ", " " & Data(R, 1) & " ", vbTextCompare) Then OnOff = UCase(Data(R, 1)) = "B"
    If OnOff Then
      If Data(R, 1) = "" Then Data(R, 1) = "H"
    End If
  Next
  Range("A1").Resize(UBound(Data)) = Data
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,838
Messages
6,121,885
Members
449,057
Latest member
Moo4247

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