+ Reply to Thread
Results 1 to 5 of 5

Loop Copying Problems

Hybrid View

  1. #1
    Registered User
    Join Date
    03-10-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Loop Copying Problems

    Hi, i've wrote a code with loops to try and copy data between 2 times.
    here it is

    Sub Test2()
    i = 1
    j = 1
    Set vType = ActiveSheet.Cells(i, "A")
    Set vCelli = ActiveSheet.Cells(i, "B")
    Set vCellc = ActiveSheet.Cells(i, "C")
    Set vCriti = Sheet5.Cells("3", "A")
    Set vCritc = Sheet5.Cells("3", "B")
    Set vCur6 = Sheet6.Cells(j, "A")
    Set vType6 = Sheet6.Cells(j, "B")
    
    vCur6.Value = vCellc.Value
    vType6.Value = vType.Value
    Do
    If vCelli.Value >= vCriti.Value And vCellc.Value <= vCritc.Value Then
    
    Do
    If IsEmpty(vCur6) Then
    vCur6.Value = vCellc.Value
    vType6.Value = vType.Value
    Else
    j = j + 1
    Set vCur6 = Sheet6.Cells(j, "A")
    Set vType6 = Sheet6.Cells(j, "B")
    End If
    Loop Until IsEmpty(vCur6)
    i = i + 1
    Set vType = ActiveSheet.Cells(i, "A")
    Set vCelli = ActiveSheet.Cells(i, "B")
    Set vCellc = ActiveSheet.Cells(i, "C")
    Else
    i = i + 1
    Set vType = ActiveSheet.Cells(i, "A")
    Set vCelli = ActiveSheet.Cells(i, "B")
    Set vCellc = ActiveSheet.Cells(i, "C")
    End If
    Loop Until IsEmpty(vCelli)
    
    End Sub
    Now my first Problem is that for the if statment with the and i cannot get the <= and >= to work properly is i use as is written in the code at the moment then it only give me a couple of value out of 10 even though they are all between the dates, also is i change it so they both are >= then it gives me every value but the first one. Can anyone tell me where i'm going wrong.
    Thanks for all the help

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Loop Copying Problems

    Welcome to the forum, Wes.

    We're looking at code that doesn't work, and trying to imagine the workbook that goes with it. That's a tough combination.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    03-10-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop Copying Problems

    haha ok i get your point, hope thats a bit better.
    sorry
    Attached Files Attached Files

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Loop Copying Problems

    I remember this workbook.

    Format all the cells that are supposed to be dates as General. If they don't turn into numbers, they're stored as text, which means code isn't going to work. You need to fix that first.

  5. #5
    Registered User
    Join Date
    03-10-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop Copying Problems

    I've fixed the problem now, i realised that the 2 times i was using were in different formats, one did not have the seconds added to them, so after adding this it gave me the correct values. Also to solve the problem of it not including the 1st value i did all of the data, then at the end i put the title to the columns at the end.
    Sub Test2()
    Sheet6.Select
    Range("A1").EntireColumn.Select
    Selection.Delete
    Range("A1").EntireColumn.Select
    Selection.Delete
    If Sheet1.Range("A1") = "Rain" Then
    Sheet2.Select
    ElseIf Sheet1.Range("A1") = "Temp" Then
    Sheet3.Select
    ElseIf Sheet1.Range("A1") = "Part" Then
    Sheet4.Select
    Else: MsgBox "Error! Please restart the system."
    End If
    i = 1
    j = 1
    Set vType = ActiveSheet.Cells(i, "A")
    Set vCelli = ActiveSheet.Cells(i, "B")
    Set vCellc = ActiveSheet.Cells(i, "C")
    Set vCriti = Sheet5.Cells("3", "A")
    Set vCritc = Sheet5.Cells("3", "B")
    Set vCur6 = Sheet6.Cells(j, "A")
    Set vType6 = Sheet6.Cells(j, "B")
    
    Do
    If vCelli.Value >= vCriti.Value And vCellc.Value <= vCritc.Value Then
    
    Do
    If IsEmpty(vCur6) Then
    vCur6.Value = vCellc.Value
    vType6.Value = vType.Value
    Else
    j = j + 1
    Set vCur6 = Sheet6.Cells(j, "A")
    Set vType6 = Sheet6.Cells(j, "B")
    End If
    Loop Until IsEmpty(vCur6)
    i = i + 1
    Set vType = ActiveSheet.Cells(i, "A")
    Set vCelli = ActiveSheet.Cells(i, "B")
    Set vCellc = ActiveSheet.Cells(i, "C")
    Else
    i = i + 1
    Set vType = ActiveSheet.Cells(i, "A")
    Set vCelli = ActiveSheet.Cells(i, "B")
    Set vCellc = ActiveSheet.Cells(i, "C")
    End If
    Loop Until IsEmpty(vCelli)
    Sheet6.Select
    Range("A1").EntireRow.Select
    Selection.Insert Shift:=xlDown
    
    If Sheet1.Range("A1") = "Rain" Then
    Sheet2.Select
    ElseIf Sheet1.Range("A1") = "Temp" Then
    Sheet3.Select
    ElseIf Sheet1.Range("A1") = "Part" Then
    Sheet4.Select
    Else: MsgBox "Error! Please restart the system."
    End If
    
    Set vType = ActiveSheet.Cells("1", "A")
    Set vCellc = ActiveSheet.Cells("1", "C")
    Set vCur6 = Sheet6.Cells(1, "A")
    Set vType6 = Sheet6.Cells(1, "B")
    
    vCur6.Value = vCellc.Value
    vType6.Value = vType.Value
    
    
    End Sub
    And also i will include the final piece of the weather system that records data and now will show data between 2 dates on a graph, all from the forms menu.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1