shadow6810
New Member
- Joined
- Oct 1, 2021
- Messages
- 2
- Office Version
- 2016
- Platform
- Windows
Hi, I want to use the code below without having to write out the sheet name in Sheets("Sheet1").Activate in the Public Sub function, so I can just click on the sheet and run the code. I am unfamiliar with VBA language, and this should be a simple fix, but after an hour of trying things out, I can't get it working so I'm asking here now. Any help would be appreciated!
VBA Code:
Public gcolWords As New Collection
Public Sub ReplaceAllWrds()
Dim vWord, vAbv, itm
Dim i As Integer
Dim Lastrow As Integer
LoadAbbrevs
Sheets("Sheet1").Activate
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("H2:J" & Lastrow).Select
For Each itm In gcolWords
i = InStr(itm, ":")
vWord = Left(itm, i - 1)
vAbv = Mid(itm, i + 1)
Replace1Wrd vWord, vAbv
Next
Set gcolWords = Nothing
End Sub
Private Sub Replace1Wrd(ByVal pvWrd, pvAbv)
On Error Resume Next
Selection.Replace What:=pvWrd, Replacement:=pvAbv, LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Private Sub LoadAbbrevs()
Dim vWord, vAbv, vItm
Sheets("abbrevs").Activate
Range("A2").Select
While ActiveCell.Value <> ""
vWord = ActiveCell.Offset(0, 0).Value
vAbv = ActiveCell.Offset(0, 1).Value
vItm = vWord & ":" & vAbv
gcolWords.Add vItm
ActiveCell.Offset(1, 0).Select 'next row
Wend
End Sub