Copy blank cells from the top "by excluding certain columns"

nelsontan

New Member
Joined
Jul 2, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi, I have the code which able to fill the blank cells by copying the content from the top, basically the vba codes is running for all columns in worksheet, but I would like to exclude certain columns, what should I do with the following codes, for example to exclude column M, N, O.


VBA Code:
'copy data if cell are blank for number value from top to bottom
    Application.ScreenUpdating = False
    
    With Sheets("GFL")
    
    For RowIndex = 1 To LastRowIndex
         For iCntr = 1 To UsedRng.Columns.Count
           If Not IsEmpty(Cells(RowIndex, iCntr).Value) Then
            If IsNumeric(Cells(RowIndex, iCntr).Value) = False Then
                If IsEmpty(Cells(RowIndex + 1, iCntr).Value) Then
                 Cells(RowIndex + 1, iCntr).Value = Cells(RowIndex, iCntr).Value
                End If
            End If
           End If
         Next iCntr
    Next RowIndex
    End With
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try this (untested) :
VBA Code:
Application.ScreenUpdating = False
For RowIndex = 1 To LastRowIndex
    For iCntr = 1 To UsedRng.Columns.Count
        If Not Intersect(Columns(iCntr), [M:O]) Is Nothing Then GoTo nxt
        If Not IsEmpty(Cells(RowIndex, iCntr).Value) Then
            If IsNumeric(Cells(RowIndex, iCntr).Value) = False Then
                If IsEmpty(Cells(RowIndex + 1, iCntr).Value) Then
                    Cells(RowIndex + 1, iCntr).Value = Cells(RowIndex, iCntr).Value
                End If
            End If
        End If
nxt: Next iCntr
Next RowIndex

The With....End With is not doing anything so I've omitted it.

Your code is not very efficient. I am unable to suggest more efficient code without knowing more about your worksheet.
 
Upvote 0
Solution
A slight improvement (but still not very efficient) :
VBA Code:
Application.ScreenUpdating = False
For RowIndex = 1 To LastRowIndex
    For iCntr = 1 To UsedRng.Columns.Count
        If Not IsEmpty(Cells(RowIndex, iCntr).Value) Then
            If IsNumeric(Cells(RowIndex, iCntr).Value) = False Then
                If IsEmpty(Cells(RowIndex + 1, iCntr).Value) Then
                    Cells(RowIndex + 1, iCntr).Value = Cells(RowIndex, iCntr).Value
                End If
            End If
        End If
        If iCntr = 12 Then iCntr = 16
   Next iCntr
Next RowIndex
 
Upvote 0
A bit more efficient :
VBA Code:
Dim rng As Range, cel As Range
Set rng = Union([A:L], Range(Columns(16), Columns(Columns.Count)))
Set rng = Intersect(rng, ActiveSheet.UsedRange).Offset(1).SpecialCells(xlCellTypeBlanks)
Application.ScreenUpdating = False
For Each cel In rng
    If Not IsNumeric(cel(0)) Then cel = cel(0).Value
Next
 
Upvote 0
Try this (untested) :
VBA Code:
Application.ScreenUpdating = False
For RowIndex = 1 To LastRowIndex
    For iCntr = 1 To UsedRng.Columns.Count
        If Not Intersect(Columns(iCntr), [M:O]) Is Nothing Then GoTo nxt
        If Not IsEmpty(Cells(RowIndex, iCntr).Value) Then
            If IsNumeric(Cells(RowIndex, iCntr).Value) = False Then
                If IsEmpty(Cells(RowIndex + 1, iCntr).Value) Then
                    Cells(RowIndex + 1, iCntr).Value = Cells(RowIndex, iCntr).Value
                End If
            End If
        End If
nxt: Next iCntr
Next RowIndex

The With....End With is not doing anything so I've omitted it.

Your code is not very efficient. I am unable to suggest more efficient code without knowing more about your worksheet.
Thanks for the suggestion, it's works. It's kinda hard to explain all the scenario that in the worksheet due to the format of report generated from the system (e.g. a lot of blank column, rows, and need to fill in blank cells from top/right, and etc). I appreciate the help by the way. :biggrin: ;)
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,547
Members
449,089
Latest member
davidcom

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