1

I seem to have a strange problem as some ghost values have entered my file. I got this file from someone but looks like file has seen several deletion, copy pastes etc. Please see attached image.

enter image description here

It shows ghost values in cell J186 and the values returned by various IS*** functions on cell j186. Such values are there in several columns in the file and I am sure they are consuming a lot of Filesize and the file is crashing every now and then. The file is 100 MB.

For example, when I select any cell in column L say Cell L56 and press Ctrl+Down, the cursor gets stuck in the cell L186 even when there is no value. If I select the cells L3:L186 and manually enter delete, something gets deleted (I cant see) and then the range functions as a normal range (i.e. If i select any random cell in that range and do a Ctrl+Down, it goes to the last row in Excel Row 1048576) Any cell in the range upto L186 shows the same behaviour as cell J186.

Is there a way to write a VBA code to identify such cells and clear contents of such cells? Thanks in advance.

Community
  • 1
  • 1
Peekay
  • 238
  • 11
  • 21
  • What if you try to use `Debug.Print` on those cells? What is the result? – sam092 Jan 30 '14 at 03:57
  • Is it possible that there is a space or that there are multiple spaces in these "ghost" cells? – DeanBDean Jan 30 '14 at 04:38
  • Hey Sam092 and DeabBDean. Thank you both for reading my post. I think the problem seems to be that they contain "". I have written a code to delete the ""'s and hope it will fix it. I will post the code once the code is successful. This file was sent to me by someone and I have no clue how he managed to put so many ""'s . Looks like an if statement was used to put "" and then all the cells were copypasted as values. – Peekay Jan 30 '14 at 04:43
  • I once inherited a file with similar problems. The source was that the user preferred to clear out cells by hitting the spacebar instead of delete or backspace. It caused all kinds of problems and was quite annoying. – DeanBDean Jan 30 '14 at 05:04
  • Also, wondering if its the non-breaking space - CHAR(160) issue. See http://stackoverflow.com/questions/9578397/how-to-remove-leading-or-trailing-spaces-in-an-entire-column-of-excel-worksheet/9582919#9582919 – brettdj Jan 30 '14 at 09:16

2 Answers2

1

Yes, there is something strange here ..... zero length cells that are not actually blank (when tested with SpecialCells(xlBlank)

On your sample file =CODE(A117) returns #VALUE. Yet the cell is not blank

This array based code provides a very quick way of turning the cells to truely blank

Sub QuickReplace()
Dim rng1 As Range
Dim X
Dim lngRow As Long
Dim lngCol As Long

ActiveSheet.UsedRange

X = ActiveSheet.UsedRange.Value2
For lngRow = 1 To UBound(X, 1)
    For lngCol = 1 To UBound(X, 2)
        If Len(X(lngRow, lngCol)) = 0 Then X(lngRow, lngCol) = vbNullString
    Next
Next
ActiveSheet.UsedRange.Value2 = X

End Sub
brettdj
  • 54,857
  • 16
  • 114
  • 177
  • 1
    Wow!! I thought this will take atleast 2 hours going cell by cell. It hardly took 2 minutes to clear the range. Fantastic!!!! Thanks a tonne!!! – Peekay Jan 30 '14 at 13:03
  • I used to for ranges smaller than Usedrange and it works like a charm. Thanks a lot. – Peekay Jan 31 '14 at 03:21
  • Sorry, my earlier post is misleading. I am not able to get it work for some files. For one file, it worked while for others it is giving out of memory error. – Peekay Feb 04 '14 at 03:47
  • For your problematic files or all files? A file as large as the one you mentioned I initially is likely to be corrupt. What modifications did you make to alppy it to *ranges smaller than the UsedRange*? – brettdj Feb 04 '14 at 07:19
  • Thanks for the continued interest. To answer your question, I have only problematic files. :-). But I have managed a workround and I am posting the code that finally worked. Many thanks for setting me off in the right direction. – Peekay Feb 04 '14 at 12:51
0

This code was successful. But the file size did not decrease much.

Sub cleancolumns()
Dim i As Integer
Dim j As Integer
Dim Rng As Range

j = 1

Do While j < 5010
Set Rng = Range(Cells(5, j), Cells(186, j))
If WorksheetFunction.Sum(Rng) = 0 Then
    Rng.Select
    Selection.ClearContents
    j = j + 1
Else
    j = j + 1
End If
Loop

ActiveWorkbook.Save

End Sub

There are a million cells in all, also counting the above ranges where full range is "". searching each cell one by one is very slow. Hence I did the above workaround.

The above code checks for the sum of the range and if the sum of the range is zero it is assumed to contain ""'s and clears contents. Else it skips the column and checks for the next column.

However, this does not remedy a situation where there are few genuine values and the rest are ""'s. These also have to be taken into account in a separate If statement i guess. That will make it very slow but doing this appears to be unavoidable.


Update based on Brettdj's response


The following variant of Brettdj's code worked. usedrange appeared to be larger than what my 6GB computer could handle. So I broke the data chunk by chunk to avoid "Out of memory" error. Also there were some error values which had to be removed before the Len function was applied. Now the file sizes have shrunk by a third (mainly by replacing 0's by blanks - there were too many). Thankfully the ghosts seem to have been busted.

Sub QuickReplace1()
Dim rng1 As Range
Dim X As Variant
Dim lngRow As Long
Dim lngCol As Long

' took no more than 500 columns at a time not to risk file crashing. Changed the values      manually to clear chunk by chunk
Set rng1 = Range(Cells(1, 3501), Cells(7500, 4000))

X = rng1.Value2
For lngRow = 1 To UBound(X, 1)
    For lngCol = 1 To UBound(X, 2)
        If IsError(X(lngRow, lngCol)) Then X(lngRow, lngCol) = vbNullString
        If X(lngRow, lngCol) = 0 Then X(lngRow, lngCol) = vbNullString
        If Len(X(lngRow, lngCol)) = 0 Then X(lngRow, lngCol) = vbNullString
    Next
Next

rng1.Value2 = X

End Sub
Peekay
  • 238
  • 11
  • 21
  • Siddharth. Thanks. I can upload a sample of the file. How can I upload a file here? I copy pasted some ghost-ridden range to another sample worksheet and the ghosts promptly carried over. I can upload the sample file. – Peekay Jan 30 '14 at 10:03
  • you can upload it in www.wikisend.com and share the link here – Siddharth Rout Jan 30 '14 at 10:04
  • How big is the original file? I would like to see the original file so I can ascertain the exact reason for the huge size. – Siddharth Rout Jan 30 '14 at 10:05
  • I tried converting the file sheet by sheet into csv and compared the total size of csv files with that of the original file. The total is in the same range as the original file. The reason is huge data. So I first cleaning the data and then replacing the long formulae with several nested ifs with VBA macros. Not sure I can share the original size. Thanks anyway. – Peekay Jan 30 '14 at 10:32
  • There are about 10 million formulae where length of each formula is more than 200 characters. – Peekay Jan 30 '14 at 10:59