Hi,
I will post the code to solve your problem in a few minutes. Since you are interested in learning, I have the following suggestions on how to improve your existing code.
1. Use of 'On Error Resume Next'. A blanket use of this statement will ignore actual errors and mask problems in the code. There are legitimate reasons for using the statement, but usually followed by your code checking for errors and processing them (or ignoring them) as required.
2. You already have a value in those cells (image name). The LinkedCell will overwrite the contents of the cell, and destroy the image name.
3. As written, 'c & ToRow' evalute to "52" which is an illegal Range. In the current context, the column number must be converted to a character first, so 'c & ToRow' evalutes to "E2". There is a function in the code in the next post that does that.
4. I couldn't get this to work properly (always returned 52, when I knew the answer was 7).
My favorite constructions for finding the number of rows/columns used is:
I like this construction because it seems to work for me especially after the number of rows (and/or columns) has been changed. I have used other constructions similar to yours with varying degrees of success.
----------------
5. exists = InStr(Cells(iColumn), "unknown") = 0. I'm not sure what you are trying to do here with the two equal signs. What VBA does is evaluate the first two expressions and if they are the same, the result is 'True' (boolean) and if they are not the same they evaluate to 'False'. Then the line evaluate to 'True' if the 'True' (or 'False') value equals 0. This can never happen in my version of Excel (2003), because Microsoft assigns a value of 1 to 'True' and -4146 to 'False'.
6. Else: Columns(iColumn).ColumnWidth = 25.71. Use of the colon to put multiple statements on one line (in my opinion) is a very poor construction (although it is perfectly legal) and should be avoided.
-----------
7. Use of this construction inside a for loop where i2 is the index, is poor programming practice, even though the language allows the construction. In your context you probably should have used for i2 = something to something Step 2.
This was written to help, not to criticize, and I hope it is taken in that light.
Lewis
Bookmarks