Filling multiple columns witha loop

Brucea

New Member
Joined
May 25, 2007
Messages
40
I have the following code to fill in cell in column B. It works fine but I have a 4 other columns (Columns C,G,H,N) where I need to fill in the same information (whatever the contents of the first cell in that column is). There must be a more efficient way to fill than to copy the code 4 times. The columns will not change.

Public Sub CountColorBlanks()
Dim CurrentCell As String, StyleRowsCount As Integer, ColorRowsCount As Integer
Dim TotalRows As Integer, StyleName As String
Sheets("Raw Data").Select
Range("B1").Select
' initialize Variables
StyleName = ActiveCell.Value
CurrentCell = ActiveCell.Value
' Get the total Number of rows
StyleRowsCount = Range("C" & Rows.Count).End(xlUp).Row
ColorRowsCount = Range("AA" & Rows.Count).End(xlUp).Row
If StyleRowsCount > ColorRowsCount Then
TotalRows = StyleRowsCount
Else
TotalRows = ColorRowsCount
End If
' Check if we are at the end of the list
Do Until TotalRows = ActiveCell.Row
'blank rows get the PRODUCT TYPE name filled in, when the style name changes the loopstarts over
If CurrentCell = "" Then
ActiveCell.Value = StyleName
Selection.Offset(1).Select 'moves to next cell down
CurrentCell = Selection.Value
Else
StyleName = ActiveCell.Value
Selection.Offset(1).Select 'moves to next cell down
CurrentCell = Selection.Value
End If

Loop
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi,

I added an array to your code.
This should(not tested) loop thru each column that you specified.
There is 1 line of you code I'm not sure it it is to be changed I indicated this with <<<<.

Code:
Public Sub CountColorBlanks()
Dim MyArray As Variant
Dim CurrentCell As String, StyleRowsCount As Integer, ColorRowsCount As Integer
Dim TotalRows As Integer, StyleName As String
Dim i As Long
MyArray = Array("B", "C", "G", "H", "N")
Sheets("Raw Data").Select
For i = 0 To UBound(MyArray)
    Range(MyArray(i) & "1").Select
' initialize Variables
    StyleName = ActiveCell.Value
    CurrentCell = ActiveCell.Value
' Get the total Number of rows
    StyleRowsCount = Range("C" & Rows.Count).End(xlUp).Row'<< always C??
    ColorRowsCount = Range("AA" & Rows.Count).End(xlUp).Row
    If StyleRowsCount > ColorRowsCount Then
        TotalRows = StyleRowsCount
    Else
        TotalRows = ColorRowsCount
    End If
' Check if we are at the end of the list
    Do Until TotalRows = ActiveCell.Row
'blank rows get the PRODUCT TYPE name filled in, when the style name changes the loopstarts over
        If CurrentCell = "" Then
            ActiveCell.Value = StyleName
            Selection.Offset(1).Select 'moves to next cell down
            CurrentCell = Selection.Value
        Else
            StyleName = ActiveCell.Value
            Selection.Offset(1).Select 'moves to next cell down
            CurrentCell = Selection.Value
        End If

Loop
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,879
Messages
6,122,065
Members
449,064
Latest member
scottdog129

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