Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Problem with IfInString


  • Please log in to reply
7 replies to this topic
Prankie
  • Members
  • 16 posts
  • Last active: Apr 18 2012 09:17 AM
  • Joined: 18 Apr 2012
It's a excel file, the first cell contains the firstname. The second cell contains first and lastname.

Send ^c{tab}
varfirst = %clipboard%

Send ^c{tab}
name = %Clipboard%

IfInString, name, %varfirst%
{
Send match {tab}

}
else
Send no match

The thing is, it always says no match if I get the varfirst from clipboard in excel's cell. It works alright if I set the variable myself without using the clipboard. If I use msgbox %varfirst% %name% both everything checks out. What am I missing?

CodeKiller
  • Members
  • 2067 posts
  • Last active: Feb 26 2016 09:30 AM
  • Joined: 10 Jul 2008
For me Tab change the cell to the right, but if I use this one :

it's workign fine.

F1::
Send ^c
ClipWait
varfirst = %clipboard%

Send {Return 2}
Sleep 250
Send ^c
ClipWait
Send {Return}
name = %Clipboard%

If InStr(name, varfirst)
	Send {Up}{Right 2}match{Return}
else
	Send {Up}{Right 2}no match{Return}
Return


Prankie
  • Members
  • 16 posts
  • Last active: Apr 18 2012 09:17 AM
  • Joined: 18 Apr 2012
Thank you! :D

For some reason

IfInString, name, %varfirst%
NOT WORKING

If InStr(name,varfirst)
WORKING

CodeKiller
  • Members
  • 2067 posts
  • Last active: Feb 26 2016 09:30 AM
  • Joined: 10 Jul 2008
It's not "for some reason", there is a good reason in fact : this command doesn't accept a var for 2nd arg :

IfInString, var, SearchString

Parameters
var The name of the variable whose contents will be searched for a match.
SearchString The string to search for. Matching is not case sensitive unless StringCaseSense has been turned on.



Prankie
  • Members
  • 16 posts
  • Last active: Apr 18 2012 09:17 AM
  • Joined: 18 Apr 2012
I tried with your code again but it seem now it seems to no longer be working. :/

Lets say the first cell contains Batman. The second cell contains Batman Forever.
Using clipboard to store both value into two variables, how do I get a response that Batman exist in the variable Batman Forever? And if I change the text to Mr Freeze, then I would get it doesn't exist.

Prankie
  • Members
  • 16 posts
  • Last active: Apr 18 2012 09:17 AM
  • Joined: 18 Apr 2012

It's not "for some reason", there is a good reason in fact : this command doesn't accept a var for 2nd arg :

IfInString, var, SearchString

Parameters
var The name of the variable whose contents will be searched for a match.
SearchString The string to search for. Matching is not case sensitive unless StringCaseSense has been turned on.

What if I need to use a variable as searchstring, how should I proceed?

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
Just FYI if you're using the latest version (AHK_L) there are much easier ways to work with things like Excel spreadsheets. If you download and install AHK_L here's some sample code you can run to show you how much easier you can check cells against one another:

Clipboard=
(
dog	cat
Batman	Batman Forever
sharp	tacky
fungus	a fungus among us
)
xl :=	ComObjCreate("Excel.Application"), xl.Workbooks.Add()
sheet :=	xl.Workbooks(1).Worksheets("Sheet1"), sheet.Paste(sheet.Range["A1"]), xl.Visible :=	True
For c in sheet.Range["A1:A4"]
{
	xl.Range[c.Address(0,0) ":" c.Offset(0,1).Address(0,0)].Select
	Sleep, 1000
	if	InStr(c.Offset[0,1].Value,c.Value)
		MsgBox, , Found it!!!, %	c.Value " was found in " c.Offset[0,1].Value "!"
	else	MsgBox, , Couldn't find it :(, %	c.Value " was not found in " c.Offset[0,1].Value "...curses!"
}
return

If you try it and like it, come back and we can talk about how to get you started on learning how to do it yourself.

Prankie
  • Members
  • 16 posts
  • Last active: Apr 18 2012 09:17 AM
  • Joined: 18 Apr 2012
I'm using the AHK_L, I thought it might be one of the reasons why it didn't work at the first place.

I do like it, please do teach oh great master.