VBA String compare in Mac

If you use equal comparison with strings, that contain all none ASCII characters in Mac Excel VBA 2011, then it will be always true if the strings have the same length:
Word1=word2 is always returns TRUE if len(word1)=len(word2)
To avoid this used strcomp(word1,word2,vbtextcompare) function stead!

Use Find object to manipulate all appearances of a text of a document

Using Find method of Select object is very effective way to change all appearance of a text in a document. For example you may want to capitalize all the first case after ” in the document. Below is pseudo code to demo it:

Sub convertToUppercases() 
   'Goto start of the document 
    Selection.HomeKey Unit:=wdStory 
    switchCases False 
End Sub 
Sub convertToLowercases() 
    'Goto start of the document 
     Selection.HomeKey Unit:=wdStory 
     switchCases True 
End Sub 

Sub switchCases(ByVal bToLowerCase As Boolean) 
    Dim sText As String 
    Dim lCount As Long 
    Selection.Find.ClearFormatting 'Clear current find  
    With Selection.Find  
        .Text = <put some text need to be found here>   
        .Replacement.Text = ""   
        .Forward = True   
        .Wrap = wdFindContinue   
        .Format = True   
        .MatchCase = False   
        .MatchWholeWord = False   
        .MatchAllWordForms = False   
        .MatchSoundsLike = False   
        .MatchWildcards = True   
    End With    
        If Selection.Find.Found Then     
            if bToLowerCase then     
                <put the code need to be done here>     
                <put the code need to be done here>     
            end if     
        end if     
    Loop Until Not Selection.Find.Found     
End Sub

Add picture to bookmark

Sub UpdateBookmarkedImage(BmkNm As String, NewTxt As String)
Dim BmkRng As Range
With ActiveDocument
  If .Bookmarks.Exists(BmkNm) Then
    Set BmkRng = .Bookmarks(BmkNm).Range
    If  BmkRng.InlineShapes.Count >0 then 
    End if
    BmkRng.InlineShapes.AddPicture FileName:=NewTxt
    .Bookmarks.Add BmkNm, BmkRng
  End If
End With
Set BmkRng = Nothing
End Sub

Add links to cells with VBA

Sub addLinks()

‘ Converts each cell text of a selected range into a working hyperlink
For Each xCell In Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell

End Sub

Use Find method against Cells collection to detemine the last used Row, Column in Excel

Function lastUsedRow() As Long
On Error Resume Next
Dim lLastRow As Integer
lLastRow = Cells.Find(What:=”*”, after:=Range(“A1″), SearchOrder:=xlByRows, _
If lLastRow = 0 Then
lLastRow = 1
End If
lastUsedRow = lLastRow

End Function

Function lastUsedCol() As Long
On Error Resume Next
Dim lLastCol As Integer
lLastCol = Cells.Find(What:=”*”, after:=Range(“A1”), SearchOrder:=xlByColumns, _
If lLastCol = 0 Then
lLastCol = 1
End If
lastUsedCol = lLastCol

End Function

Prompt for a file name using VBA

Function prompt4FileName(Optional sFolder As String = vbNullString, Optional sDefaultName As String = "Untitled") As String
    Dim sMacScript As String
    Dim sFileName As String

    If sFolder = vbNullString Then 'Get user document folder
        sFolder = userDocumentFolder()
    End If
    If isMac Then
        sMacScript = "set sFile to (choose file name with prompt ""Name this file"" default name """ & _
        sDefaultName & vbLf & _
        """ default location the path to documents folder)" & vbLf & _
        "return sFile"
        sFileName = MacScript(sMacScript)
        sFileName = Application.GetSaveAsFilename(sDefaultName, "Excel Files,*.xls,Excel macro enable files,*.xlsm,All Files,*.*", , "Name this file", "Save as")    End If
    prompt4FileName = sFileName
End Function

Looking for a row using Find method against the Selection object

Function myFindRowPos(sText As Variant, oSheet As Worksheet, _
  Optional SearchDirection As XlSearchDirection = xlNext, _
  Optional SearchOrder As XlSearchOrder = xlByRows, _
  Optional sFindRange As String = "A1:A10000", _
  Optional LookAt As XlLookAt = xlWhole) As Long

  Dim lResult As Long, oRg As Range

  On Error GoTo errLabel

 Set oRg = Selection.Find(What:=sText, after:=ActiveCell, LookIn:=xlValues, LookAt:= _ LookAt, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _ False)   If Not oRg Is Nothing Then
     lResult = oRg.Row
     lResult = -1 'not found
        End IferrExit:
    myFindRowPos = lResult
    Set oRg = Nothing
    Exit Function
    lResult = -1
    Resume errExit
End Function