/ /範囲内の空白またはゼロの列を削除します-excel、vba、excel-vba

範囲内の空白またはゼロの列を削除する - excel、vba、excel-vba

これが私に必要なものです:範囲は列F:F、G:G、K:Kを言います。これらの特定の列についてのみセルがゼロまたは空白の場合は、列全体を削除します。他の値が含まれている場合、アクションはありません。

以下は私が現在使用しているものです。ただし、これにより、削除してはならない他の列が削除されます。

LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
LastColumn = Sheets("Sheet1").Cells(9, Columns.Count).End(xlToLeft).Column
lastcol_name_1 = Replace(Cells(1, LastColumn).Address(False, False), "1", "")

"to find letter of last column

With Sheets("Sheet1")
For i = 1 To LastColumn
lastcol_name_1 = Replace(Cells(1, i).Address(False, False), "1", "") "to find letter of last column
If(lastcol_name_1=“K” or lastcol_name_1=”L” or lastcol_name_1=”M”) then ‘write the column name here
If Evaluate("sum(countif(" & .Range(.Cells(10, i), _
.Cells(LastRow, i)).Address & ",{"""",0}))") = LastRow - 10 + 1 Then _
.Columns(i).Delete
Endif
Next i

回答:

回答№1は0

私があなたを正しく理解しているなら、次の列「F」、「G」、「K」が空の場合は削除し、残りの列を左にシフトする必要があります。これがここで実行したいことだと仮定します。独自のコードをできるだけ多く残しました。できる限り:

Sub main():

LastColumn = Sheets("Sheet1").Cells(9, Columns.Count).End(xlToLeft).Column
lastcol_name_1 = Replace(Cells(1, LastColumn).Address(False, False), "1", "")

For i = LastColumn To 1 Step -1
lastcol_name_1 = Replace(Cells(1, i).Address(False, False), "1", "")
If (lastcol_name_1 = "F" Or lastcol_name_1 = "G" Or lastcol_name_1 = "K") Then
If Application.CountA(Columns(i).EntireColumn) = 0 Then
Columns(i).Delete Shift:=xlToLeft
End If
End If
Next i

End Sub