Linking and 255 Characters

jroo

Board Regular
Joined
May 22, 2003
Messages
157
Hi,

I have the following code which identifies certain files in a directory and looks up explanations provided in the corresponding workboook.

Sub RetrieveExplanations()

For i = 5 To 428
Cells(i, 15).Formula = "=VLOOKUP(C" & i & ",'C:\My Documents\Survey\[" & Cells(i, 1) & _
".xls]Explanations'!$A$1:$B$100,2,FALSE)"
Next i

End Sub

My problem is that if the text in a cell is more than 255 characters, it will not pick up the remaining text. Is there a way around this? Please help! Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
What about this??
the following code opens the workbook containing the explanation, looks for cells(i,"C") in A1:A100. If the value is found, say cell A53, returns in cell(i,15) the value of B53.

Sub ...
...
Dim c As Range
Dim StrToFind As String
...
ThisWorkbook.Activate
Sheets(1).Activate 'Activate proper sheet
StrToFind = Cells(i, "C")
'If not, open "C:\My Documents\Survey\" & Cells(i, 1) & ".xls]Explanations

Workbooks.Open Filename:= _
"C:\My Documents\Survey\" & Cells(i, 1) & ".xls"

'If open, activate it!!!

Sheets("Explanations").Select
Set c = Range("A1:A100").Find(What:=StrToFind, After:=Range("A1"), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not c Is Nothing Then
ThisWorkbook.ActiveSheet.Cells(i, 15) = c.Offset(0, 1)
Else
ThisWorkbook.ActiveSheet.Cells(i, 15) = "Not found"
End If
...
End Sub

Ciao
 
Upvote 0
Chiello,

For the most part your code works nicely, but I ran into some minor problems. Can you let me know what the correct syntax would be to close each workbook in the directory within the For loop

I tried inserting

'Workbooks.Close Filename:= _
'"K:\MVSSAS\DataColl\datcol04\Finished\" & Cells(i, 1) & ".xls"

after your "end if" statement...but i got an error.

Also for on of my "i" observations i ran into an error stating:

"Run-time error '1004': Application-defined or object-defined error"

when i hit the debug button it hilghted the syntax "ThisWorkbook.Activesheet.Cells(i,15)=c.offset(0,1)"

Would you happen to know why this occured? Or what code can be inserted so at least it would flag that there's something wrong for that specific observation, but continue running for the remaining observations?

Thanks so much!
 
Upvote 0
Try this:

'Workbooks.Close Filename:= _
'"K:\MVSSAS\DataColl\datcol04\Finished\" & Cells(i, 1) & ".xls"

dim StrFileName as String
StrFileName = Cells(i, 1) & ".xls"
Workbooks(StrFileName).Close SaveChanges:=False

Also for on of my "i" observations i ran into an error stating:

"Run-time error '1004': Application-defined or object-defined error"

when i hit the debug button it hilghted the syntax "ThisWorkbook.Activesheet.Cells(i,15)=c.offset(0,1)"
Would you happen to know why this occured?

Check 0<i<65537 and c.Column < 256

Or what code can be inserted so at least it would flag that there's something wrong for that specific observation, but continue running for the remaining observations?

Insert at the top of routine:
On Error GoTo ErrorOccurred

and at bottom:
...
Exit Sub
ErrorOccurred:
MsgBox "Error Occurred"
Resume Next
End Sub

Hope it's clear

Ciao
 
Upvote 0
I've refined my code, but l found that using the "workbook open" technique - it takes extremely long to go through all of my 424 observations. I ended the run after 15 minutes because I'm assuming there must be another alternative. I hope!

Below i've included the sub procedures that I initially posted and the revised code based on the suggestions posted by ciello

Option 1 uses a vlookup formula and runs relatively fast. But it still truncates explanations that contain more than 255 characters.

Option 2 uses the workbook open method.

Sub Option1()
Dim MyCount
MyCount = Range(Range("A5"), Range("A5").End(xlDown)).count + 4
For i = 5 To MyCount
Cells(i, 15).Formula = "=VLOOKUP(C" & i & ",'C:\My Documents\Survey\[" & Cells(i, 1) & _
".xls]Explanations'!$A$1:$B$100,2,FALSE)"
Next i

End Sub



Sub Option2()

Application.ScreenUpdating = False

Dim c As Range
Dim StrToFind As String
Dim MyCount
MyCount = Range(Range("A5"), Range("A5").End(xlDown)).count + 4
'On Error GoTo ErrorOccurred

For i = 5 To MyCount
ThisWorkbook.Activate
Sheets(1).Activate
StrToFind = Cells(i, "C")

Workbooks.Open Filename:= _
"C:\My Documents\Survey\" & Cells(i, 1) & ".xls"
Sheets("Explanations").Select

Set c = Range("A1:A100").Find(What:=StrToFind, After:=Range("A1"), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

If Not c Is Nothing Then
ThisWorkbook.ActiveSheet.Cells(i, 15) = c.Offset(0, 1)
Else
ThisWorkbook.ActiveSheet.Cells(i, 15) = "Not found"
End If
ActiveWorkbook.Close (False)
Next i

Exit Sub

ErrorOccurred:
ThisWorkbook.ActiveSheet.Cells(i, 15) = "ERROR"
Resume Next

Application.ScreenUpdating = False

End Sub


Does anyone have any other suggestions? I attempted to search for help on MS's website:
http://support.microsoft.com/?scid=.../servicedesks/bin/kbsearch.asp?article=213841
it has sample code - but it's foreign to me



Also, I noticed that when I ran the "option 2" macro - an error occured for 1 observation - but I'm not sure why it happend exactly. There were 1,104 characters in that observation. But when I reduced the characters in that worksheet (to 25) and reran the macro - no error occured. Is there a certain range above 255 characters where an error arises?
 
Upvote 0

Forum statistics

Threads
1,216,512
Messages
6,131,091
Members
449,618
Latest member
lewismillar

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