473,508 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type Mismatch(Error13): find a string in excel sheet and store cell address in array

3 New Member
I am trying to find a string in excel sheet and store the string cell address in an array. This string appears several times. By storing every cell address where the string is located, im checking if the string is located one below another (in row) or one beside another (in column). But I am having a type mismatch error 13, please someone help me. Code is as following:

Sorry for such a long code:
Expand|Select|Wrap|Line Numbers
  1. Sub Find()
  2.  
  3.     Dim find1, find2, find3, find4 As Range
  4.     Dim FirstFound As String
  5.     Dim i As Integer
  6.     Dim y(), z() As Long
  7.  
  8.     i = 1
  9.     Application.FindFormat.Clear
  10.     Set find1 = Cells.Find(What:=Trim("Amplifier type"), _
  11.             After:=Cells(1, 1), _
  12.             LookIn:=xlValues, _
  13.             LookAt:=xlPart, _
  14.             SearchOrder:=xlByRows, _
  15.             SearchDirection:=xlNext, _
  16.             MatchCase:=False, _
  17.             SearchFormat:=False)
  18.         If Not find1 Is Nothing Then
  19.             'if found, remember location
  20.             FirstFound = find1.Address
  21.             'y(i) = cl.Row
  22.             'z(i) = cl.Column
  23.             Do
  24.                 find1.Font.Bold = True
  25.                 find1.Interior.ColorIndex = 3
  26.                 Set find1 = Cells.FindNext(After:=find1)
  27.                 ReDim Preserve y(i)
  28.                 ReDim Preserve z(i)
  29.                 y(i) = find1.Row
  30.                 z(i) = find1.Column
  31.                 i = i + 1
  32.                 Loop Until FirstFound = find1.Address
  33.         End If
  34.  
  35.     For i = 1 To UBound(y)
  36.         If ((y(i) = y(i + 1)) & (z(i + 1) - z(i) = 1)) Then
  37.         MsgBox "Horizontal"
  38.         End If
  39.         If ((z(i) = z(i + 1)) & (y(i + 1) - y() = 1)) Then
  40.         MsgBox "Vertical"
  41.         Next i
  42.  
  43.  
  44. End Sub

Some one let me know even if there is a better solution.
Thank you.
Mar 14 '13 #1
5 4366
zmbd
5,501 Recognized Expert Moderator Expert
andy1989:

Your first step is to put a "Stop" command at line #7 and step thru your code until you find which line it is erroring out on. Then take a look at your data and that line...

If you like, I can merge your two posts and delete the duplicate code.

Simply stating that your code "doesn't work," and posting code that appears to have had very little if any troubleshooting performed, and expecting someone to help doesn't usually result in much of an answer and may result in your thread being deleted.
Mar 14 '13 #2
andy1989
3 New Member
yeah sure zmbd.. nd thanks for your reply.
Mar 14 '13 #3
zmbd
5,501 Recognized Expert Moderator Expert
Just noticed line 6:
Dim y(), z() As Long
Depending on which version of VB/VBA you are using, this might be your issue. With the latest release, that construct is nolonger allowed in that what you get is "y()" as a variant and "z()" as Long type cast.

Instead the new construct requires the explicit declaration so that you now have:
Dim y() As Long, z() As Long

You should make that change. Sorry I missed that the first time around... I'm still used to that being "OK" in the older versions.
Mar 14 '13 #4
ADezii
8,834 Recognized Expert Expert
  1. Personally, I would take a totally different approach on the Basic Concept for finding a String within a specified Range, and it is:
    Expand|Select|Wrap|Line Numbers
    1. Dim r1 As Excel.Range
    2. Dim r2 As Excel.Range
    3. Const conSTING_TO_FIND As String = "Test"       'String to find
    4.  
    5. Set r1 = Range("A1:M37")    'Range conSTING_TO_FIND may be found in
    6.  
    7. For Each r2 In r1
    8.   With r2
    9.     If .Value = conSTING_TO_FIND Then
    10.       'Redimension an Array to contain Cell Addressess containing String,
    11.       'then populate said Array
    12.       Debug.Print "[" & conSTING_TO_FIND & "] found at Address " & .Address
    13.     End If
    14.   End With
    15. Next
  2. OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. [Test] found at Address $M$1
    2. [Test] found at Address $A$6
    3. [Test] found at Address $L$7
    4. [Test] found at Address $F$13
    5. [Test] found at Address $K$25
    6. [Test] found at Address $B$31
    7. [Test] found at Address $H$34
    8. [Test] found at Address $A$37
    9. [Test] found at Address $M$37
  3. The populated Array can be analyzed as so desired.
Mar 14 '13 #5
andy1989
3 New Member
Thanks a lot guys i got the error I was having.
It was in the line

If ((y(i) = y(i + 1)) & (z(i + 1) - z(i) = 1)) Then

Instead of '&' it should be 'And'.

Thanks once again. :)
Mar 14 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2835
by: dgoel | last post by:
Hi, I Have a text file & I want to open it in excel sheet ( withou importing). I have written code for it, but it is not opening exce sheet. It opens the text file, but does not create a excel...
14
5733
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
1
2399
by: st | last post by:
Hi, I've a routine that exports a DB query to Excel by building an XmlDocument and saving to a XmlTextWriter. I'm having trouble with carriage returns in a mailing address not showing up in the...
2
8901
by: Ch Pravin | last post by:
Hi All: I am having the following xml which i need to convert to excel using xslt. Please help me out. Afghanistan.xml <?xml version="1.0" encoding="utf-16"?> <Languages...
12
9023
by: forrestgump | last post by:
I am currently trying to create VBA to send a specified excel sheet to varied email sources. I currently have the code below which sends the attachment in an email to a specified source e.g....
7
12037
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
5
4835
by: Ramya28 | last post by:
I need to prevent the cells in excel from being selected.and it shud be done through java code only.Is there any way to do it? Here my program generates excel and I need that excel sheet to be...
3
5033
kirubagari
by: kirubagari | last post by:
I would like to duplicate the numbers from from excel sheet 1 to excel sheet 2.Kindly help me on this.Sometime its unable to duplicate.. Sub Duplicate Dim oDoc As Object, oSheet As Object,...
1
2674
kirubagari
by: kirubagari | last post by:
Hai experts, How to duplicate the data from 1 excel sheet to another excel sheet 2. Lets say Name Voucher Value Voucher Number lee 300.00 ...
1
5633
by: Nilesh K R | last post by:
hello, I am try to retrieve data from my c# windows application to excel sheet and store this .xls file to desired location. but my problem is that my code save excel file at a predefined location....
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.