Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-23-2018, 11:18 PM
Franci Franci is offline Difficulties with replacing certain language-specific characters using VBA. Windows 7 32bit Difficulties with replacing certain language-specific characters using VBA. Office 2010 32bit
Novice
Difficulties with replacing certain language-specific characters using VBA.
 
Join Date: May 2016
Posts: 12
Franci is on a distinguished road
Default Difficulties with replacing certain language-specific characters using VBA.

In excel, I need to process data that is prepared and exported from another application in the .xls file format.
Because certain language-specific characters in this .xls file are not displayed properly (eg "è" instead of "č"), I wanted to replace them with the help of a macro. So I recorded a macro, which should replace, for example, all "è" characters with the "č" character. There was no problem while recording the macro and all the characters were replaced correctly. But running the same macro on another .xls file caused a problem because instead of all "è" characters, all "e" characters were replaced with "č" character.
This is part of a code I recorded:
Code:
Cells.Replace What:="e", Replacement:="č", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
The recorded VBA code shows that the character to be replaced is not correctly displayed and is actually "e" and not "è". Therefore, I tried to paste the character "è" into the code instead. Unfortunately, this was not successful because after pasting, a question mark ("?") appeared.



Code:
Cells.Replace What:="?", Replacement:="č", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
How can I make a macro that will replace characters correctly?
Reply With Quote
  #2  
Old 05-24-2018, 05:18 PM
BobBridges's Avatar
BobBridges BobBridges is offline Difficulties with replacing certain language-specific characters using VBA. Windows 7 64bit Difficulties with replacing certain language-specific characters using VBA. Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

Franci, just to be clear, have you tried running the program since you pasted "è" into the program and saw it recorded as "?"? I ask because it's just possible the VBA Editor doesn't know how to display such characters on the screen but nevertheless stores them correctly. You could try running it and see what happens.


But that's just a thought; worth trying but I don't really believe it. If I were attempting this, the next thing I'd try is to use hex constants in your code, like this:
Code:
Cells.Replace What:=&H00E8, Replacement:=&H010D, LookAt:=xlPart, SearchOrder:=xlByRows _
  , MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
If that doesn't work, you might have to set two string variables to the What and Replacement values and put them in the code instead:
Code:
Cells.Replace What:=sWhat, Replacement:=sRepl, LookAt:=xlPart, SearchOrder:=xlByRows _
  , MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
One problem is that the "č" character requires an extra byte (is that what they call "Unicode"?); I'm not sure how you represent that in VBA. You might have to examine the value where it appears in a worksheet and see what hex code is being used under the covers.
Reply With Quote
  #3  
Old 05-25-2018, 12:32 AM
ArviLaanemets ArviLaanemets is offline Difficulties with replacing certain language-specific characters using VBA. Windows 8 Difficulties with replacing certain language-specific characters using VBA. Office 2016
Expert
 
Join Date: May 2017
Posts: 873
ArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud of
Default

The function CHRW(UnicodeValue) in VBA returns Unicode character;
The function UNICODE(TextString) in VBA returns Unicode value of 1st character in TextString.

You can use REPLACE() in VBA to replace one Unicode/ASCII character in call with another one;
Or you can give an ReplaceAll command to replace one Unicode/ASCII character on worksheet with another one.

So general approach for VBA procedure will be:
Create 2-dimensional array with Unicodes for characters to be replaced and to replace to;
a) Cycle through all cells in used area in worksheet. For every cell cycle through array to replace the character;
b) Cycle through array. For every character to be replaced invoke ReplaceAll for worksheet.
Reply With Quote
  #4  
Old 05-28-2018, 01:20 AM
Franci Franci is offline Difficulties with replacing certain language-specific characters using VBA. Windows 10 Difficulties with replacing certain language-specific characters using VBA. Office 2016
Novice
Difficulties with replacing certain language-specific characters using VBA.
 
Join Date: May 2016
Posts: 12
Franci is on a distinguished road
Default

Hi Bob and Arvil!
I apologize for the late reply, but this weekend I was absent and I was not at my computer.

Thank you for your for your suggestions that have been a shift in the right direction to solve my problem.


Bob, the answer to your question

Quote:
Franci, just to be clear, have you tried running the program since you pasted "è" into the program and saw it recorded as "?"? I ask because it's just possible the VBA Editor doesn't know how to display such characters on the screen but nevertheless stores them correctly. You could try running it and see what happens.
I did not mention it, but I tried this before. This replaces all the characters with the "č" sign.


Bob's suggestion

Code:
Cells.Replace What:=&H00E8, Replacement:=&H010D, LookAt:=xlPart, SearchOrder:=xlByRows _   , MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
didn't work for me, but after I read Arvil post I came up with
Code:
Cells.Replace What:=ChrW(&HE8), Replacement:=ChrW(&H10D), LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False

This does the job, so I can continue from here.



Thank you both.
Reply With Quote
Reply

Tags
characters, replacing, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficulties with replacing certain language-specific characters using VBA. A Macro to break lines automatically after specific number of characters Annie Wordy Word VBA 1 11-03-2017 08:36 PM
Difficulties with replacing certain language-specific characters using VBA. Deleting Characters in a specific location in Word 2010 ppayaw Word VBA 8 12-13-2016 08:11 AM
Customize grammar settings for specific language? jpgauvin Word 0 12-05-2016 03:06 PM
Difficulties with replacing certain language-specific characters using VBA. How do I Remove A Specific Amount of Characters from Lines in a Document? tatihulot Word 5 01-22-2016 05:55 AM
Difficulties with replacing certain language-specific characters using VBA. Extract Line of Text w/ specific characters up to the paragraph character, send to Excel dmarie123 Word VBA 10 07-20-2015 12:16 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:57 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft