get value the lastrow from sheet to another

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
389
Office Version
  1. 2016
Platform
  1. Windows
hi
i have data in two columns a,b the col a is date and b the values I would bring the value for last row based on date (today) and copy to cells f5 in sheet2
this is what I got so far
VBA Code:
Sub vv()
Dim rows As String
Dim value As String
Dim concat As String

rows = Range("b1048576").End(xlUp).Row

Dim a As String
'Column
b = "b"
concat = a & rows
'Value in last low is below
value = Range(concat).value

MsgBox (value)
End Sub
 
OK, I still need the answer to this question I asked up in post 7:
2. Where exactly on Sheet2 do you want these values copied over to?
I need to know the range address where you want this to show up!
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this:
VBA Code:
Sub MyCopy()

    Dim lastRow As Long
    
    Sheets("Sheet1").Activate
    
'   Find last row with data in column A on Sheet1
    lastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    
'   Copy values from columns B and C in last row to E5 and F5 on Sheet2
    Sheets("Sheet1").Range(Cells(lastRow, "B"), Cells(lastRow, "C")).Copy Sheets("Sheet2").Range("E5")
    
End Sub
 
Upvote 0
it's ver impressive ! well done ! but I would add condition it based only date (today) as in my pc is it possible?
 
Upvote 0
but I would add condition it based only date (today) as in my pc is it possible?
I am not sure I understand.
Through our conversations back and forth, I thought we determined that today's date will always be in the list (that is why I asked all those questions), and it will always be on the last row of Sheet1.
Is that not the case?

If not, can you post an example of another scenario, and explain what should happen in that case?
 
Upvote 0
This version of the code is longer, but will only copy over the last row in column A with today's date in it.
VBA Code:
Sub MyCopy()

    Dim lastRow As Long
    Dim r As Long
    Dim cr As Long
    
    Sheets("Sheet1").Activate
    
'   Find last row with data in column A on Sheet1
    lastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows in Sheet1, starting from the bottom, until you find today's date
    For r = lastRow To 2 Step -1
        If Cells(r, "A") = Date Then
            cr = r
        End If
    Next r
    
'   See if it found today's date
    If cr > 0 Then
'       Copy values from columns B and C in last row to E4 and F4 on Sheet2
        Sheets("Sheet1").Range(Cells(cr, "B"), Cells(cr, "C")).Copy Sheets("Sheet2").Range("E4")
    Else
        MsgBox "Today's date not found in column A on Sheet1.", vbOKOnly, "ERROR!"
    End If
    
End Sub
 
Upvote 0
as far as I know when write this date (today) should mean current date and the current date is 02/10/2021 but if change the last row for more than current date or less than the code also bring them
your code design's for the value in last row with ignore the currently date
as in my picture in last row 02/19/2021 the the cells e5,f5 should are empty
I thought we determined that today's date will always be in the list
about this is my mistake should say not not the date today always is existed in lastrow and i'm sorry
datebrandquantity
02/01/2021as12
02/02/2021asd12
02/03/2021asd123
02/04/2021zx1
02/05/2021zz12
02/06/2021fg34
02/07/2021rt23
02/08/2021hh12
02/09/2021cf12
02/10/2021zx15
02/11/2021zx16
02/12/2021zx17
02/13/2021zx18
02/14/2021zx19
02/15/2021zx20
02/16/2021zx21
02/17/2021zx22
02/18/2021zx23
02/19/2021zx24
 
Upvote 0
I tested your code there is still problem because in the lastrow is not date(today) then should e5,f5 are empty
 
Upvote 0
I tested your code there is still problem because in the lastrow is not date(today) then should e5,f5 are empty
OK, I was assuming that you wanted to look up the values in the table and find today's date.
(You are beginning to see now why complete, clear explanations are so important! While your problem is familiar to you, all that we have to go on is the little bit of information you provide to us here).

Here is code that I think will do what you want:
VBA Code:
Sub MyCopy()

    Dim lastRow As Long
    
    Sheets("Sheet1").Activate
    
'   Find last row with data in column A on Sheet1
    lastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    
'   Check to see if last row is today's date
    If Cells(lastRow, "A") = Date Then
'       Copy values from columns B and C in last row to E5 and F5 on Sheet2 if last row on Sheet1 is today
        Sheets("Sheet1").Range(Cells(lastRow, "B"), Cells(lastRow, "C")).Copy Sheets("Sheet2").Range("E5")
    Else
'       Blank out values on Sheet2 if last row on Sheet1 is not today
        Sheets("Sheet2").Range("E5:F5").ClearContents
    End If
    
End Sub
 
Upvote 0
Solution
astonishing ! !! I'm really sorry about my explanation is not clear but honestly I do my best to explain what I want , may be my language is not good to express by English I hope accept my apology if I waste your time
many thanks for your assistance :)
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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