[VBA][excel] Occurred error When Using 'Japanese - Katakana' in 'inStr' - vba

I'm Korean Data analyst. I Used Korean version Windows 7 and OS.
I have a some problem about instr in VBA (Excel).
In normal case,
instr is generate accurate number at "C2" indicating starting point number at "A2" string
Here is normal sample:
Even Korean, and Japanese (hiragana) have no problem too.
Please excuse me I can't more sample
because I have not enough reputation point this in this site.
But, there is always occur error when I use Japanese-Katakana
The error message is below sentence.
'7' runtime error has occurred, Not enough memory
Refer to this sample:
I have embarrassed about this only one error
and trying to solve this problem,
but still don't know how.
I hope Anyone advice me good opinion and solution.
Thanks

I'm not able to replicate this issue, in my English Excel 2013/Win7.
However, I have seen such issue before in old excel version, and most probably, below should work.
Please change function "InStr" to "InStrb", and see whether it works
Sub instr()
Range("C2") = InStrB(1, Range("A2"), Range("B2"), 1)
End Sub
Or you might want to do StrConv(string, vbUnicode) first before you call InStr function.
Sub instr()
Range("C2") = InStr(1, StrConv(Range("A2"), vbUnicode), StrConv(Range("B2"), vbUnicode), 1)
End Sub
Whatever approach you selected, the return position value need be divided by two, if the input string is not Half-width chars. (Half-width chars are already gone in recent excel, and I suppose your excel is too old. Upgrading it is the best choice.)

Related

Range accepts sometimes only semicolons instead of commas

I have reduced my problem to the following code example. I am using a German Excel version in which separators in normal Excel formulas are semicolons ";" instead of "," (e.g. =SUMME(A1;A3) instead of =SUM(A1,A3)).
Now the code which works different from time to time:
Sub CommasDoNotWorkAnymore()
Dim a()
Dim i%
a = Array("A1,A3,A5", "B1", "B2")
i = 0
Debug.Print Sheets(1).Range(a(i)).Address
End Sub
Normally, when starting Excel, this code works. But sometimes Excel seem to switch the accepted separators used in the Range() to semicolons untill I restart Excel. This occurs most times when rerunning the code after a runtime error.
Is this a general Excel bug? Does anybody know what is behind this behaviour? Is there some Excel-wide "local option" for the Range class?
EDIT: I just tried to convert the a(i) with CStr(a(i) but this does also not work. So no ByRef kind of problem...
If you want to control it, check first what separator is currently in use. What I guess is that you want to know the list separator:
Application.International(xlListSeparator)
Check other separators here:
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-international-property-excel
The other time I had a problem with identifying decimal separator in VBA. Finnally I was able to get it in this way:
Function GetVBAdecimalSep()
Dim a(0) As Variant
a(0) = 1 / 2
GetVBAdecimalSep = Mid(a(0), 2, 1)
End Function
Changing separator not always works. Please see this: Changing decimal separator in VBA (not only in Excel)
The best solution is to check/change locale, even temporary.
Application.LanguageSettings.LanguageID(msoLanguageIDUI)
gives the LCID which would be 1033 for English (US)

VBA Find (LookAt=xlWhole) will always return an error

I have a search function in a VBA code that searches column A. Column A is filled with acronyms and the corresponding rows in Column B are the meanings for that acronym.
I have a user form setup that the user can enter an acronym and if it is in the file, it will show a message box saying what that acronym means.
I am trying to search for an exact match of the acronym the user enters, via this line:
Range("A:A").Find(acro, LookAt = xlWhole).Select
However, when I run it, even if I copy a cell containing an acronym and paste it into the user form text box, it will act as if it could not find it and follows my On Error handle.
What did I do wrong that made it unable to find the acronym string I am looking for?
Thank you!
#nwhaught already answered and pointed out the real issue, but to add something too large for a comment:
It's best not to chain Select/Activate directly to Find():
Range("A:A").Find(acro, LookAt = xlWhole).Select
...since that requires error handling when the item is not found (and as you discovered that can mask other issues with your code)
Try something like this instead:
Dim f
Set f = Range("A:A").Find(acro, LookAt := xlWhole)
If Not f Is Nothing then
'do something with f
End If
The Is Nothing test avoids the use of error handling.
Your On Error handle is masking the true cause of the error.
Code should be Range("A:A").Find(What:=acro, LookAt:= xlWhole).Select
When debugging, it's best to disable error handling, as the whole point of handling an error is to mask its effect to the user. (which makes debugging nearly impossible)

Replacing nothing with a string

This may be a simple question, but I have searched quite a few sites and have tried a few of my own ideas and I still cannot seem to find a simple way to get Visual Studio to replace all of the listbox items with the string of nothing with some other text.
Using things such as :
For Each S In ListBox1.Items
S.Replace("", "Not Blank")
Next
Shows:
Error
String cannot be of zero length
Which is quite annoying because the actual listbox item contains no text.
This seems to be one of the easiest things I have ever encountered while using vb.net. But it now seems very hard for what should be a simple command.
A couple of problems. The Replace function returns a new value, and you promptly ignore it. Second, you can't really modify the collection as you For-Each over it, so a For-Loop would be more appropriate.
I think you want something like this instead:
For i As Integer = 0 To ListBox1.Items.Count - 1
If String.IsNullOrEmpty(ListBox1.Items(i).ToString) Then
ListBox1.Items(i) = "Not Blank"
End If
Next

VBA for Dummies example, display message box giving syntax error

I am trying to run this code from an example in 'VBA for Dummies'
It is giving me 'Syntax Error' at the message box line, not very encouraging
Sub AddEmUp()
Sum = 1 + 1
MsgBox “The answer is “ & Sum
End Sub
Why does this not work?
As pointed by L42, it is the quotation usage; While you might have problems to copy-paste, it could be the source that have wrong formatting as well.
Try to always type your code when learning, it will still more to you :)

VBA Excel + using match function

I'm having some kind of problem in a piece of code.
I need to find values in a excel book and then copy it to another. The thing is that i need to copy it concept by concept in the right place. The problem is that the file doesnt come every week in the same order.
So i need to find the concept and then copy the next cell that is the value of that concept.
First of all i use need to locate the correct line so i can start copying (this part is easy and its done).
Secondly, having the correct line, i need to find the concept, which in this example im going to use the "619".
After having find the location of this value i store the value in "c_audiovisual".
On Error GoTo ErrhandlerCAV
lRowC_AV = Application.WorksheetFunction.Match(619, Range("A" & line & ":FI" & line), 0) + 1
'On Error GoTo ErrhandlerCAV
Continue:
If errorCAV = 1 Then
c_audiovisual = 0
errorCAV = 0
Else
c_audiovisual = ActiveSheet.Cells(line+ m2, lRowC_AV).Value
End If
I've made a escape in case that this concept isn't present in that line (which sometimes occurs).
Sometimes this piece of code works, and other it doesn't.
When i'm on debug mode (pressing F8) it works. And when i use small files to look for the values it works. On bigger files sometimes don't.
Any ideas?
I faced similar issues. I fixed it with using ThisWorkbook.Sheets("MySheet").Range(...) inside the Match function. Or try ActiveWorkbook as applicable. Let me know if this worked. Curious to know.
Btw I noticed column and row number both are "line". Is that a mistake?