Copy contents of Col F in last row of Sheet 1 to same location Sheet 2 when text entered in Sheet 1 Col i

Ironman

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

I need the values in Col F Col G and Col H in the last filled row of sheet 'Training Log' to be copied to the first empty row in Col F Col G and Col I (NOT H) in sheet 'Indoor Bike' only if/when Col I of the last filled row of sheet 'Training Log' contains the text 'Indoor Bike Session'.

For clarity, the tables below both show the value 125 in Col F, 78% in Col G, the value J in Col H and I and the text 'Indoor Bike Session...' in Col I of Training Log.

Training Log
Wed, 29 Sep 2021OTHER12578%JIndoor bike session, 60 mins.
#NUM!

Indoor Bike
Wed, 29 Sep 20211:00:0021.713.5812578%160J

Many thanks!
 
Last edited:
without deleting formula test it, I want know which one find your correct last row
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Ok, I put the formulas in Col G back in.
 

Attachments

  • Screenshot 2021-09-30 17.15.png
    Screenshot 2021-09-30 17.15.png
    4.4 KB · Views: 7
Upvote 0
Then this should be working:
VBA Code:
Sub Test()
Dim Lr1 As Long, Lr2 As Long
Lr1 = Sheets("Training Log").Range("A" & Rows.Count).End(xlup).Row
Lr2 = Sheets("Indoor Bike").Range("A" & Rows.Count).End(xlup).Row + 1
If Trim(Left(Sheets("Training Log").Range("I" & Lr1).Value, 20)) = "Indoor Bike Session" Then
Sheets("Indoor Bike").Range("F" & Lr2 & ":G" & Lr2).Value = Sheets("Training Log").Range("F" & Lr1 & ":G" & Lr1).Value
Sheets("Indoor Bike").Range("I" & Lr2).Value = Sheets("Training Log").Range("H" & Lr1).Value
Sheets("Indoor Bike").Range("A" & Lr2).Value = Sheets("Training Log").Range("A" & Lr1).Value
End If
MsgBox Lr1 & " , " & Lr2 & Vblf & Trim(Left(Sheets("Training Log").Range("I" & Lr1).Value, 20)) & "*"
End Sub
 
Upvote 0
I ran it and got this message (correct rows but no data copied)
 

Attachments

  • Screenshot 2021-09-30 171729.png
    Screenshot 2021-09-30 171729.png
    4.4 KB · Views: 8
Upvote 0
I find the Problem Try this:
VBA Code:
        Sub Test()
Dim Lr1 As Long, Lr2 As Long
Lr1 = Sheets("Training Log").Range("A" & Rows.Count).End(xlup).Row
Lr2 = Sheets("Indoor Bike").Range("A" & Rows.Count).End(xlup).Row + 1
If Trim(Left(Sheets("Training Log").Range("I" & Lr1).Value, 19)) = "Indoor bike session" Then
Sheets("Indoor Bike").Range("F" & Lr2 & ":G" & Lr2).Value = Sheets("Training Log").Range("F" & Lr1 & ":G" & Lr1).Value
Sheets("Indoor Bike").Range("I" & Lr2).Value = Sheets("Training Log").Range("H" & Lr1).Value
Sheets("Indoor Bike").Range("A" & Lr2).Value = Sheets("Training Log").Range("A" & Lr1).Value
End If
MsgBox Lr1 & " , " & Lr2 & Vblf & Trim(Left(Sheets("Training Log").Range("I" & Lr1).Value, 20)) & "*"
End Sub
 
Upvote 0
Or Use this:
VBA Code:
Sub Test()
Dim Lr1 As Long, Lr2 As Long
Lr1 = Sheets("Training Log").Range("A" & Rows.Count).End(xlup).Row
Lr2 = Sheets("Indoor Bike").Range("A" & Rows.Count).End(xlup).Row + 1
If UCase(Trim(Left(Sheets("Training Log").Range("I" & Lr1).Value, 19))) = "INDOOR BIKE SESSION" Then
Sheets("Indoor Bike").Range("F" & Lr2 & ":G" & Lr2).Value = Sheets("Training Log").Range("F" & Lr1 & ":G" & Lr1).Value
Sheets("Indoor Bike").Range("I" & Lr2).Value = Sheets("Training Log").Range("H" & Lr1).Value
Sheets("Indoor Bike").Range("A" & Lr2).Value = Sheets("Training Log").Range("A" & Lr1).Value
End If
MsgBox Lr1 & " , " & Lr2 & Vblf & Trim(Left(Sheets("Training Log").Range("I" & Lr1).Value, 20)) & "*"
End Sub
 
Upvote 0
Solution
Another approach may be to look at the date columns for last and write rows and use InStr to check for Indoor Bike Session
VBA Code:
Sub TryThis()
    Dim LastRow As Long, WriteRow As Long
    Dim src As Worksheet, dest As Worksheet
    
Set src = Sheets("Training Log")
Set dest = Sheets("Indoor Bike")

WriteRow = dest.Range("A" & Rows.Count).End(xlUp).Row + 1

With src
    LastRow = src.Range("A" & Rows.Count).End(xlUp).Row
    If InStr("Indoor Bike Session", .Range("I" & LastRow).Value) <> 0 Then
        dest.Range("A" & WriteRow).Value = .Range("A" & LastRow).Value
        dest.Range("F" & WriteRow).Value = .Range("F" & LastRow).Value
        dest.Range("G" & WriteRow).Value = .Range("G" & LastRow).Value
        dest.Range("I" & WriteRow).Value = .Range("H" & LastRow).Value
    End If
End With

End Sub
 
Upvote 0
Yes! That works, thank you so much maabadi for persevering with me!

@NoSparks - Hi Nolan, I'm really pleased you're watching - I need to amend the code you very kindly did for me just slightly if you'd be OK to do that please?

I have added part of your code to the above and this is what I have now
VBA Code:
Dim Lr1 As Long, Lr2 As Long
Lr1 = Sheets("Training Log").Range("A" & Rows.Count).End(xlUp).Row
Lr2 = Sheets("Indoor Bike").Range("A" & Rows.Count).End(xlUp).Row + 1
If UCase(Trim(Left(Sheets("Training Log").Range("I" & Lr1).Value, 19))) = "INDOOR BIKE SESSION" Then
Sheets("Indoor Bike").Range("F" & Lr2 & ":G" & Lr2).Value = Sheets("Training Log").Range("F" & Lr1 & ":G" & Lr1).Value
Sheets("Indoor Bike").Range("I" & Lr2).Value = Sheets("Training Log").Range("H" & Lr1).Value
Sheets("Indoor Bike").Range("A" & Lr2).Value = Sheets("Training Log").Range("A" & Lr1).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 "
End If
End Sub
I just need to combine the above with the below code you did for me to run as a worksheet_change event (I have deleted the Heart Rate Data rows as it has now been copied, thanks to maabadi).
The code needs to start in Col C, not A, because A has also been copied, as above. It then needs to jump from Col C to Col H and then Col J.
VBA Code:
Dim NextRow As Long

lr = Range("A" & Rows.Count).End(xlUp).Row

If Target.Address(0, 0) = Range("A" & Rows.Count).End(xlUp).Address(0, 0) Then
Application.EnableEvents = False
    Range("C" & Target.Row).Select 'move to this cell to start inputting data

    MsgBox "Enter distance", vbInformation, "Indoor Bike Session Data"
End If

' jump from C to H on that same row
If Target.Address(0, 0) = Range("C" & lr).Address(0, 0) Then
    Range("H" & lr).Select
    MsgBox "Enter Average Watts", vbInformation, "Indoor Bike Session Data"
End If

If Target.Address(0, 0) = Range("H" & lr).Address(0, 0) Then
    Range("J" & lr).Select
Application.EnableEvents = True
End If
 
Last edited:
Upvote 0
Sorry Paul and maabadi,

when I posted no response had yet been marked as the answer and when I saw that maabadi's post was marked as the answer I tried to delete my post but it was past the 10 minute time limit.
My suggestion actually doesn't account for maybe Indoor not having a capital I, so wouldn't consistently work.

As for what you're now asking,
are you saying that the code maabadi supplied should be integrated as part of the "Indoor Bike" Worksheet_Change event ?
Maybe you should be asking as a new question.
 
Upvote 0
are you saying that the code maabadi supplied should be integrated as part of the "Indoor Bike" Worksheet_Change event ?
Yes please Nolan - sure, I'll do that now.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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