Worksheet_Change Private Sub only affecting part of the worksheet, and not the whole range specified in my code.

LauraJane

New Member
Joined
Feb 9, 2015
Messages
7
I have some code that isn’t working right. On the Student report page, I’ve written a sub that should change all cells’ formulas to the row number I specified. It successfully changes the formulas in cells A3:A6, but doesn't for any other cells. It’s not throwing any errors, the error handler doesn't trap anything, and it's compiling without issue, so I think it’s an issue with the logic. The code is in a private sub for the worksheet that it applies to: 'Student Report'.

Private Sub Worksheet_Change(ByVal Target As Range)
'Changes the row value in the worksheets' formulas to match the row value shown in B2 of the record the listbox(A2)


On Error GoTo ErrHandler
Dim lRow As String

If Target.Address = Range("A2").Address Then 'If the value of A2 changed, then
lRow = Range("B2").Value 'store the value of lRow - the line row value of the name that was selected in A2
Worksheets("Student Report").Range("A3:Q104").Replace What:="$*", Replacement:=lRow, LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False 'Replace any part of a cell that contains a $ and any number with the value in lRow
End If
Exit Sub


ErrHandler:
MsgBox Err.Number & "; " & Err.Description
Err.Clear

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Welcome to MrExcel!

If your code is running, and successfully changing A3:A6, then there is no reason it shouldn't be changing A3:Q104.

Can you give an example of a cell in this range that you think should change, but isn't?

Replacing using "$*" sounds dangerous. It implies that you're sure the only "$" in the sheet are in simple references like:

='Sheet Name'!A$10

Instead of using VBA to hard-code all row references every time the row number changes, a more conventional approach would be to use the row number in an Excel formula, e.g. INDEX() so that your formulae always point dynamically to the right row.
 
Upvote 0

Forum statistics

Threads
1,215,857
Messages
6,127,372
Members
449,381
Latest member
Aircuart

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