Consulting

Results 1 to 3 of 3

Thread: Solved: Copy results to sheet2

  1. #1
    VBAX Contributor
    Joined
    May 2007
    Posts
    128
    Location

    Solved: Copy results to sheet2

    Dear Experts

    In Listbox, I get required data against given Date with following codes.

    These codes work fine to data into Listbox.

    But now I want copy required data to Sheet2.

    Please modify codes

    PHP Code:
    Private Sub CommandButton1_Click()
    Dim c As Range
    Dim sDate 
    As Date
    Dim ws 
    As Worksheet
    Set ws 
    Worksheets("weights")
    sDate Me.TextBox1.Value
    ListBox1
    .CLEAR

    For Each c In ws.Range("E:E")
    If 
    c.Value sDate Then
    With ListBox1
    .AddItem Format(ws.Cells(c.Row"E").Value"dd-mm-yy")
    .List(.
    ListCount 11) = ws.Cells(c.Row"B").Value
    .List(.ListCount 12) = ws.Cells(c.Row"J").Value
    .List(.ListCount 13) = ws.Cells(c.Row"K").Value
    .List(.ListCount 14) = ws.Cells(c.Row"G").Value
    .List(.ListCount 15) = ws.Cells(c.Row"F").Value
    .List(.ListCount 16) = ws.Cells(c.Row"N").Value
    .List(.ListCount 17) = ws.Cells(c.Row"O").Value
    .List(.ListCount 18) = ws.Cells(c.Row"M").Value
    .List(.ListCount 19) = ws.Cells(c.Row"D").Value
    End With
    End 
    If
    Next c
    End Sub 

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub CommandButton1_Click()
    Dim c As Range
    Dim sDate As Date
    Dim ws As Worksheet
    Dim iRow As Long

    Set ws = Worksheets("weights")
    sDate = Me.TextBox1.Value

    With Worksheets("Sheet2")
    iRow = 1
    For Each c In ws.Range("E:E")
    If c.Value = sDate Then
    .Cells(iRow, "A").Value = Format(ws.Cells(c.Row, "E").Value, "dd-mm-yy")
    .Cells(iRow, "B").Value = ws.Cells(c.Row, "B").Value
    .Cells(iRow, "C").Value = ws.Cells(c.Row, "J").Value
    .Cells(iRow, "D").Value = ws.Cells(c.Row, "K").Value
    .Cells(iRow, "E").Value = ws.Cells(c.Row, "G").Value
    .Cells(iRow, "F").Value = ws.Cells(c.Row, "F").Value
    .Cells(iRow, "G").Value = ws.Cells(c.Row, "N").Value
    .Cells(iRow, "H").Value = ws.Cells(c.Row, "O").Value
    .Cells(iRow, "I").Value = ws.Cells(c.Row, "M").Value
    .Cells(iRow, "J").Value = ws.Cells(c.Row, "D").Value
    End If
    Next c
    iRow = iRow + 1
    End With
    End Sub
    [/vba]
    ____________________________________________
    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

  3. #3
    VBAX Contributor
    Joined
    May 2007
    Posts
    128
    Location
    Dear Sir,

    Your codes copy only the last row of data to first row.
    I made some modifications as follows.
    Now every thing is fine.

    I changed the location of this line
    iRow = iRow + 1

    PHP Code:
    Private Sub CommandButton1_Click()
    Dim c As Range
        Dim sDate 
    As Date
        Dim ws 
    As Worksheet
        Dim iRow 
    As Long
         
        Set ws 
    Worksheets("weights")
        
    sDate Me.TextBox1.Value
        
        Sheets
    ("sheet2").Select
        Selection
    .Clear
        
        With Worksheets
    ("Sheet2")
            
    iRow 1
            
    For Each c In ws.Range("E:E")
                If 
    c.Value sDate Then
                    
    .Cells(iRow"A").Value Format(ws.Cells(c.Row"E").Value"dd-mm-yy")
                    .
    Cells(iRow"B").Value ws.Cells(c.Row"B").Value
                    
    .Cells(iRow"C").Value ws.Cells(c.Row"J").Value
                    
    .Cells(iRow"D").Value ws.Cells(c.Row"K").Value
                    
    .Cells(iRow"E").Value ws.Cells(c.Row"G").Value
                    
    .Cells(iRow"F").Value ws.Cells(c.Row"F").Value
                    
    .Cells(iRow"G").Value ws.Cells(c.Row"N").Value
                    
    .Cells(iRow"H").Value ws.Cells(c.Row"O").Value
                    
    .Cells(iRow"I").Value ws.Cells(c.Row"M").Value
                    
    .Cells(iRow"J").Value ws.Cells(c.Row"D").Value
                End 
    If
                
    iRow iRow 1
            Next c
            
        End With
            Unload Me
            Sheets
    ("sheet2").Activate
        
    'sort the sheet
            Cells.Select
            Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
            Range("E18").Select
    End Sub 

Posting Permissions

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