Last line of code not running

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

The below code runs almost perfectly
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim NextRow As Long

lr = Range("A" & Rows.Count).End(xlUp).Row
Lr2 = Sheets("Indoor Bike").Range("A" & Rows.Count).End(xlUp).Row + 1
Lr3 = Sheets("Indoor Bike").Range("A" & Rows.Count).End(xlUp).Row

If Target.Column = 2 And Target.Value = "OTHER" Then
    Application.EnableEvents = False
Range("A" & Target.Row).Resize(, 6).Interior.Color = RGB(197, 217, 241)
Range("I" & Target.Row).Resize(, 2).Interior.Color = RGB(197, 217, 241)
Range("I" & Target.Row).Value = "Indoor bike session, 60 mins."
   
Sheets("Indoor Bike").Range("A" & Lr2).Value = Sheets("Training Log").Range("A" & lr).Value
Sheets("Indoor Bike").Range("B" & Lr2).Value = "1:00:00"
Sheets("Indoor Bike").Range("E" & Lr2).Value = "8"
Sheets("Indoor Bike").Range("J" & Lr2).Value = "Session "
   
Range("F" & Target.Row).Select 'move to this cell to input heart rate
MsgBox "Enter Average Heart Rate", vbInformation, "Indoor Bike Session Data"
End If

Sheets("Indoor Bike").Range("F" & Lr3).Value = Sheets("Training Log").Range("F" & lr).Value  'F = Heart Rate
' jump from F to H on the same row
If Target.Address(0, 0) = Range("F" & lr).Address(0, 0) Then   'F = Heart Rate
Range("H" & lr).Select  'H = Session rating

Sheets("Indoor Bike").Range("I" & Lr3).Value = Sheets("Training Log").Range("H" & lr).Value
   Application.EnableEvents = True
End If
The only part that isn't running i.e. it is not copying to Sheet 'Indoor Bike' is this line and I can't work out why
VBA Code:
Sheets("Indoor Bike").Range("I" & Lr3).Value = Sheets("Training Log").Range("H" & lr).Value

Help appreciated!

Thank you.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
P.S. Range("H" & lr).Select is for me to input data. After I have entered this, I expect the value to be copied to sheet 'Indoor Bike' but it isn't.
 
Upvote 0
It will only run that code if you change the value in column F for the last used row in column A. Also, your code disables events if you enter OTHER in column B and doesn't re-enable them.
 
Upvote 0
Thanks Rory, but I'm sorry, I don't understand what I need to do.
 
Upvote 0
First, is this code in the Indoor Bike sheet?
 
Upvote 0
No, the code is in Training Log sheet
 
Upvote 0
OK. Currently your code will run this line:

Code:
BikeSheet.Range("F" & Lr3).Value = Range("F" & lr).Value  'F = Heart Rate

if you change any cell on the Training Log sheet - is that really what you want?
 
Upvote 0
No it isn't. I only want the above code to run when the value in Col B of Training Log sheet = "OTHER", which it currently does OK. I just can't get the value (it's just a letter) in Col H of that sheet to copy to Col I of Indoor Bike sheet after I have input the letter.
 
Upvote 0
I've got it to work
VBA Code:
Sheets("Indoor Bike").Range("F" & Lr3).Value = Sheets("Training Log").Range("F" & lr).Value  'F = Heart Rate
' jump from F to H on the same row
If Target.Address(0, 0) = Range("F" & lr).Address(0, 0) Then   'F = Heart Rate
Range("H" & lr).Select  'H = Session rating
End If

If Target.Address(0, 0) = Range("H" & lr).Address(0, 0) Then
Range("I" & lr).Select  'H = Session rating
Sheets("Indoor Bike").Range("I" & Lr3).Value = Sheets("Training Log").Range("H" & lr).Value
   Application.EnableEvents = True
End If
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

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