Why it works only on the first iteration? (1 Viewer)

prabha_friend

Prabhakaran Karuppaih
Local time
Today, 18:00
Joined
Mar 22, 2009
Messages
784
Code:
Sub Operation_PWA_Members()
Dim xlApp As New Excel.Application
xlApp.Visible = True
Dim xlSheet As Worksheet
Set xlSheet = xlApp.Workbooks.Open(Replace(ThisDocument.Path & "\" & ThisDocument.Name, "docm", "xlsm")).Worksheets(1)
With xlSheet
    .Range("A1").Value = "Font_Name"
    For Each Character In ThisDocument.Characters
        With Character
            Dim ArrayFromFilter As Variant
            ArrayFromFilter = Filter(Array(xlSheet.UsedRange.Value), .Font.Name)
            If Not IsEmpty(ArrayFromFilter) Then
                xlSheet.Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 0).Value = .Font.Name
            End If
        End With
    Next Character
End With
End Sub

How to make it work on the following iterations too... Please Help.

With Hope,
Prabhakaran

p.s:
I am getting a 'Type-Mismatch' Error from the 2nd iteration itself.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:30
Joined
Feb 28, 2001
Messages
27,186
Invariably you and others do this same thing all the time. In common language, what is the purpose/intent/goal of the code snippet? We need to know what you were intending to do.

If this is suppose to make mayonnaise then it isn't going to work. (No eggs.) But if it is supposed to do something else, maybe it could be made to work and we could advise you better on how to do that.
 

prabha_friend

Prabhakaran Karuppaih
Local time
Today, 18:00
Joined
Mar 22, 2009
Messages
784
There is a word document presently typed in our mother tongue (Tamil) but by using different encoding methods at different places.
My Goal is to convert the whole document in Unicode.
That's why First I am making list of fonts used in the present document. To make a list in excel (unique list) we need to know whether the current character's font is already listed or not. That's why Filter Function.
Filter function (Visual Basic for Applications) | Microsoft Learn
 

sonic8

AWF VIP
Local time
Today, 14:30
Joined
Oct 27, 2015
Messages
998
How to make it work on the following iterations too... Please Help.
[...]
I am getting a 'Type-Mismatch' Error from the 2nd iteration itself.
I don't know where the Type-Mismatch is coming from.
However, there is a general error in you code. IsEmpty(ArrayFromFilter) will never be true. Even if the Filter function found nothing, it will return an empty array. IsEmpty is checking for uninitialized Variant variables! If a variable contains an empty array, it is not uninitialized!
 

prabha_friend

Prabhakaran Karuppaih
Local time
Today, 18:00
Joined
Mar 22, 2009
Messages
784
Code:
Dim ArrayFromFilter As Variant
ArrayFromFilter = Filter(Array(xlSheet.UsedRange.Value), .Font.Name)
2nd line in 2nd iteration throws 'Type-Mismatch'
 

sonic8

AWF VIP
Local time
Today, 14:30
Joined
Oct 27, 2015
Messages
998
Code:
Dim ArrayFromFilter As Variant
ArrayFromFilter = Filter(Array(xlSheet.UsedRange.Value), .Font.Name)
2nd line in 2nd iteration throws 'Type-Mismatch'
Most likely because UsedRange.Value then returns an array of values, which is not what the Array functions expects.
 

cheekybuddha

AWF VIP
Local time
Today, 13:30
Joined
Jul 21, 2014
Messages
2,280
Code:
' ...
        With Character
            Dim ArrayFromFilter As Variant
            ArrayFromFilter = Filter(Array(xlSheet.UsedRange.Value), .Font.Name)
' ...
Is this code in a Word document module?

Is Filter() a Word VBA method?
 

Josef P.

Well-known member
Local time
Today, 14:30
Joined
Feb 2, 2023
Messages
826
Filter is a VBA function. https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/filter-function

Example:
Code:
Dim filteredValues() As String
Dim SourceArray() As Variant

SourceArray = Array("abc", "xyz", "cba", "xxx")

filteredValues = Filter(SourceArray, "a", True, vbTextCompare)

Dim x As Variant
For Each x In filteredValues
   Debug.Print x
Next

This will not work: (see #5 + answer from Philipp #6)
Code:
Dim filteredValues() As String
Dim SourceArray() As Variant

SourceArray = Array("abc", "xyz", "cba", "xxx")

filteredValues = Filter(Array(SourceArray), "a", True, vbTextCompare) ' <--- Array + 1. item of array is an array

Dim x As Variant
For Each x In filteredValues
   Debug.Print x
Next
 

Josef P.

Well-known member
Local time
Today, 14:30
Joined
Feb 2, 2023
Messages
826
I'm picky about language elements: ;)
Filter is a function inside Strings module of VBA not from any Office library.

VBA-Strings-Filter.png
 

Users who are viewing this thread

Top Bottom