Sheet Name is Last Cell Value in A Range

Trancell

New Member
Joined
Jan 29, 2018
Messages
3
I have tried looking through the forums to solve this and i know i have done this some way before. What i am trying to do is Activate a sheet to paste to but the sheet name changes. There are several sheets and i am looking to paste cell values that correspond to the sheet name which is the value of the last cell in Column F. I hope this is making sense. Essentially i want the Sheet to look up the last cell in the "Fuel Log" Column F to produce which sheet it is suppose to select. then it would go to that sheet and paste what it is has copied.

Code:
Sub Test()Sheets("Fuel Log").Range("E" & Cells.Rows.Count).End(xlUp).Copy
Sheets(Range("F" & Cells.Rows.Count).End(xlUp)).Range("A" & Cells.Rows.Count).End(xlUp).Offset(1).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False


End Sub
 

Some videos you may like

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,083
Office Version
2010
Platform
Windows
try this, I haven't tested it so could be prone to errors but hopefully it will show you a way to do it:
Code:
Sub Test()
Dim shtname As String


lastrow = Cells(Rows.Count, "E").End(xlUp).Row
'pick up data to paste in variant array
inarr = Sheets("Fuel Log").Range("E1:" & lastrow)
lastF = Cells(Rows.Count, "F").End(xlUp).Row
' pick up the sheet name in string variable
shtname = Cells("f" & lastF)
With Worksheets(shtname)
' paste the dat to the output worksheet
 Range("A1:A" & lastrow) = inarr
End With
 

Trancell

New Member
Joined
Jan 29, 2018
Messages
3
I am getting an error with this. I have not had much time to play with it though, I am getting an error on the inarr . I will try it more when i get back to the office.

try this, I haven't tested it so could be prone to errors but hopefully it will show you a way to do it:
Code:
Sub Test()
Dim shtname As String


lastrow = Cells(Rows.Count, "E").End(xlUp).Row
'pick up data to paste in variant array
inarr = Sheets("Fuel Log").Range("E1:" & lastrow)
lastF = Cells(Rows.Count, "F").End(xlUp).Row
' pick up the sheet name in string variable
shtname = Cells("f" & lastF)
With Worksheets(shtname)
' paste the dat to the output worksheet
 Range("A1:A" & lastrow) = inarr
End With
 

Trancell

New Member
Joined
Jan 29, 2018
Messages
3
My main question to this is the fact at the Range("F" & cells.Rows Count) part of the function the F is returning an error that says needs a list separator but i do not know why. This function works as a stand alone but when nested inside of the other function it doesnt work.
 

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,083
Office Version
2010
Platform
Windows
Sorry my fault i got the syntax wrong i

try this:
Code:
Sub Test()Dim shtname As String




lastrow = Cells(Rows.Count, "E").End(xlUp).Row
'pick up data to paste in variant array
inarr = Sheets("Fuel Log").Range("E" & lastrow & ":E" & lastrow)
lastF = Cells(Rows.Count, "F").End(xlUp).Row
' pick up the sheet name in string variable
shtname = Range("F" & lastF & ":F" & lastF)
With Worksheets(shtname)
' paste the dat to the output worksheet
 Range("A1:A" & lastrow) = inarr
End With
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
36,150
Office Version
365
Platform
Windows
2 things with your original code
Code:
Sub Test()
Range("E" & Cells.Rows.Count).End(xlUp).Copy
Sheets(Range("F" & Cells.Rows.Count).End(xlUp)[COLOR=#ff0000].Value[/COLOR]).Range("A" & Cells.Rows.Count).End(xlUp).Offset(1).[COLOR=#ff0000]PasteSpecial[/COLOR] _
   Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False


End Sub
It's best to include the .value, otherwise the code may be trying to use the range instead. Also that sheet is not the active sheet you cannot use .Select
 

Watch MrExcel Video

Forum statistics

Threads
1,090,484
Messages
5,414,807
Members
403,543
Latest member
Tonkytonk

This Week's Hot Topics

Top