Last line of code not running

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
948
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 show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
948
Office Version
  1. 365
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,059
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
948
Office Version
  1. 365
Platform
  1. Windows
Thanks Rory, but I'm sorry, I don't understand what I need to do.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,059
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

First, is this code in the Indoor Bike sheet?
 

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
948
Office Version
  1. 365
Platform
  1. Windows
No, the code is in Training Log sheet
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,059
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

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?
 

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
948
Office Version
  1. 365
Platform
  1. Windows
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.
 

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
948
Office Version
  1. 365
Platform
  1. Windows
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
 
Learn Excel from Bill Jelen

Understanding data is crucial, and the easiest place to start is with Microsoft Excel.

Forum statistics

Threads
1,151,700
Messages
5,766,007
Members
425,322
Latest member
galaxy6623top

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
Top