Good day everyone,
Been struggling on and off with this one for a few days now. It's an extract from a larger piece of code, but the only piece I cannot get to work as intended. The values in N1:P1 will be dynamic and will be matched against the values in A1:J1 (all of this was oversimplified just in order to get the process working after which I will transfer the logic to the original piece of code). If the values in the array in N1:P1 is not found in the range A1:J1 then the entire column in the range A1:J1 should be deleted. It deletes some of the columns but there are columns remaining afterwards that do not form part of the array. Been considering to rather add the column numbers to a new array and then delete the array instead of the current format, but not sure if that will solve the problem.
Anyway, below the code as it stands now.
Any assistance is greatly appreciated.
Been struggling on and off with this one for a few days now. It's an extract from a larger piece of code, but the only piece I cannot get to work as intended. The values in N1:P1 will be dynamic and will be matched against the values in A1:J1 (all of this was oversimplified just in order to get the process working after which I will transfer the logic to the original piece of code). If the values in the array in N1:P1 is not found in the range A1:J1 then the entire column in the range A1:J1 should be deleted. It deletes some of the columns but there are columns remaining afterwards that do not form part of the array. Been considering to rather add the column numbers to a new array and then delete the array instead of the current format, but not sure if that will solve the problem.
Anyway, below the code as it stands now.
VBA Code:
Sub DeleteColumns()
Dim VarArr As Variant
a = Range("M1").Value
VarArr = Array(Range(Cells(1, 14), Cells(1, 14 + a - 1)))
Set TestRng = Range(Cells(1, 1), Cells(1, 10))
For Each cell In TestRng
If IsError(Application.Match(cell.Value, VarArr, 0)) Then
cell.EntireColumn.Delete
End If
Next
End Sub
Any assistance is greatly appreciated.