<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: VBA/Macro's help (newby alert!) in ArcObjects SDK Questions</title>
    <link>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664158#M17810</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Wayne, I will play around with it a bit more today and let you know how I get on. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Dan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 23 Jan 2013 07:30:10 GMT</pubDate>
    <dc:creator>DanielHardwick</dc:creator>
    <dc:date>2013-01-23T07:30:10Z</dc:date>
    <item>
      <title>VBA/Macro's help (newby alert!)</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664156#M17808</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi guys, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;First post here... I work in GIS, using ArcGIS 9.2, and I am a complete newby to VBA macro's scripts etc. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I hope this is the right place to post this (did a search for VBA and this thread seem to come up). &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Basically I only started using VBA because a user wanted to add a count of unique values to one of his shapefiles, and then the count to appear in his legend. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The count field displays the count when in layer properies&amp;gt;unique values, but as soon as you come out of that the count ends. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So I found a macro which takes existing Unique Value and calculates feature counts for each value, adding the count to each Class Label. Class Labels appear in the ArcMap Table of Contents and Legend.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;After a bit of trial and error I got the script to work. The problem is the script doesn't work on every shapefile, it works on some but not others. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the script; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
Const DESCRIP_FIELD = "STATE_NAME"
Const CONCATENATE_TO_BUILD_DESCRIPTION = True
Const CONCAT_CHAR = vbNewLine

Option Explicit

Sub UniqueValues_LabelCount_and_DescripFromField()

Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap
Dim pGeoLayer As IGeoFeatureLayer
Set pGeoLayer = pMap.Layer(0)

If Not TypeOf pGeoLayer.Renderer Is IUniqueValueRenderer Then
MsgBox "Current symbology is not Unique values. Exiting."
Exit Sub
End If

Dim pUVRend As IUniqueValueRenderer
Set pUVRend = pGeoLayer.Renderer

If pUVRend.FieldCount &amp;gt; 1 Then
MsgBox "Current Unique values symbology is based on multiple fields. Exiting."
Exit Sub
End If

Dim sFieldName As String
sFieldName = pUVRend.Field(0)

Dim i As Integer
Dim varValue As Variant

Dim pFeatClass As IFeatureClass
Set pFeatClass = pGeoLayer.FeatureClass

Dim varLabelDescrip As Variant
For i = 0 To pUVRend.ValueCount - 1
varValue = pUVRend.Value(i)
varLabelDescrip = GetLabelDescription(pFeatClass, pUVRend.Field(0), varValue)
pUVRend.Label(varValue) = varLabelDescrip(0)
pUVRend.Description(varValue) = varLabelDescrip(1)
Next i

pDoc.ActiveView.ContentsChanged
pDoc.UpdateContents
pDoc.ActiveView.Refresh
End Sub

Private Function GetLabelDescription(pFeatClass As IFeatureClass, ValField As String, Value As Variant) As Variant
' returns an array of length 2
' (0) is the new label (string) appended with count of features
' (1) is the new descrip (string) driven from DESCRIP_FIELD

Dim pQueryFilter As IQueryFilter
Set pQueryFilter = New QueryFilter

pQueryFilter.WhereClause = ValField &amp;amp; " = '" &amp;amp; CStr(Value) &amp;amp; "'"
pQueryFilter.AddField DESCRIP_FIELD
Dim pFeatCursor As IFeatureCursor
Set pFeatCursor = pFeatClass.Search(pQueryFilter, False)

' ---------------------------------------------------------
' Description
Dim pFeat As IFeature
Dim sDescrip As String
Dim iDescrip As Integer
iDescrip = pFeatClass.Fields.FindField(DESCRIP_FIELD)
Set pFeat = pFeatCursor.NextFeature

Dim iCount As Integer
iCount = 0
Dim bCountsDetermined As Boolean
bCountsDetermined = False

If CONCATENATE_TO_BUILD_DESCRIPTION Then
bCountsDetermined = True
Do While Not pFeat Is Nothing
iCount = iCount + 1
If sDescrip &amp;lt;&amp;gt; "" Then sDescrip = sDescrip + CONCAT_CHAR
sDescrip = sDescrip + CStr(pFeat.Value(iDescrip)) ' get value from DESCRIP_FIELD
Set pFeat = pFeatCursor.NextFeature
Loop

Else ' only get descrip from first feature found
If Not pFeat Is Nothing Then
sDescrip = CStr(pFeat.Value(iDescrip)) ' get value from DESCRIP_FIELD
End If

End If

' ---------------------------------------------------------
' Label
If Not bCountsDetermined Then
' optimization: re-query only if we don't
' already have the counts from above
iCount = pFeatClass.FeatureCount(pQueryFilter)
End If
Dim sLabel As String
sLabel = Value &amp;amp; " (" &amp;amp; iCount &amp;amp; ") "

' ---------------------------------------------------------
' setup return array and return
Dim sReturnArray(2) As String
sReturnArray(0) = sLabel
sReturnArray(1) = sDescrip

GetLabelDescription = sReturnArray

End Function&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I modify the constants for my particular data and situation (so essentially change 'STATE_NAME' to whatever the field im using is), it worked for the particular shapefile our user was using, but out of interest I tried it with a few other shapefiles, and it works for some and not others, the error message sometimes varies, but the latest one read ' Run-time error '-2147220985 (80040207) Automation error. The owner SID on a per-user subscription doesn't exist'. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Like I said above, I am a complete newby to this, and only started using it last week... it could be something obvious, so apologies if so. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help appreciated &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Dan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Jan 2013 13:43:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664156#M17808</guid>
      <dc:creator>DanielHardwick</dc:creator>
      <dc:date>2013-01-22T13:43:35Z</dc:date>
    </item>
    <item>
      <title>Re: VBA/Macro's help (newby alert!)</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664157#M17809</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Pretty certain it's your query encountering numeric values and not treating them as text properly...haven't tested this though.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Sounds like you can override the 'coercion'?&amp;nbsp; ...kind of 'intercept' the handoff of this value and convert to string before the error can be generated - it's an old post, but see:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The Owner SID on a per user subscription doesn't exist &lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://forums.esri.com/Thread.asp?c=93&amp;amp;f=1148&amp;amp;t=209335"&gt;http://forums.esri.com/Thread.asp?c=93&amp;amp;f=1148&amp;amp;t=209335&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Particularly, the Oct 2008 post by Sebastian Good:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"...encountered this error when constructing a &lt;/SPAN&gt;&lt;STRONG&gt;query filter&lt;/STRONG&gt;&lt;SPAN&gt; for a shape file where the data type of the columns did not match, i.e. 'A&amp;gt;B' where A is a numeric column, and B is a text column. Normal SQL rules allow type coercion so I would expect ESRI to 'try' anyway (and in this case, column B was indeed all numbers, though in a text column), but it reported this error instead. This was under 9.2 SP4. &lt;/SPAN&gt;&lt;STRONG&gt;My solution was to evaluate the predicate myself in the cursor&lt;/STRONG&gt;&lt;SPAN&gt;."&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Jan 2013 14:45:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664157#M17809</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-01-22T14:45:02Z</dc:date>
    </item>
    <item>
      <title>Re: VBA/Macro's help (newby alert!)</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664158#M17810</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Wayne, I will play around with it a bit more today and let you know how I get on. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Dan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Jan 2013 07:30:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664158#M17810</guid>
      <dc:creator>DanielHardwick</dc:creator>
      <dc:date>2013-01-23T07:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: VBA/Macro's help (newby alert!)</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664159#M17811</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well got this script to work, but it wasnt using the tips from above... &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One of error messages related to 'Layer 0', and I noticed that the problem seemed to occur (in this particular isntance) to a shapefile I had half way down in my TOC, so I dragged the shapefile to the top of my TOC, and ran the macro again, and it worked. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have tested this with another project, and it only seems to work when the shapefile is first in the TOC. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It's not something we will use everyday, so this solution will do for now! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Dan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2013 09:45:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664159#M17811</guid>
      <dc:creator>DanielHardwick</dc:creator>
      <dc:date>2013-01-25T09:45:24Z</dc:date>
    </item>
    <item>
      <title>Re: VBA/Macro's help (newby alert!)</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664160#M17812</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Well got this script to work, but it wasnt using the tips from above... &lt;BR /&gt;&lt;BR /&gt;One of error messages related to 'Layer 0', and I noticed that the problem seemed to occur (in this particular isntance) to a shapefile I had half way down in my TOC, so I dragged the shapefile to the top of my TOC, and ran the macro again, and it worked. &lt;BR /&gt;&lt;BR /&gt;I have tested this with another project, and it only seems to work when the shapefile is first in the TOC. &lt;BR /&gt;&lt;BR /&gt;It's not something we will use everyday, so this solution will do for now! &lt;BR /&gt;&lt;BR /&gt;Dan&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;That's because your code is written to operate on the first layer in the TOC.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Set pGeoLayer = pMap.Layer(0)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:04:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664160#M17812</guid>
      <dc:creator>NeilClemmons</dc:creator>
      <dc:date>2021-12-12T04:04:45Z</dc:date>
    </item>
    <item>
      <title>Re: VBA/Macro's help (newby alert!)</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664161#M17813</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ah, those cryptic error messages...yes, ArcObjects VBA is fun, isn't it?&amp;nbsp; And therein lies the bonus round, in your statements "...error message sometimes varies..." and "One of error messages..." -- just trying to isolate them all is a daunting (sometimes futile) task!&amp;nbsp; So you get kudos for being one of the brave souls persistent enough to delve further into the ArcObjects world.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If this adds any convenience to what you are doing, you do not have to move your layers around - there are functions for searching for the layer in ArcMap's TOC, for example it's relatively straightforward to search by layer name, entering some variation of the famous 'FindLayerByName' function, as in this simple version:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
Function FindLayerByName (pMap as IMap, sName as String) as ILayer
&amp;nbsp; Dim i as Integer
&amp;nbsp; For i = 0 to pMap.LayerCount -1
&amp;nbsp;&amp;nbsp;&amp;nbsp; If pMap.Layer(i).Name = sName Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Set FindLayerByName = pMap.Layer(i)
&amp;nbsp;&amp;nbsp;&amp;nbsp; End If
&amp;nbsp; Next
End Function
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you aren't familiar with this, you can simply paste this after you main subroutine.&amp;nbsp; How does it work?&amp;nbsp; pMap and sName are the map (an object) and layer name (a text string) that is passed in from your sub and passed back out to your sub as a result is a ILayer 'handle' or reference you need to the layer object.&amp;nbsp; The way you have your sub set up, you'd probably hardcode the string at the beginning for convenience, sort of like you have with your defined constants.&amp;nbsp; The beauty of it is, you are already defining pMap in your sub and the ILayer passed back out can be directly assigned to your pGeoLayer object.&amp;nbsp; [Your critical line is "Set pGeoLayer = pMap.Layer(0)"]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps.&amp;nbsp; ArcObjects is powerful coding.&amp;nbsp; Enjoy.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Oh, and honorable mention goes to &lt;/SPAN&gt;&lt;STRONG&gt;Neil Clemmons&lt;/STRONG&gt;&lt;SPAN&gt; - I don't work much with VBA at the moment and frankly don't remember much but know where to look.&amp;nbsp; You are working with 9.2 I think you said, so I searched the forum archives and Neil's post in 2005 was my 1st clue:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Find layer in TOC by name, then insert features&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://forums.esri.com/Thread.asp?c=93&amp;amp;f=992&amp;amp;t=165984&amp;amp;mc=1#msgid487921" rel="nofollow noopener noreferrer" target="_blank"&gt;http://forums.esri.com/Thread.asp?c=93&amp;amp;f=992&amp;amp;t=165984&amp;amp;mc=1#msgid487921&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, if you need it, the documentation library is here - old info, but still very potent:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://edndoc.esri.com/arcobjects/9.2/welcome.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;http://edndoc.esri.com/arcobjects/9.2/welcome.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:04:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664161#M17813</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-12T04:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: VBA/Macro's help (newby alert!)</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664162#M17814</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you Wayne, a lot of that stuff is taking a while to settle in, as I am not familiar with VBA to be honest. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But it is certainly helping me to understand it more. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I gathered (after failing many times) that it was pointing to 'Layer(0)', but I had no clue if I could just go in and put something in like 'Layer(0)ANDLayer(1)' for example. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I will play around with 'FindLayerByName' and see how I get on&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2013 13:00:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/vba-macro-s-help-newby-alert/m-p/664162#M17814</guid>
      <dc:creator>DanielHardwick</dc:creator>
      <dc:date>2013-01-25T13:00:13Z</dc:date>
    </item>
  </channel>
</rss>

