Problem with Error 9 while using files already open

Olbap

New Member
Joined
Nov 6, 2009
Messages
19
Hello,

I have a macro that should post a note in a cell wheever this is highlighted in red. The content of the note comes from the value at the same location in another file. I have tried running the code with some parts not active and all seems to come from the 'If' statement, notes appear in all cells if there is no condition. I keep getting a "Subscript out of range" error message.

All the refered files are open while I run the macro and all the sheets have the same name in both files.

Could someone tell me where the problem could be?
Thanks a lot.

Here is the code:


Sub GetDifferences()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim WkSh As String
Dim lastvalue As String
Dim OlFile As String
Dim NwFile As String

'OlFile = Workbooks("Compare2007-08 ENRLPedro.xls").Worksheets("Sheet1").Range("OldFile")
NwFile = Workbooks("Compare2007-08 ENRLPedro.xls").Worksheets("Sheet1").Range("NewFile")

For i = 1 To 10
WkSh = Workbooks("Compare2007-08 ENRLPedro.xls").Worksheets("Sheet1").Cells(i, 8).Value

For j = 1 To 250
For k = 1 To 40
If Workbooks("Compare2007-08 ENRLPedro.xls").Worksheets(WkSh).Cells(j, k).Interior.ColorIndex = 3 Then
lastvalue = CStr(Workbooks(NwFile).Sheets(WkSh).Cells(j, k).Value)
Workbooks("Compare2007-08 ENRLPedro.xls").Worksheets(WkSh).Cells(j, k).NoteText Text:=lastvalue, Start:=1
End If
Next k
Next j
Next i

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Sorry it seems that I had another bug that was hiding the error 9 message. In fact, in the code, even "lastvalue" cannot take the value from the refered cell.

I am using variables to call the workbooks because I have to do this for a quite big collection of files and it seemed simpler to take the name from a cell. Besides that, files are located in different units..
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,273
Members
448,559
Latest member
MrPJ_Harper

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top