Consulting

Results 1 to 6 of 6

Thread: Extracting values from sheet 1 to sheet 2

  1. #1

    Extracting values from sheet 1 to sheet 2

    Hi,

    I found the code below from this forum. The thread was about extracting data from one sheet to another and the code should work so that when you click the commandbutton, the values from row 1 in sheet1 should be copied to the sheet2. My button is however copying only values of "0". So if I have in my sheet1 in cell A1 =H10 and in H10 is value 500, the code copies "0" instead of "500". On the previous thread there were no comments about any problems, so the code should work fine but it just isn't?

    I also have another question - could this be modified so that the values could be located in row 10 for instance, instead of row1?


    Private
    Sub  CommandButton1_Click() 
         Dim Lastrow As Long   
         With Worksheets("Sheet2")      
             If .Range("A1").Value = "" Then    
                Lastrow = 1 
             Else   
                Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 
             End If
            Rows(1).Copy .Cells(Lastrow, "A") 
            .Rows(Lastrow).Value = .Rows(Lastrow).Value 
         End With 
    End Sub
    Last edited by Aussiebear; 08-04-2014 at 12:18 AM. Reason: Added code tags

  2. #2
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    210
    Location
    You have no reference to SHEET1.

    You may want it: sheets("sheet1").Rows(1).Copy .Cells(Lastrow, "A")

  3. #3
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location
    Try This, alter you ranges to suit application
    Private Sub CommandButton1_Click()
    Dim LastRow As Long
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    Application.ScreenUpdating = False
        
    Set sh1 = Worksheets("Sheet1")
    Set sh2 = Worksheets("Sheet2")
    LastRow = sh1.Cells(Rows.Count, 1).End(xlUp).Row
    With sh1
        .Range("A1:M1" & LastRow).Copy Destination:=sh2.Range("A1")
    End With
    Application.ScreenUpdating =True    
    End Sub

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It's copying the formula, which will get adjusted, so if you want values, try

    Sub  CommandButton1_Click() 
        Dim Lastrow As Long 
    
        With Worksheets("Sheet2") 
    
            If .Range("A1").Value = "" Then 
                Lastrow = 10 
            Else 
                Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 
            End If 
            Rows(1).Copy 
            .Cells(Lastrow, "A").PasteSpecial Paste:=xlPasteValues
        End With 
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thank you all for your advice,

    at least with the last code it's working fine. Somehow this didn't need any reference to sheet1, that is something I also wondered myself.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because VBA assumes the activesheet.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •