Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-01-2021, 07:52 AM
JamesWood JamesWood is offline Counting numbers in Word VBA Windows 10 Counting numbers in Word VBA Office 2019
Advanced Beginner
Counting numbers in Word VBA
 
Join Date: Nov 2020
Posts: 37
JamesWood is on a distinguished road
Question Counting numbers in Word VBA

Hi guys


Strange one here, but any idea how to COUNTA in Word VBA, not Excel? Or the equivalent? I'm aware this sub is totally wrong but hopefully it'll get across what I want to do:


Public Function MergeFromToTotal() As String




MergeFromToTotal = CountA(Range(Me.FromThis.Value:Me.ToThis.Value))


End Function


Where Me.FromThis.Value is a textbox that might contain any number, and the same for the ToThis value. If it was say 3-6 then I'd want MergeFromToTotal to return with 4 (e.g. 3, 4, 5, 6).


Hope that makes sense?


Thanks a lot!
James
Reply With Quote
  #2  
Old 06-01-2021, 02:50 PM
macropod's Avatar
macropod macropod is offline Counting numbers in Word VBA Windows 10 Counting numbers in Word VBA Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Word has no equivalent to Excel's COUNTA function. In any event, Excel's COUNTA worksheet function is not a method that can be used outside the Excel worksheet context. Besides which, Excel's COUNTA function doesn't do anything like what you envisage.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-01-2021, 04:36 PM
Guessed's Avatar
Guessed Guessed is offline Counting numbers in Word VBA Windows 10 Counting numbers in Word VBA Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
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

I think you need to iterate through the array and test each value
Code:
Sub CountIt()
  Dim arr() As String, i As Integer, iCount As Integer
  arr = Split("1,Dog,2,Apple,3.7,22,Green", ",")
  For i = LBound(arr) To UBound(arr)
    If IsNumeric(arr(i)) Then iCount = iCount + 1
  Next i
  MsgBox "Array contains " & iCount & " numbers", vbOKOnly
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 06-02-2021, 12:41 AM
JamesWood JamesWood is offline Counting numbers in Word VBA Windows 10 Counting numbers in Word VBA Office 2019
Advanced Beginner
Counting numbers in Word VBA
 
Join Date: Nov 2020
Posts: 37
JamesWood is on a distinguished road
Default

Hmm okay! This is essentially for a progress bar on a mail merge, so here's my dilemma. If recordNumber = 3 To 7 (potentially, if that's the merge range), how can I get a progress bar to read that? Stuck on this!


Me.ProgressBar1.Value = Round(100 / MergeCountTotal * recordNumber)


the above obviously is actually saying times 3, and the next would be times 4, until it reaches 7... but for the percentage to make sense, it needs to see 3 as actually being 1, of 4... damn this is confusing haha
Reply With Quote
  #5  
Old 06-02-2021, 05:23 AM
Guessed's Avatar
Guessed Guessed is offline Counting numbers in Word VBA Windows 10 Counting numbers in Word VBA Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
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

Perhaps you could try

Code:
If iStart = 0 then iStart = recordNumber
Me.ProgressBar1.Value = Round(100*(recordNumber-iStart)/MergeCountTotal)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 06-02-2021, 06:14 AM
JamesWood JamesWood is offline Counting numbers in Word VBA Windows 10 Counting numbers in Word VBA Office 2019
Advanced Beginner
Counting numbers in Word VBA
 
Join Date: Nov 2020
Posts: 37
JamesWood is on a distinguished road
Talking

I've managed to figure it out guys!!

So just before the merge starts, I've got this:


Dim n As Integer
n = 1



Then the merge loop starts:

For recordNumber = MergeStart To MergeFinish



And then just before "Next recordNumber", I now have


'Progress bar based on how many records in merge
If Me.OptionButtonAll.Value = True Or Me.OptionButtonCurrent.Value = True Or Me.OptionButtonFromTo.Value = True Then
Me.ProgressBar1.Value = Round(100 / MergeCountTotal * n)
Me.ProgressPercentage.Caption = Me.ProgressBar1.Value & "%"
End If
n = n + 1


This way it adds +1 to n just before it loops again. It's working perfectly now for calculating the correct percentage based on how many merge records it's processing. Thank f**k. This was driving me wild!


Thanks for your help though guys!
Reply With Quote
Reply

Tags
count, counta

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting footnotes in Word Sylvain Word VBA 0 03-10-2021 06:45 AM
Counting Consecutive Periods In Rows Using Numbers Yaaseen23 Excel 18 03-24-2017 11:15 AM
counting "x" in a cell and sum the numbers in weekday rayramirezdvm Excel 4 04-02-2015 02:58 PM
Counting unique visitors by ward, counting monthly visits by status, editing existing workbook JaxV Excel 9 11-14-2014 12:25 AM
Word counting macro for newly added portion? New Daddy Word VBA 24 09-30-2013 07:30 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:05 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