PDA

View Full Version : [SOLVED:] Toggle Text Case



Rob Conklin
02-09-2021, 02:19 PM
I have the below code from an Excel macro that I would like to use in Word. Can someone help modify the code so that it will work?


Sub ToggleTextCase()
'PURPOSE: Toggle selected cell text values between Upper, Lower, & Proper cases
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault


Dim FirstCellValue As String
Dim rng As Range
Dim cell As Range


'Optimize Code
Application.ScreenUpdating = False


'Was a Range Selected? (Error Handling)
If TypeName(Selection) <> "Range" Then
MsgBox "No cell range was selected!", vbCritical, "No Range Selected"
Exit Sub
End If


'Remove cells with formulas or cells that are hidden from selection
On Error Resume Next
If Selection.Cells.Count > 1 Then
Set rng = Selection.SpecialCells(xlCellTypeVisible)
Set rng = rng.SpecialCells(xlConstants)
Else
Set rng = ActiveCell
End If


'Store Current Case Structure of first cell in selection
FirstCellValue = rng.Cells(1, 1).Value

'Apply Case Change (based on first cell's current case structure)
If FirstCellValue = LCase(FirstCellValue) Then
'If Lowercased, make Propercased
For Each cell In rng.Cells
cell.Value = Application.WorksheetFunction.Proper(cell.Value)
Next cell

ElseIf FirstCellValue = UCase(FirstCellValue) Then
'If Uppercased, make Lowercased
For Each cell In rng.Cells
cell.Value = LCase(cell.Value)
Next cell

Else
'If Propercased, make Uppercased
For Each cell In rng.Cells
cell.Value = UCase(cell.Value)
Next cell

End If


End Sub


Any help would be GREATLY appreciated!!

Chas Kenyon
02-09-2021, 03:21 PM
I have the below code from an Excel macro that I would like to use in Word. Can someone help modify the code so that it will work?
***

Any help would be GREATLY appreciated!!

It would help if you would tell us the goal of your code. What is it you are trying to accomplish? Although both use vba, the object models of Excel and Word are different. Word has a much more versatile Replace function.

I confess that I did not read through your code. My coding skills in Word are upper-level beginner to moderate. In Excel, they are novice.

macropod
02-09-2021, 03:41 PM
To work in Word, any such macro would need to be re-written from scratch. Perhaps:

Sub Demo()
With Selection.Range
Select Case .Case
Case wdLowerCase
.Case = wdUpperCase
Case wdUpperCase
.Case = wdTitleWord
Case wdTitleWord
.Case = wdTitleSentence
Case wdTitleSentence
.Case = wdLowerCase
End Select
End With
End Sub

Rob Conklin
02-09-2021, 03:59 PM
My apologies Chas, I thought it was implied when I said that I wanted to use the the Excel module in Word. I do a lot of copy/paste in both Word and Excel, and unfortunately the source data is NEVER grammatically correct. So I spend a lot of time having to fix the text case. I found the solution for my Excel documents, but I could not get it to work in Word, as you explained above, the object models were different. I will confess, I can code a little in Excel, but I have zero clue about doing it Word.

Rob Conklin
02-09-2021, 04:07 PM
That worked beautifully, Paul. Thank you VERY much! Do you happen to know where I can find other useful macros for Word (besides VBAExpress)?

macropod
02-09-2021, 04:18 PM
There are numerous such forums, including:
Word Formatting & General (excelforum.com) (https://www.excelforum.com/word-formatting-and-general/)
Results in Microsoft 365 and Office - Microsoft Community (https://answers.microsoft.com/en-us/msoffice/forum?sort=LastReplyDate&dir=Desc&tab=All&status=all&mod=&modAge=&advFil=&postedAfter=&postedBefore=&threadType=All&isFilterExpanded=false&page=1)
Microsoft Office Forums - Word, Excel, Outlook, PowerPoint, Project (msofficeforums.com) (https://www.msofficeforums.com/)
Newest 'ms-word' Questions - Stack Overflow (https://stackoverflow.com/questions/tagged/ms-word)

Rob Conklin
02-09-2021, 04:23 PM
Thank you. One more question. How do I mark the solution as completed?

macropod
02-09-2021, 04:33 PM
Via the 'Thread Tools' dropdown at the top of the page.