Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-12-2021, 01:21 PM
jeffreybrown jeffreybrown is offline VBA insert quick part formula and format as percentage Windows 10 VBA insert quick part formula and format as percentage Office 2016
Expert
VBA insert quick part formula and format as percentage
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default VBA insert quick part formula and format as percentage

It seems I've looked everywhere, but can't seem to find the right way to format a quick part formula as percentage. Here is what I've tried.

Code:
Sub test()
    With ActiveDocument.Tables(1)
        .Cell(5, 5).Formula Formula:="=A2/B2*100"
        .Cell(5, 5).Range.Text = Format(.Cell(5, 5), "0.00%")
    End With
End Sub
I used the macro recorder and came up with the below, works fine, but of course it makes me select the table cell first.

Code:
Sub Macro3()
    Selection.InsertFormula Formula:="=A2/B2*100", NumberFormat:="0.00%"
End Sub

Reply With Quote
  #2  
Old 03-12-2021, 02:11 PM
macropod's Avatar
macropod macropod is offline VBA insert quick part formula and format as percentage Windows 10 VBA insert quick part formula and format as percentage Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

There are various ways of doing this. For example:
Code:
ActiveDocument.Tables(1).Cell(5, 5).Formula Formula:="=A2/B2*100 \# ""0.00"""
Code:
With ActiveDocument
  .Fields.Add Range:=.Tables(1).Cell(5, 5).Range, Type:=wdFieldEmpty, Text:="=A2/B2*100 \# 0.00%", PreserveFormatting:=False
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-12-2021, 02:14 PM
jeffreybrown jeffreybrown is offline VBA insert quick part formula and format as percentage Windows 10 VBA insert quick part formula and format as percentage Office 2016
Expert
VBA insert quick part formula and format as percentage
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Thanks Paul. So in the end, it was the double quotes I was missing.
Reply With Quote
  #4  
Old 03-12-2021, 02:52 PM
macropod's Avatar
macropod macropod is offline VBA insert quick part formula and format as percentage Windows 10 VBA insert quick part formula and format as percentage Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I'll have to take your word for that, since you didn't post the code that wasn't working...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA insert quick part formula and format as percentage Insert landscape page as quick part melsmiff12 Word 1 04-28-2017 12:51 PM
Quick Part Galleries dfmdfm Word 1 03-27-2017 04:45 PM
VBA insert quick part formula and format as percentage Quick part building blocks - Formula lukegwilson96 Word 6 12-13-2016 10:46 AM
VBA insert quick part formula and format as percentage Insert quick part when cell contains a value winstond Word VBA 1 09-30-2014 11:45 AM
How to Rename a Quick Part sleake Word 14 10-14-2013 09:21 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:17 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft