473,569 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type Mismatch(Error1 3): 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 4377
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
2841
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 sheet populate it. I do not know what the problem is, can someone please hel out with this one, Here is my code: StreamReader dataFileReader = null;...
14
5763
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 SQL DATABASE, probably by the click of a button. Is this possible? & what is the BEST APPROACH for doing this? & also if any links are there do...
1
2406
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 final Excel sheet. What's added to the InnerText of an XmlDocument is something similar to:
2
8904
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 BuildVersion="1,5,0815,0 " CountryName="Afghanistan" > <Language locale="English">
12
9032
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. mrqwe@hotmails.com, however I want to change this so the address is chosen from a specified cell on a worksheet. I have tried to reference a cell e.g....
7
12054
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
4847
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 protected and its cell from being selected and copied elsewhere.Someone can help??. The below code helps in making excel read only, and prompts for...
3
5039
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, oCell As Object, oCell2 As Object, oCell3 As Object, oString As String Dim oCells As Object Dim oCursors As Object Dim aAddresss As Variant
1
2678
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 58419-58421 meena 300.00 58422-58424 Tan 300.00 58425-58427
1
5643
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. private Excel.Application m_objExcel = null; private Excel.Workbooks m_objBooks = null; private Excel._Workbook m_objBook =...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6286
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5222
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2116
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 we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.