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 2 Änderungen über Ihr Code funktioniert vorgeschlagen machen.
@Niva Ich bin noch herauszufinden, grundlegenden Grund für Countifs oder CountA nicht die gewünschten Ergebnisse zu geben. Für Ihre unmittelbaren Anforderungen können Sie ein zusätzliches Programm verwenden, um Leerzeichen in Sheet1 zu löschen. Bitte machen Sie es Active Sheet und verwenden Sie das folgende Programm.
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
Der Code wird immer noch nicht ausgeführt. – Niva
Was bekommen Sie? Erreicht Ihr Code diese Zeile? – dePatinkin
Hallo Es tut mir leid, es funktioniert jetzt, aber es fügt immer noch Spalten, die keine Werte> 0 – Niva