View Single Post
 
Old 06-02-2021, 04:41 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The bookmark must be there otherwise it wouldn't be in the loop, however the bookmark may not include a table cell so you should avoid that complication and just write to the bookmarked range instead. Note that doing this deletes the bookmark so you need to re-add it at the end (like you do).

Is the RangeValue function looking Word or Excel? I presume it must be in Excel and it is not clear that the function tests whether the named range exists in Excel so that could fail if that is the case.
Code:
Dim aRng as Range
For a = wDoc.Bookmarks.Count To 1 Step -1
  Set item = wDoc.Bookmarks(a)
  itemName = item.Name
  aRng = item.Range
  fValue = RangeValue(itemName)
  If IsNumeric(fValue) Then
     Select Case formatCell
         Case "Percentage", "0%", "0.0%", "0.00%", "0.00""x"""
             aRng.Text = Format(fValue, formatCell)
         Case Else
             aRng.Text = Format(fValue, "#,##0;(#,##0);"" - """)
     End Select
  Else
     aRng.Text = fValue
  End If
  wDoc.Bookmarks.Add itemName, aRng 
Next a
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote