I need a program that analyzes the information inside of an array. There are 2 worksheets, “CleanDATA” and “iTOAcolumns”, in iTOAcolumns there is a table that contains 3 columns: how we named the column, the letter (A, B,C…, AF,AG, etc.) and a “Keep” column that either can be blank or have an X (meaning that we need to keep that item). That array get its information from the table in iTOAcolumns that determines which columns from “CleanDATA” are deleted (image attached). When you run the macro, the columns that are blank are to be deleted (from the CleanDATA worksheet) and moved to the left to leave no spaces between columns. I want to use an array because I don’t want to move between sheets anymore.
This is my code
Sub Macro6()
' Macro6 Macro
Dim DirArray As Variant
Dim curCOL As String
Dim i As Integer
Dim targetSheet As Worksheet
' Set target sheet
Set targetSheet = Sheets("iTOAcolumns") ' Replace with your actual sheet name if different
DirArray = targetSheet.Range("KEEPcols").Value ' Get values from KEEPcols in iTOAcolumns sheet
' Loop through each element in the DirArray
For i = LBound(DirArray, 1) To UBound(DirArray, 1) Step 1
curCOL = DirArray(i, 1) ' Get the column letter from the array
' Check if the column exists
If Columns(curCOL).Count > 0 Then
' Go to the cell below in column D (assuming criteria)
targetSheet.Cells(i + 1, 4).Select ' Select the cell for checking
' Check if the cell to the right is empty
If ActiveCell.Offset(0, 1).Value = "" Then
' Delete the column using the column letter from the array
Columns(curCOL).EntireColumn.Delete Shift:=xlToLeft
End If
End If
Next i
End Sub
The problems that I am facing is that when reviewing the code using F8, the macro does go through the list, but does not delete any of the desired columns. I don’t know if the bounds are incorrect or if the loop is incorrect. Also, is important to say that the iTOAcolumn list is listed in ascending order, AJ, AI, AH… first, then C, B, A, at last. HELPPPPP
fedemorado is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.