View Single Post
  #6  
Old 07-01-2009, 04:36 PM
broro183 broro183 is offline
Forum Guru
 
Join Date: 03 Jan 2006
Location: London :-)
MS Office Version:2003 (work) & 2007 (home)
Posts: 1,812
broro183 is very confident of their ability broro183 is very confident of their ability broro183 is very confident of their ability broro183 is very confident of their ability
Re: Changing Date Values to a working Integer

hi BDB,

Thanks for the feedback, I'm pleased we could help

In the interests of "teaching you to fish rather than just giving you a fish" I've wandered through the code in your last post & have made a few more changes which tidy it up (ignoring the comments! ) & may even make it faster. To pass on my thoughts (note I'm still learning too & am open to being challenged) & hopefully help you learn a bit more I've included heaps of comments about my suggested changes. I've also listed the code without the extra comments for ease of use.

If you are happy with this thread & the other thread that is linked in a previous post, can you please mark them as Solved?

Code:
Option Explicit

Sub Test2WithLotsOfComments()
    Dim OutIY As Worksheet
    'if using a set template file the number of header rows should not be changed within the _
     code, therefore by defining this as a Const (ie Constant) it can not be changed during the macro by mistake.
    Const FirstDataRow As Long = 10
    Dim i As Long
    Dim LastDataRow As Long

    'to speed up the macro (you could also turn off calculations & other actions/events but I don't _
     think it's needed in this situation).
    Application.ScreenUpdating = False

    Set OutIY = ThisWorkbook.Worksheets("Sheet1")
    'can remove the "WhichSht" because this is Set to be the same as the OutIY sheet therefore it is unnecessary.

    'by preceding all the "Cells(..." with a dot ie ".Cells(...", they become sub-ordinate to the _
     OutIY sheet Object in the below With Statement.
    With OutIY

        'remove this from the loop so that it is only calculated once (& there is no chance of it being changed within in the _
         loop, eg if a row was deleted).
        LastDataRow = .Cells(.Rows.Count, 1).End(xlUp).Row

        For i = FirstDataRow To LastDataRow

            'I think the below findit variable can be removed & replaced by the simpler use of the looping "i" variable (with _
             this belief, I've changed the remaining code to replace "findit" with "i").
            '### Using "i" assumes that a type of inventory will only appear once in the list, if this assumption is _
             inaccurate then you will need to use "findit", but NOTE, you may need to incorporate a loop to ensure that _
             all rows for the specific inventory type are checked & correctly adjusted.
            'delete this line...        Set findit = .Range("A:A").Find(what:=.Cells(i, 1).Value)

            'the code previously posted relied on the default property of the Cells property being Value, but _
             potentially this could change in a future version of Excel (unlikely but possible) therefore I _
             think it's better to always explicitly state the desired property as ".value" (see _
             brief comments @ http://www.dailydoseofexcel.com/archives/2004/07/07/the-strange-object/).
            'Also, I've used ".value2" for the date column due to potential Excel-VBA translation _
             problems demonstrated in http://support.microsoft.com/kb/182812
            If ((.Cells(i, "D").Value <> .Cells(i, "F").Value) And (.Cells(i, "G").Value2 < CLng(Date))) Then

                'move any code that doesn't need to be in the With statement outside it.
                .Cells(i, "F").Value = Cells(i, "D").Value
                'I've changed the following for speed ("in theory" but probably not actually noticeable) by moving the _
                 formatting outside the loop. therefore the below commented lines can be replaced by the _
                 subsequent uncommented line & the later formatting line identified with "'***"
                'With .Cells(i, "G")
                '    .Value2 = CLng(Date)
                '    .NumberFormat = "mm/dd/yyyy"
                'End With
            Else
                'do nothing
            End If
        Next i
        '*** added to format all dates in one go (after the loop has finished).
        .Range(.Cells(FirstDataRow, "G"), .Cells(LastDataRow, "G")).NumberFormat = "mm/dd/yyyy"
    End With

    'release variables.
    Set OutIY = Nothing
    'reset Excel settings
    Application.ScreenUpdating = True
    MsgBox "done"
End Sub
More succinctly as...
Code:
Option Explicit
Sub Test2WithFewerComments()
    Dim OutIY As Worksheet
    Const FirstDataRow As Long = 10
    Dim i As Long
    Dim LastDataRow As Long
    'to speed up the macro
    Application.ScreenUpdating = False
    Set OutIY = ThisWorkbook.Worksheets("Sheet1")
    With OutIY
        LastDataRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        For i = FirstDataRow To LastDataRow
            If ((.Cells(i, "D").Value <> .Cells(i, "F").Value) And (.Cells(i, "G").Value2 < CLng(Date))) Then
                .Cells(i, "F").Value = Cells(i, "D").Value
            Else
                'do nothing
            End If
        Next i
        .Range(.Cells(FirstDataRow, "G"), .Cells(LastDataRow, "G")).NumberFormat = "mm/dd/yyyy"
    End With
    'release variables.
    Set OutIY = Nothing
    'reset Excel settings
    Application.ScreenUpdating = True
    MsgBox "done"
End Sub
hth
Rob
__________________
Rob Brockett
Kiwi in the UK
Always learning & the best way to learn is to experience...

Last edited by broro183; 07-01-2009 at 04:40 PM.
Reply With Quote