<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>VBA-JSON</title>
	<atom:link href="https://javeo.jp/tag/vba-json/feed/" rel="self" type="application/rss+xml" />
	<link>https://javeo.jp</link>
	<description>ほどほどレベルのプログラミング</description>
	<lastBuildDate>Sat, 16 Jul 2022 23:00:00 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://javeo.jp/wp-content/uploads/2025/08/cropped-ExcelPython_future-32x32.jpg</url>
	<title>VBA-JSON</title>
	<link>https://javeo.jp</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【VBA】VBA-JSONで「KeyNotFoundError」エラーが発生する</title>
		<link>https://javeo.jp/vba-json-error/</link>
					<comments>https://javeo.jp/vba-json-error/#comments</comments>
		
		<dc:creator><![CDATA[ジャベ雄]]></dc:creator>
		<pubDate>Sat, 16 Jul 2022 23:00:00 +0000</pubDate>
				<category><![CDATA[VBA・Excel]]></category>
		<category><![CDATA[SeleniumBasic]]></category>
		<category><![CDATA[VBA-JSON]]></category>
		<guid isPermaLink="false">https://javeo.jp/?p=707</guid>

					<description><![CDATA[目次 ある日突然エラーが発生した実際のエラー発生箇所原因は「Dictionary」でした解決方法明示的に「Microsoft Scripting Runtime」のDictionaryとして宣言する参照設定の優先順位を変 [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="codoc-evacuations" style="display:none;" data-shortcode=""></div>
<div class="wp-block-cocoon-blocks-info-box block-box primary-box">
<p>VBA-JSONを使ってる時にエラー「KeyNotFoundError」が発生して解決に結構時間がかかったので備忘半分で原因と解消方法まとめてみました</p>



<p>VBA-JSONは何って話はコチラから</p>



<figure class="wp-block-embed is-type-wp-embed"><div class="wp-block-embed__wrapper">

<a rel="noopener follow noreferrer" target="_blank" href="https://javeo.jp/vba-json/" title="【VBA】VBA-JSONのインストール方法と初心者向けの簡単な使い方" class="blogcard-wrap internal-blogcard-wrap a-wrap cf"><div class="blogcard internal-blogcard ib-left cf"><div class="blogcard-label internal-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail internal-blogcard-thumbnail"><img fetchpriority="high" decoding="async" width="320" height="198" src="https://javeo.jp/wp-content/uploads/2022/02/VBA-JSON-320x198.png" class="blogcard-thumb-image internal-blogcard-thumb-image wp-post-image" alt="" srcset="https://javeo.jp/wp-content/uploads/2022/02/VBA-JSON-320x198.png 320w, https://javeo.jp/wp-content/uploads/2022/02/VBA-JSON-240x148.png 240w, https://javeo.jp/wp-content/uploads/2022/02/VBA-JSON-640x396.png 640w" sizes="(max-width: 320px) 100vw, 320px" /></figure><div class="blogcard-content internal-blogcard-content"><div class="blogcard-title internal-blogcard-title">【VBA】VBA-JSONのインストール方法と初心者向けの簡単な使い方</div><div class="blogcard-snippet internal-blogcard-snippet">VBAでAPIを叩くときとかでJSON形式のデータを取り扱う時に簡単にデータ取得ができるモジュールはVBA-JSONです</div></div><div class="blogcard-footer internal-blogcard-footer cf"><div class="blogcard-site internal-blogcard-site"><div class="blogcard-favicon internal-blogcard-favicon"><img decoding="async" src="https://www.google.com/s2/favicons?domain=https://javeo.jp" alt="" class="blogcard-favicon-image internal-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain internal-blogcard-domain">javeo.jp</div></div><div class="blogcard-date internal-blogcard-date"><div class="blogcard-post-date internal-blogcard-post-date">2025.08.16</div></div></div></div></a>
</div></figure>
</div>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">ある日突然エラーが発生した</a></li><li><a href="#toc2" tabindex="0">実際のエラー発生箇所</a></li><li><a href="#toc3" tabindex="0">原因は「Dictionary」でした</a></li><li><a href="#toc4" tabindex="0">解決方法</a><ol><li><a href="#toc5" tabindex="0">明示的に「Microsoft Scripting Runtime」のDictionaryとして宣言する</a></li><li><a href="#toc6" tabindex="0">参照設定の優先順位を変える</a></li></ol></li><li><a href="#toc7" tabindex="0">あとがき</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">ある日突然エラーが発生した</span></h2>



<p>まずはエラーメッセージから</p>



<figure class="wp-block-image size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_21h10_29.png"><img decoding="async" width="427" height="194" src="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_21h10_29.png" alt="" class="wp-image-709" srcset="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_21h10_29.png 427w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_21h10_29-300x136.png 300w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_21h10_29-150x68.png 150w" sizes="(max-width: 427px) 100vw, 427px" /></a></figure>



<div class="hcb_wrap"><pre class="prism undefined-numbers lang-vba" data-file="Err.Description" data-lang="Visual Basic + VBA"><code>KeyNotFoundError
Dictionary key not found: &lt;KeyName&gt;</code></pre></div>



<p>そのまま受け取るとDictionary型の変数にKeyが無いと</p>



<p>ピンと来る理由もないので実際のエラーソースを見てみる</p>



<h2 class="wp-block-heading"><span id="toc2">実際のエラー発生箇所</span></h2>



<div class="hcb_wrap"><pre class="prism undefined-numbers lang-vba" data-lang="Visual Basic + VBA" data-line="27"><code>Private Function json_ParseObject(json_String As String, ByRef json_Index As Long) As Dictionary
    Dim json_Key As String
    Dim json_NextChar As String

    Set json_ParseObject = New Dictionary
    json_SkipSpaces json_String, json_Index
    If VBA.Mid$(json_String, json_Index, 1) &lt;&gt; &quot;{&quot; Then
        Err.Raise 10001, &quot;JSONConverter&quot;, json_ParseErrorMessage(json_String, json_Index, &quot;Expecting &#39;{&#39;&quot;)
    Else
        json_Index = json_Index + 1

        Do
            json_SkipSpaces json_String, json_Index
            If VBA.Mid$(json_String, json_Index, 1) = &quot;}&quot; Then
                json_Index = json_Index + 1
                Exit Function
            ElseIf VBA.Mid$(json_String, json_Index, 1) = &quot;,&quot; Then
                json_Index = json_Index + 1
                json_SkipSpaces json_String, json_Index
            End If

            json_Key = json_ParseKey(json_String, json_Index)
            json_NextChar = json_Peek(json_String, json_Index)
            If json_NextChar = &quot;[&quot; Or json_NextChar = &quot;{&quot; Then
                Set json_ParseObject.Item(json_Key) = json_ParseValue(json_String, json_Index)
            Else
                json_ParseObject.Item(json_Key) = json_ParseValue(json_String, json_Index)
            End If
        Loop
    End If
End Function</code></pre></div>



<p>実際にエラーになるのは27行目の部分</p>



<p>なにもおかしなソースではないし、そもそも今までエラーにならなかったし同じVBA-JSONを使っているのに急にエラーになってしまう理由がやっぱりわからない・・・</p>



<h2 class="wp-block-heading"><span id="toc3">原因は「Dictionary」でした</span></h2>



<p>まず前提としてDictionary型と言われると連想することは参照設定だと「M<span class="bold">icrosoft Scripting Runtime</span>」のことを指すと思いますし、私も思ってました</p>



<p>が、実は「<strong>Selenium Type Library</strong>」の中にもDictionary型があったのです</p>



<figure class="wp-block-image aligncenter size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h21_00.png"><img decoding="async" width="838" height="517" src="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h21_00.png" alt="" class="wp-image-714" srcset="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h21_00.png 838w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h21_00-300x185.png 300w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h21_00-150x93.png 150w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h21_00-768x474.png 768w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h21_00-240x148.png 240w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h21_00-320x198.png 320w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h21_00-640x396.png 640w" sizes="(max-width: 838px) 100vw, 838px" /></a><figcaption class="wp-element-caption">「Microsoft Scripting Runtime」のDictionary</figcaption></figure>



<figure class="wp-block-image aligncenter size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h20_53.png"><img decoding="async" width="838" height="517" src="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h20_53.png" alt="" class="wp-image-713" srcset="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h20_53.png 838w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h20_53-300x185.png 300w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h20_53-150x93.png 150w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h20_53-768x474.png 768w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h20_53-240x148.png 240w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h20_53-320x198.png 320w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h20_53-640x396.png 640w" sizes="(max-width: 838px) 100vw, 838px" /></a><figcaption class="wp-element-caption">「Selenium Type Library」のDictionary</figcaption></figure>



<p>つまり「Selenium Type Library」のDictionaryとして動作していたのでエラーになっていたわけです</p>



<h2 class="wp-block-heading"><span id="toc4">解決方法</span></h2>



<p>原因がわかれば対応方法は難しくなくて2パターンありますので状況に応じてお好きな方を</p>



<h3 class="wp-block-heading"><span id="toc5">明示的に「Microsoft Scripting Runtime」のDictionaryとして宣言する</span></h3>



<p>※※個人的にはコチラをおススメ※※</p>



<p>「Dictionary」を「Scripting.Dictionary」に置換してあげればOK</p>



<p>ちなみにVBA-JSONの中には別の箇所でも「Dictionary」の単語があってそこは置換すると別のエラーになるので「json_ParseObject」の型とそのソース内の1箇所だけを置換</p>



<div class="hcb_wrap"><pre class="prism undefined-numbers lang-vba" data-lang="Visual Basic + VBA" data-line="1,5"><code>Private Function json_ParseObject(json_String As String, ByRef json_Index As Long) As Scripting.Dictionary
    Dim json_Key As String
    Dim json_NextChar As String

    Set json_ParseObject = New Scripting.Dictionary
    json_SkipSpaces json_String, json_Index
    If VBA.Mid$(json_String, json_Index, 1) &lt;&gt; &quot;{&quot; Then
        Err.Raise 10001, &quot;JSONConverter&quot;, json_ParseErrorMessage(json_String, json_Index, &quot;Expecting &#39;{&#39;&quot;)
    Else
        json_Index = json_Index + 1

        Do
            json_SkipSpaces json_String, json_Index
            If VBA.Mid$(json_String, json_Index, 1) = &quot;}&quot; Then
                json_Index = json_Index + 1
                Exit Function
            ElseIf VBA.Mid$(json_String, json_Index, 1) = &quot;,&quot; Then
                json_Index = json_Index + 1
                json_SkipSpaces json_String, json_Index
            End If

            json_Key = json_ParseKey(json_String, json_Index)
            json_NextChar = json_Peek(json_String, json_Index)
            If json_NextChar = &quot;[&quot; Or json_NextChar = &quot;{&quot; Then
                Set json_ParseObject.Item(json_Key) = json_ParseValue(json_String, json_Index)
            Else
                json_ParseObject.Item(json_Key) = json_ParseValue(json_String, json_Index)
            End If
        Loop
    End If
End Function</code></pre></div>



<h3 class="wp-block-heading"><span id="toc6">参照設定の優先順位を変える</span></h3>



<p>半分原因の話にもなりますが「Dictionary」と宣言した時に「Microsoft Scripting Runtime」側になるように優先順位を変えるだけ</p>



<figure class="wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="wp-block-image size-large"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h38_15.png"><img decoding="async" width="455" height="317" data-id="716" src="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h38_15.png" alt="" class="wp-image-716" srcset="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h38_15.png 455w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h38_15-300x209.png 300w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h38_15-150x105.png 150w" sizes="(max-width: 455px) 100vw, 455px" /></a><figcaption class="wp-element-caption">Selenium Type Libraryが優先されている</figcaption></figure>



<figure class="wp-block-image size-large"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h38_26.png"><img decoding="async" width="455" height="317" data-id="717" src="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h38_26.png" alt="" class="wp-image-717" srcset="https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h38_26.png 455w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h38_26-300x209.png 300w, https://javeo.jp/wp-content/uploads/2022/07/2022-07-16_22h38_26-150x105.png 150w" sizes="(max-width: 455px) 100vw, 455px" /></a><figcaption class="wp-element-caption">Selenium Type Libraryが優先されている</figcaption></figure>
</figure>



<h2 class="wp-block-heading"><span id="toc7">あとがき</span></h2>



<p>APIとSeleniumBasicを使ったスクレイピングを併用したツールを作った時にこんな落とし穴があるとは知らず、解決するのに半日かかりました・・・</p>



<p>同じエラーに嵌った人の助けになれば幸いでございます</p>
]]></content:encoded>
					
					<wfw:commentRss>https://javeo.jp/vba-json-error/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
