vba Replace not working

melodramatic

Board Regular
Joined
Apr 28, 2003
Messages
180
Office Version
  1. 365
Platform
  1. Windows
Yes, we're slow at work, so I'm building like crazy. Thanks to all of you who have helped me out the last few weeks with my "still learning" problems!

I've created a macro that will take standardized charge sheets for rentals, and copy them, showing the client, etc. per our engineer's data table that he creates.

Everything in the macro works fine, EXCEPT for the replacing of variable markers to be replaced by variables. When the sheets are complete, the markers are still there instead of the project data.

I know that I had a similar problem earlier this month, which was solved by making the changes in a "With Worksheets(…) layout, but I don't know how to do that when I don't know the addresses for the markers (there are 5 different rental sheets, and each one has a different layout).

So, here goes - hopefully you can tell me what I am doing wrong here...

Code:
'Replace datapoints with variables - THIS IS WHERE I'M HAVING MY ISSUES ON THE CODE BELOW
Set r = ActiveSheet.Range("A1:M10")
r.Replace "PNPN", PN
Set r = ActiveSheet.Range("A1:M10")
r.Replace "CLIENTCLIENT", Client
Set r = ActiveSheet.Range("A1:M10")
r.Replace "DATEDATE", WeekEnding

The full macro is as follows (the trouble code is about 3/4 of the way down):

VBA Code:
Private Sub CommandButton1_Click()
' This macro creates worksheets for all active projects (per the list)
' Written by Melody October May
' Created May 26, 2020
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim Pwd As String
Dim Confirmation As Integer
Dim WeekEnding As Date
Dim CountTabs As Long
Dim PNrow As Long
Dim PN As Long
Dim Phase As String
Dim Lab As String
Dim PM As String
Dim Client As String
Dim WorkTab As String
Dim AfterTab As String
Dim r As Range
Dim cellrange As Range
Dim cellrow As Range
Dim cellcol As Range
Pwd = Range("M2")
'Make sure that a date is selected, and if so, that the date is correct
If Range("C1") = "Select Week-End Date:" Then
MsgBox "You must select a week ending date for this file before continuing. See Cell C1."
Exit Sub
End If
WeekEnding = Range("C1")
CountTabs = 0
'Sort Table to PN.Phase.Lab
ActiveSheet.Unprotect Password:=Pwd
    ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Add2 Key:=Range("Table_PNs[[#All],[Lab]]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Add2 Key:=Range("Table_PNs[[#All],[Phase]]"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Add2 Key:=Range("Table_PNs[[#All],[PN]]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

ActiveSheet.Protect Password:=Pwd
For PNrow = Range("M3") + 1 To Range("M4")
    If Range("G" & PNrow) = "READY TO CREATE TAB" Then

PN = Range("A" & PNrow)
Phase = Range("B" & PNrow)
Lab = Range("C" & PNrow)
PM = Range("E" & PNrow)
Client = Range("F" & PNrow)
WorkTab = Range("J" & PNrow)
AfterTab = Range("K" & PNrow)

Sheets(Lab).Visible = True
Sheets(Lab).Select
Application.CopyObjectsWithCells = False
ActiveSheet.Copy After:=ActiveWorkbook.Sheets(AfterTab)
ActiveSheet.Name = WorkTab

'Replace datapoints with variables - THIS IS WHERE I'M HAVING MY ISSUES ON THE CODE BELOW
Set r = ActiveSheet.Range("A1:M10")
r.Replace "PNPN", PN
Set r = ActiveSheet.Range("A1:M10")
r.Replace "CLIENTCLIENT", Client
Set r = ActiveSheet.Range("A1:M10")
r.Replace "DATEDATE", WeekEnding

'Mark Tab as Created; Create Hyperlink
Sheets("Projects").Select
ActiveSheet.Unprotect Password:=Pwd
Range("H" & PNrow).Value = "P"
Range("A" & PNrow).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'" & WorkTab & "'!A1"

With Selection.Font
.Size = 12
End With
With Selection
.HorizontalAlignment = xlCenter
End With

ActiveSheet.Protect Password:=Pwd
CountTabs = CountTabs + 1
Sheets(Lab).Visible = xlVeryHidden

End If

Next PNrow
CommandButton5.Caption = "Unprotect This Worksheet"
MsgBox "Setting up Lab Rental Sheets is Complete. " & CountTabs & " lab rental sheets have been set up."

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Set r = ActiveSheet.Range("A1:M10")
r.Replace "PNPN", PN
r.Replace "CLIENTCLIENT", Client
r.Replace "DATEDATE", WeekEnding

If you debug, at this point what value does each variable have?

Try this:
VBA Code:
  Set r = ActiveSheet.Range("A1:M10")
  r.Replace "PNPN", PN, xlWhole, xlByRows, False, , False, False
  r.Replace "CLIENTCLIENT", Client, xlWhole, xlByRows, False, , False, False
  r.Replace "DATEDATE", WeekEnding, xlWhole, xlByRows, False, , False, False
 
Upvote 0
At this point, if I run the vba, it makes no changes to the cells with the searchable text - they still show PNPN, CLIENTCLIENT and DATEDATE.

I made your changes, and it made no difference on the outcome.

Dangit - I was hoping you could easily point out a typo or something I missed in adding :(
 
Upvote 0
You did not answer my question.

If you debug, at this point what value does each variable have?

PN ?
Client ?
WeekEnding ?

What data do you have within those variables?
That is, if you debug, step by step the execution of your macro and you get to this line of your macro:

r.Replace "PNPN", PN

If you mouse over the PN variable, the value of what that variable has should appear.

_____________________________________________________________________________________________________
Or run the following code and tell me what appears in the msgbox

VBA Code:
Private Sub CommandButton1_Click()
' This macro creates worksheets for all active projects (per the list)
' Written by Melody October May
' Created May 26, 2020
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim Pwd As String
Dim Confirmation As Integer
Dim WeekEnding As Date
Dim CountTabs As Long
Dim PNrow As Long
Dim PN As Long
Dim Phase As String
Dim Lab As String
Dim PM As String
Dim Client As String
Dim WorkTab As String
Dim AfterTab As String
Dim r As Range
Dim cellrange As Range
Dim cellrow As Range
Dim cellcol As Range
Pwd = Range("M2")
'Make sure that a date is selected, and if so, that the date is correct
If Range("C1") = "Select Week-End Date:" Then
MsgBox "You must select a week ending date for this file before continuing. See Cell C1."
Exit Sub
End If
WeekEnding = Range("C1")
CountTabs = 0
'Sort Table to PN.Phase.Lab
ActiveSheet.Unprotect Password:=Pwd
    ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Add2 Key:=Range("Table_PNs[[#All],[Lab]]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Add2 Key:=Range("Table_PNs[[#All],[Phase]]"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort.SortFields. _
Add2 Key:=Range("Table_PNs[[#All],[PN]]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Projects").ListObjects("Table_PNs").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

ActiveSheet.Protect Password:=Pwd
For PNrow = Range("M3") + 1 To Range("M4")
    If Range("G" & PNrow) = "READY TO CREATE TAB" Then

PN = Range("A" & PNrow)
Phase = Range("B" & PNrow)
Lab = Range("C" & PNrow)
PM = Range("E" & PNrow)
Client = Range("F" & PNrow)
WorkTab = Range("J" & PNrow)
AfterTab = Range("K" & PNrow)

Sheets(Lab).Visible = True
Sheets(Lab).Select
Application.CopyObjectsWithCells = False
ActiveSheet.Copy After:=ActiveWorkbook.Sheets(AfterTab)
ActiveSheet.Name = WorkTab

'Replace datapoints with variables - THIS IS WHERE I'M HAVING MY ISSUES ON THE CODE BELOW

Msgbox "Values : " & PN & " " & Client & " " & WeekEnding

Set r = ActiveSheet.Range("A1:M10")
r.Replace "PNPN", PN
Set r = ActiveSheet.Range("A1:M10")
r.Replace "CLIENTCLIENT", Client
Set r = ActiveSheet.Range("A1:M10")
r.Replace "DATEDATE", WeekEnding

'Mark Tab as Created; Create Hyperlink
Sheets("Projects").Select
ActiveSheet.Unprotect Password:=Pwd
Range("H" & PNrow).Value = "P"
Range("A" & PNrow).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'" & WorkTab & "'!A1"

With Selection.Font
.Size = 12
End With
With Selection
.HorizontalAlignment = xlCenter
End With

ActiveSheet.Protect Password:=Pwd
CountTabs = CountTabs + 1
Sheets(Lab).Visible = xlVeryHidden

End If

Next PNrow
CommandButton5.Caption = "Unprotect This Worksheet"
MsgBox "Setting up Lab Rental Sheets is Complete. " & CountTabs & " lab rental sheets have been set up."

End Sub
 
Upvote 0
I apologize - I misunderstood what you were asking.

Below is what I got from the msgbox you had me use for testing, so I know the data is there...

Capture.JPG


And here is what the form looks like after I run the macro...

Capture.JPG
 
Upvote 0
Does the fact that I'm on Office 365 make any diff in how the vba is working for me?
 
Upvote 0
Can you put the image before running the macro, but show excel rows and columns?
And what do you have in the cell, do you have "PNPN" or maybe you have blank spaces like "PNPN "

__________________________________________________________________________

In addition to the above, try this:

Try this line:
r.Replace "PNPN", PN, xlPart, xlByRows, False, , False, False
 
Upvote 0
OK, trying every different solution I could find on earth, I finally found one that works.

I replaced the r variable. Instead of it being
VBA Code:
Set r = ActiveSheet.Range("A1:M10")

It is now
VBA Code:
Set r = ActiveSheet.Range("A4:A5").EntireRow.Select

That's the only line I had to change. I don't know what the diff is as far as vba is concerned, but it works.

Thanks for helping me out Dante - you kept me focused on doing the testing to find out where the problem was, and kept me from going crazy. :)
 
Upvote 0
I'm glad to hear that it works for you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,760
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top