How to convert a column of Japanese words in a column of Hiragana words in Excel

kanachan

New Member
Joined
Aug 30, 2024
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hello!
I have a large column of Japanese words (some are entirely in hiragana, katakana or kanji, some are a mix of kanji and hiragana, some are a mix of kanji and katakana, and some a mix of hiragana and katakana), and I would like to convert that column of words into another column with exactly those same words but all of them written entirely in hiragana.

So for example, if in that first column there are words like this:

ゆっくり
ファッション
家族
落ち着く
アア溶岩

I would like to convert that into:

ゆっくり
ふぁっしょん
かぞく
おちつく
ああようがん

Looking for some information about it on Google, I've read that something can be done with VBA programming inside Excel, but I have no idea of programming and I couldn't understand barely anything.

Could you please help me to do this in Excel?

Thank you very much for all the help you can offer me.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Thanks for the welcoming and sorry very much for my mistake of not pointing out my cross-posting.

I was unaware of that as a thing to bother people, but reading the link made me realize of why it is, so I apologize for it. I'm very sorry. 😞

My reason for doing it was just I have no answers on the Stackoverflow forum, and in a matter of several minutes from posting on there, my question was buried quite a few pages down and its visibility continues much worse by now, so I decided to post my question in a forum specialized in Excel like this one.

Anyway, sorry again for my mistake. I wish you could forgive me.

For all people who visit this question, if after checking my question on Stackoverflow

How to convert a column of Japanese words in a column of Hiragana words in Excel

you wish to answer it (whether it be here or there) if I still have none or to add something, please feel free to do it. I will be really thankful for any help you can provide me.
 
Upvote 0
In your SO thread you say that https://www.jcinfo.net/ja/tools/kana converts Japanese words. The macro below uses that website by automating Internet Explorer - luckily the site works with IE, so we can use standard Windows components. Words in column A starting at A2 and puts the converted word in column B.

Note - you must set the two references listed at the top of the code, via Tools -> References in the VBA editor.

VBA Code:
'References required
'Microsoft HTML Object Library
'Microsoft Internet Controls

Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If


Public Sub Convert_Japanese_Words()

    Dim r As Long

    With ThisWorkbook.ActiveSheet
        For r = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
            .Cells(r, 2).Clear
            .Cells(r, 2).Value = Convert_Word(.Cells(r, 1).Text)
        Next
    End With
    
End Sub


Public Function Convert_Word(word As String) As String

    Dim URL As String
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim button As HTMLButtonElement
    Dim textInput As HTMLTextAreaElement
    Dim katakanaCheckbox As HTMLInputElement
    Dim resultText As HTMLDivElement
    Dim timeout As Date
    
    URL = "https://www.jcinfo.net/ja/tools/kana"
    
    Set IE = Get_IE_Window(URL)
    If IE Is Nothing Then Set IE = Get_IE_Window("")
    
    If IE Is Nothing Then
        Set IE = New InternetExplorer
        IE.Visible = True
        IE.navigate URL
        While IE.Busy: DoEvents: Sleep 100: Wend
    
        Set HTMLdoc = IE.document
        
        'Click cookies Consent button, if present
        '<button class="fc-button fc-cta-consent fc-primary-button" role="button" aria-label="Consent" tabindex="0">
        ' <div class="fc-button-background"></div>
        ' <p class="fc-button-label">Consent</p>
        '</button>
        
        timeout = DateAdd("s", 5, Now)
        On Error Resume Next
        Do
            Set button = HTMLdoc.querySelector("button.fc-button.fc-cta-consent.fc-primary-button")
            DoEvents
            Sleep 100
        Loop While button Is Nothing And Now <= timeout
        On Error GoTo 0
        If Not button Is Nothing Then button.Click
    
    End If
    
    Set HTMLdoc = IE.document
    
    'Put word in text area
    '<textarea name="text" id="input_text" placeholder="?????????????" lang="ja" class="form-control">??????</textarea>
    
    Set textInput = HTMLdoc.getElementById("input_text")
    textInput.Value = word
    
    'Ensure the 'Include katakana' checkbox is ticked
    '<div class="custom-control custom-checkbox">
    ' <input id="is_katakana" class="custom-control-input" name="is_katakana" type="checkbox" value="1">
    '  <label class="custom-control-label" for="is_katakana" id="is_katakana">
    '   ????????
    '  </label>
    '</div>
  
    Set katakanaCheckbox = HTMLdoc.getElementById("is_katakana")
    If Not katakanaCheckbox.Checked Then katakanaCheckbox.Click
    
    'Click the 'Furigana conversion" button
    '<button type="submit" class="btn btn-primary">??????</button>
    
    Set button = HTMLdoc.querySelector("button.btn.btn-primary")
    button.Click
    
    While IE.Busy: DoEvents: Sleep 100: Wend
    While HTMLdoc.readyState = "loading": DoEvents: Sleep 100: Wend
    
    'Extract result from 3rd text box
    '<div class="_result my-5 p-2" lang="ja">
    ' <div class="line">
    '  <span class="morpheme">?</span>
    '  <span class="morpheme">?</span>
    '  <span class="morpheme">?</span>
    '  <span class="morpheme">?</span>
    ' </div>
    '</div>
    
    Set resultText = HTMLdoc.querySelectorAll("div._result.my-5.p-2")(2)
    
    Convert_Word = resultText.innerText
    
End Function


Private Function Get_IE_Window(partialURLorName As String) As InternetExplorer

    'Look for an IE browser window or tab already open at the (partial) URL or location name and, if found, return
    'that browser as an InternetExplorer object.  Otherwise return Nothing

    Dim Shell As Object
    Dim IE As InternetExplorer
    Dim i As Variant 'Must be a Variant to index Shell.Windows.Item() array
    
    Set Shell = CreateObject("Shell.Application")
    
    i = 0
    Set Get_IE_Window = Nothing
    While i < Shell.Windows.Count And Get_IE_Window Is Nothing
        Set IE = Shell.Windows.Item(i)
        If Not IE Is Nothing Then
            If IE.Name = "Internet Explorer" And InStr(IE.LocationURL, "file://") <> 1 Then
                If InStr(1, IE.LocationURL, partialURLorName, vbTextCompare) > 0 Or InStr(1, IE.LocationName, partialURLorName, vbTextCompare) > 0 Then
                    Set Get_IE_Window = IE
                End If
            End If
        End If
        i = i + 1
    Wend
    
End Function
 
Upvote 1
@John_w
First of all, thank you very much for your answer! I really appreciate a lot your help. :)
I went to the VBA section by doing Alt + F11 and I pasted the code in the subsection ThisWorkbook located in the left in the folder Microsoft Excel Object under "VBAProject". According to Google, it is there where I'm supposed to paste the code, but is that actually correct?
Besides, I opened Tools -> References and set the two references you mention, and finally accepted. But after this, the only thing left for me to do is to click the Save icon and exit the VBA console?

I assume the code works on all Excel sheets, right? To start up the code, what do I do? Do I select all the words in the column I want to transform, which is column A starting at A2 as you say, and where do I click?

I'm sorry if I ask perhaps obvious questions, but I have never used codes in Excel and I have no knowledge of programming.
 

Attachments

  • Excel VBA.png
    Excel VBA.png
    67.5 KB · Views: 11
Upvote 0
I went to the VBA section by doing Alt + F11 and I pasted the code in the subsection ThisWorkbook located in the left in the folder Microsoft Excel Object under "VBAProject". According to Google, it is there where I'm supposed to paste the code, but is that actually correct?

No, not quite correct. In the VBA editor click Insert -> Module and paste the code in the new module (e.g. Module1).

Besides, I opened Tools -> References and set the two references you mention, and finally accepted. But after this, the only thing left for me to do is to click the Save icon and exit the VBA console?

Yes, click Save, but stay in the VBA editor to run the Convert_Japanese_Words macro (see below).

I assume the code works on all Excel sheets, right? To start up the code, what do I do? Do I select all the words in the column I want to transform, which is column A starting at A2 as you say, and where do I click?

No, the code works on the active sheet only. With the words to be transformed starting at A2 on the active sheet (no need to select the words):

Book1
AB
1Input WordConverted
2ゆっくり
3ファッション
4家族
5落ち着く
6アア溶岩
Sheet1

Go to Module1 in the VBA editor, press F5 (the Convert_Japanese_Words macro is selected) and click Run. (For other ways to run a macro, see How to run macro in Excel and create a macro button).

When the macro has finished the result should be:

Book1
AB
1Input WordConverted
2ゆっくりゆっくり
3ファッションふぁっしょん
4家族かぞく
5落ち着くおちつく
6アア溶岩ああようがん
Sheet1
 
Upvote 1
@John_w

Thank you very very again for answering me and for all of your help. I'm very thankful to you.

I've deleted the code from the wrong place where I pasted it and pasted the code in the correct place, in Module1, and after saving, I ran the macro as you told me, but I get the error 'Run-time Error '91' Object-variable or With block variable not set', and it doesn't work.

I've tried other ways of running the macro, such as getting out the VBA Editor, going to the sheet, pressing Alt + F8, and running the macro, but I get the same error. When I click Run, automatically Internet Explorer opens as I could expect, but shortly after it, the Excel tab on the taskbar lights up, and I see that the macro has transformed the first word correctly, but that an error message has appeared: 'Run-time Error '91' Object-variable or With block variable not set' (please, see Image1 attached).

If I click in Debug, the line "Convert_Word = resultText.innerText" in the code is highlighted (please, see Image2 attached).

Should I do something else to fix it, or should the code be modified in some part of it?
 

Attachments

  • Image1.png
    Image1.png
    63.7 KB · Views: 6
  • Image2.png
    Image2.png
    53.6 KB · Views: 6
Upvote 0
error message has appeared: 'Run-time Error '91' Object-variable or With block variable not set' (please, see Image1 attached).

If I click in Debug, the line "Convert_Word = resultText.innerText" in the code is highlighted (please, see Image2 attached).

That error could be caused by a timing issue between Excel and Internet Explorer on these 2 lines:

VBA Code:
    Set resultText = HTMLdoc.querySelectorAll("div._result.my-5.p-2")(2)

    Convert_Word = resultText.innerText

Meaning that when the first line is executed, the ("div._result.my-5.p-2")(2) element on the web page doesn't exist, causing resultText to be Nothing and generate the error you see.

We can make the code wait until that element is available by replacing those 2 lines with:

VBA Code:
    Dim results As IHTMLDOMChildrenCollection
    Do
        Set results = HTMLdoc.querySelectorAll("div._result.my-5.p-2")
        DoEvents
        Sleep 100
    Loop Until results.Length = 3
  
    Convert_Word = results(2).innerText

NOTE - edited to move the Set results = line inside the loop.
 
Last edited:
Upvote 1
Solution
@John_w

Thank you once again for your answer! :)

I've replaced those two lines of code for the new piece of code, resulting the entire code like this.

VBA Code:
'References required
'Microsoft HTML Object Library
'Microsoft Internet Controls

Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If


Public Sub Convert_Japanese_Words()

    Dim r As Long

    With ThisWorkbook.ActiveSheet
        For r = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
            .Cells(r, 2).Clear
            .Cells(r, 2).Value = Convert_Word(.Cells(r, 1).Text)
        Next
    End With
    
End Sub


Public Function Convert_Word(word As String) As String

    Dim URL As String
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim button As HTMLButtonElement
    Dim textInput As HTMLTextAreaElement
    Dim katakanaCheckbox As HTMLInputElement
    Dim resultText As HTMLDivElement
    Dim timeout As Date
    
    URL = "https://www.jcinfo.net/ja/tools/kana"
    
    Set IE = Get_IE_Window(URL)
    If IE Is Nothing Then Set IE = Get_IE_Window("")
    
    If IE Is Nothing Then
        Set IE = New InternetExplorer
        IE.Visible = True
        IE.navigate URL
        While IE.Busy: DoEvents: Sleep 100: Wend
    
        Set HTMLdoc = IE.document
        
        'Click cookies Consent button, if present
        '<button class="fc-button fc-cta-consent fc-primary-button" role="button" aria-label="Consent" tabindex="0">
        ' <div class="fc-button-background"></div>
        ' <p class="fc-button-label">Consent</p>
        '</button>
        
        timeout = DateAdd("s", 5, Now)
        On Error Resume Next
        Do
            Set button = HTMLdoc.querySelector("button.fc-button.fc-cta-consent.fc-primary-button")
            DoEvents
            Sleep 100
        Loop While button Is Nothing And Now <= timeout
        On Error GoTo 0
        If Not button Is Nothing Then button.Click
    
    End If
    
    Set HTMLdoc = IE.document
    
    'Put word in text area
    '<textarea name="text" id="input_text" placeholder="?????????????" lang="ja" class="form-control">??????</textarea>
    
    Set textInput = HTMLdoc.getElementById("input_text")
    textInput.Value = word
    
    'Ensure the 'Include katakana' checkbox is ticked
    '<div class="custom-control custom-checkbox">
    ' <input id="is_katakana" class="custom-control-input" name="is_katakana" type="checkbox" value="1">
    '  <label class="custom-control-label" for="is_katakana" id="is_katakana">
    '   ????????
    '  </label>
    '</div>
  
    Set katakanaCheckbox = HTMLdoc.getElementById("is_katakana")
    If Not katakanaCheckbox.Checked Then katakanaCheckbox.Click
    
    'Click the 'Furigana conversion" button
    '<button type="submit" class="btn btn-primary">??????</button>
    
    Set button = HTMLdoc.querySelector("button.btn.btn-primary")
    button.Click
    
    While IE.Busy: DoEvents: Sleep 100: Wend
    While HTMLdoc.readyState = "loading": DoEvents: Sleep 100: Wend
    
    'Extract result from 3rd text box
    '<div class="_result my-5 p-2" lang="ja">
    ' <div class="line">
    '  <span class="morpheme">?</span>
    '  <span class="morpheme">?</span>
    '  <span class="morpheme">?</span>
    '  <span class="morpheme">?</span>
    ' </div>
    '</div>
    
    Dim results As IHTMLDOMChildrenCollection
    Set results = HTMLdoc.querySelectorAll("div._result.my-5.p-2")
    Do
        DoEvents
        Sleep 100
    Loop Until results.Length = 3
    
    Convert_Word = results(2).innerText
    
End Function


Private Function Get_IE_Window(partialURLorName As String) As InternetExplorer

    'Look for an IE browser window or tab already open at the (partial) URL or location name and, if found, return
    'that browser as an InternetExplorer object.  Otherwise return Nothing

    Dim Shell As Object
    Dim IE As InternetExplorer
    Dim i As Variant 'Must be a Variant to index Shell.Windows.Item() array
    
    Set Shell = CreateObject("Shell.Application")
    
    i = 0
    Set Get_IE_Window = Nothing
    While i < Shell.Windows.Count And Get_IE_Window Is Nothing
        Set IE = Shell.Windows.Item(i)
        If Not IE Is Nothing Then
            If IE.Name = "Internet Explorer" And InStr(IE.LocationURL, "file://") <> 1 Then
                If InStr(1, IE.LocationURL, partialURLorName, vbTextCompare) > 0 Or InStr(1, IE.LocationName, partialURLorName, vbTextCompare) > 0 Then
                    Set Get_IE_Window = IE
                End If
            End If
        End If
        i = i + 1
    Wend
    
End Function

However, the result of running the macro is that it transforms well the first word of the column, but then the macro supposedly continues running but nothing else is done, and it goes endlessly like this without doing anything until I finally, after having waited a lot, decide to close the Internet Explorer window.

When doing so, a 'Run-Time Error '-2147417848 (80010108)': Automation Error' message appears on Excel (please, see Image1), and when clicking on Debug, the line "Loop Until results.Length = 3" is highlighted (please, see Image2).

I don't know if this would be easier in terms of programming, but if I could get a macro (let's call it Macro A) that transforms just one word that is located in any cell, without it having to be any specific one (e.g. A1, A2, A3, and so on), and puts the result in the adjacent cell on the right, I could then create then a Macro B that runs Macro A, moves the cell selection down and repeats the process a certain number of times, and that would work for me as well.
 

Attachments

  • Image1.png
    Image1.png
    66.7 KB · Views: 5
  • Image2.png
    Image2.png
    50.4 KB · Views: 5
Upvote 0

Forum statistics

Threads
1,224,802
Messages
6,181,048
Members
453,014
Latest member
Chris258

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top