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: - Sub Find()
-
-
Dim find1, find2, find3, find4 As Range
-
Dim FirstFound As String
-
Dim i As Integer
-
Dim y(), z() As Long
-
-
i = 1
-
Application.FindFormat.Clear
-
Set find1 = Cells.Find(What:=Trim("Amplifier type"), _
-
After:=Cells(1, 1), _
-
LookIn:=xlValues, _
-
LookAt:=xlPart, _
-
SearchOrder:=xlByRows, _
-
SearchDirection:=xlNext, _
-
MatchCase:=False, _
-
SearchFormat:=False)
-
If Not find1 Is Nothing Then
-
'if found, remember location
-
FirstFound = find1.Address
-
'y(i) = cl.Row
-
'z(i) = cl.Column
-
Do
-
find1.Font.Bold = True
-
find1.Interior.ColorIndex = 3
-
Set find1 = Cells.FindNext(After:=find1)
-
ReDim Preserve y(i)
-
ReDim Preserve z(i)
-
y(i) = find1.Row
-
z(i) = find1.Column
-
i = i + 1
-
Loop Until FirstFound = find1.Address
-
End If
-
-
For i = 1 To UBound(y)
-
If ((y(i) = y(i + 1)) & (z(i + 1) - z(i) = 1)) Then
-
MsgBox "Horizontal"
-
End If
-
If ((z(i) = z(i + 1)) & (y(i + 1) - y() = 1)) Then
-
MsgBox "Vertical"
-
Next i
-
-
-
End Sub
Some one let me know even if there is a better solution.
Thank you.
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.
yeah sure zmbd.. nd thanks for your reply.
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.
ADezii 8,834
Recognized Expert Expert - Personally, I would take a totally different approach on the Basic Concept for finding a String within a specified Range, and it is:
-
Dim r1 As Excel.Range
-
Dim r2 As Excel.Range
-
Const conSTING_TO_FIND As String = "Test" 'String to find
-
-
Set r1 = Range("A1:M37") 'Range conSTING_TO_FIND may be found in
-
-
For Each r2 In r1
-
With r2
-
If .Value = conSTING_TO_FIND Then
-
'Redimension an Array to contain Cell Addressess containing String,
-
'then populate said Array
-
Debug.Print "[" & conSTING_TO_FIND & "] found at Address " & .Address
-
End If
-
End With
-
Next
- OUTPUT:
-
[Test] found at Address $M$1
-
[Test] found at Address $A$6
-
[Test] found at Address $L$7
-
[Test] found at Address $F$13
-
[Test] found at Address $K$25
-
[Test] found at Address $B$31
-
[Test] found at Address $H$34
-
[Test] found at Address $A$37
-
[Test] found at Address $M$37
- The populated Array can be analyzed as so desired.
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. :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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....
| |
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
|
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...
|
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,...
|
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 ...
|
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....
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |