View Single Post
 
Old 08-20-2017, 11:36 AM
NoSparks NoSparks is offline Windows 7 64bit Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

Well... I guess that should be looking at WS1 seeing those are cells being copied to WS2.

If you remove the dots from each .Range("somecell") it will refer to whatever sheet is active at the time, but that's not ideal.
Putting WS1 in front of the dots would do except that you haven't yet set the WS1 and WS2 variables.

You're best to declare all variables at the beginning of the sub.
Set the worksheets right away then use With statements to keep things together, reduce the typing and speed things up.

I'm not sure but maybe this is what your PostToRegister sub should be like.
Code:
Sub PostToRegister()
    Dim Lrow As Long
    Dim inDate As Date, inNum As Long
    Dim exDate, exNum As Long
    Dim NextRow As Long
    Dim WS1 As Worksheet
    Dim WS2 As Worksheet

Set WS1 = Worksheets("INVOICE")
Set WS2 = Worksheets("LIST INVOICE")
   
With WS1
    If .Range("F16") = "" Or .Range("E31") = "" Or .Range("G31") = "" Or .Range("G38") = "" Then
        MsgBox "some stuff missing"
        Exit Sub
    End If
    ' populate variables
    inDate = .Cells(38, 7).Value
    inNum = .Cells(5, 9).Value
End With

With WS2
    ' populate variables
    Lrow = .Cells(Rows.Count, 1).End(xlUp).Row
    exDate = .Cells(Lrow, 1).Value
    exNum = .Cells(Lrow, 2).Value
End With

If inDate >= exDate And inNum > exNum Then
    With WS2
        'Figure out which row is the next row
        NextRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
        'Write the important values to Register
        .Cells(NextRow, 1).Resize(1, 6).Value = Array(WS1.Range("G38"), WS1.Range("I5"), _
                WS1.Range("H5"), WS1.Range("F16"), WS1.Range("G31"), WS1.Range("E31"))
    End With
Else
    MsgBox "ERROR"
End If

End Sub
I'm not able to test things completely because I can only guess at what you're working with. If you're still having issues attach a sanitized workbook.

PS: please don't quote entire post and please use code tags.
Reply With Quote