Sub TestPasteColumnData3()
Dim lastcol As Long
Dim j As Long
With Worksheets("WF - L12 (3)")
lastcol = .Cells(4, Columns.Count).End(xlToLeft).Column
For j = 3 To lastcol
'change >0 to <>0 and 3 to j
If CBool(Application.CountIfs(.Columns(j), "<>0")) Then
.Columns(j).Copy Destination:=Worksheets("Sheet1").Columns(j)
Else
MsgBox ("No Value")
Exit Sub
End If
Next
End With
MsgBox ("Done")
End Sub
Pl make 2 changes suggested above your code will work.
@Niva I am yet to find out basic reason of Countifs or CountA not giving desired results. For your immediate requirements you can use an additional program to delete blanks in Sheet1. Please make it Active Sheet and use the following program.
Sub DeleteBlankColumns()
With Worksheets("Sheet1")
Dim lastColumn As Long
lastColumn = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
'MsgBox lastColumn
Dim lastRow As Long
Dim rng As Range
Set rng = ActiveSheet.Cells
lastRow = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
'MsgBox lastRow
'Step1: Declare your variables.
Dim MyRange As Range
Dim iCounter As Long
'Step 2: Define the target Range.
Set MyRange = ActiveSheet.Range(Cells(1, 1), Cells(lastRow, lastColumn))
'Step 3: Start reverse looping through the range.
For iCounter = MyRange.Columns.Count To 1 Step -1
'Debug.Print iCounter
'Step 4: If entire column is empty then delete it.
Debug.Print Application.CountA(Columns(iCounter).EntireColumn) = 0
If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
Columns(iCounter).Delete
End If
'Step 5: Increment the counter down
Next iCounter
End With
End Sub