Home » excel » excel – Do if column is empty instead of first cell

excel – Do if column is empty instead of first cell

Posted by: admin May 14, 2020 Leave a comment

Questions:

The code at the bottom checks to see if cell (1,b) is empty and then copies in a column and performs a text to columns on it. However I would like it to check if column b is empty instead of just this first cell. It should be straightforward but any changes I have made have not worked. For example I have tried:

If Excel.WorksheetFunction.CountBlank(Excel.Sheets("TOP LINE").Columns(b)) <> 1048576 Then
b = b+1

Instead of:

If Cells(1, b) <> "" Then b = b + 1

Please Help!

For a = 2 To 60

If Excel.WorksheetFunction.CountBlank(Excel.Sheets("Paste In").Columns(a)) <> 1048576 Then

    Excel.Sheets("Paste In").Columns(a).Copy

    b = Excel.Sheets("TOP LINE").Cells(1, Columns.Count).End(Excel.xlToLeft).column

    Excel.Sheets("TOP LINE").Select


    If Cells(1, b) <> "" Then b = b + 1


        Excel.Sheets("TOP LINE").Columns(b).EntireColumn.Select
        Excel.ActiveSheet.Paste

        Excel.Application.CutCopyMode = False
        Selection.TextToColumns Destination:=Cells(1, b), DataType:=xlDelimited, _
                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
                Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
                :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
                Array(7, 1), Array(8, 1)), TrailingMinusNumbers:=True

End If

Next a

How to&Answers:

You can use the worksheet function to count nonempty cells in a column with column index a:

If Application.WorksheetFunction.CountA(Excel.Sheets("Paste In").Columns(a)) > 0 Then

you have both a as b in your code, please use this code for both instances:

line 2 (If Excel.WorksheetFunction.CountBlank(Excel.Sheets("Paste In").Columns(a)) <> 1048576 Then) becomes:

If Application.WorksheetFunction.CountA(Excel.Sheets("Paste In").Columns(a)) > 0 Then

line 6 (If Cells(1, b) <> "" Then b = b + 1) becomes:

If Application.WorksheetFunction.CountA(Excel.Sheets("TOP LINE").Columns(b)) > 0 Then