0

Hello everyone I have written a line of code that goes down a column and does a vlookup. the results of that function are then placed into an array to later be changed. some of the lookup values are not in the lookup range and thus returns an error. I am trying to place this column in an array, change the values of error to 0 or an empty cell, then replace the original column with the array. here is what i have written but i keep getting a type mismatch error on line:

If myArray(x) = "#N/A" Then myArray(x) = Empty

here is the code in question.

Dim myArray() As Variant
Dim DataRange As Range
Dim cell As Range
Dim x As Long

  Set DataRange = ActiveSheet.UsedRange

  ReDim myArray(DataRange.Cells.Count)

  For Each cell In DataRange.Cells
    myArray(x) = cell.Value
    x = x + 1
  Next cell
  
  
  For x = LBound(myArray) To UBound(myArray)
    If myArray(x) = "#N/A" Then myArray(x) = Empty
  Next X

End Sub

When i print the array the errors's turn into Error 2042 i have tried replacing the if with

if myArray(x) = "Error 2042" then myArray(x) = Empty

but this still returns a type mismatch error

7
  • An error value is not a String. So you can't compare an actual #N/A error to the strings "#N/A" or "Error 2042". See the linked thread for the correct approach. Commented Feb 4, 2022 at 19:43
  • @BigBen thank you for the help. how do i overwrite the original range with the modified array Commented Feb 4, 2022 at 20:00
  • Really there's an easier way to do this using Range.SpecialCells(xlCellTypeErrors), no need for an intermediate array. And even if you were to use an array, it's better to use this approach to create it, modify it, and write it back to the sheet. No need to loop over each cell. Commented Feb 4, 2022 at 20:01
  • 1
    thanks man you are a hero. 1/3rd of my day has been spent working on this. i dont know how to give you points but i would. Commented Feb 4, 2022 at 20:06
  • one final note if this isnt deleted but SpecialCells(xlCellTypeErrors) does not exist checked the microsoft support page and everything its not a special cells method @BigBen Commented Feb 4, 2022 at 20:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.