I have an Excel VBA Script I’ve written with help, I am not a neophyte when it comes to coding, but to me so far Excel VBA has been an arsenic nightmare wrapped in a sugar pill sold by MS as a language easy to learn.
Ok, rant out of the way sorry, but I digress.
Here is the complete script:
Option Explicit
Sub tgr()
Dim wsB As Worksheet 'BackOrder
Dim wsJ As Worksheet 'Jobs List
Dim wsA As Worksheet 'Archive
Dim LastRow As Long
Set wsB = Sheets("BackOrder")
Set wsJ = Sheets("Jobs List")
Set wsA = Sheets("Archive")
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
With Intersect(wsJ.UsedRange, wsJ.Columns("N"))
.AutoFilter 1, "<>Same"
With Intersect(.Offset(2).EntireRow, .Parent.Range("B:L"))
.Copy wsA.Cells(Rows.Count, "B").End(xlUp).Offset(1)
.EntireRow.Delete
End With
.AutoFilter
End With
LastRow = wsB.Range("B6").End(xlDown).Row
wsB.Range("P5:Q5").Copy wsB.Range("P6:Q" & LastRow)
Calculate
wsB.UsedRange.Copy Sheets.Add.Range("A1")
With Intersect(ActiveSheet.UsedRange, ActiveSheet.Columns("Q"))
.AutoFilter 1, "<>Different"
.EntireRow.Delete
With .Parent
.AutoFilterMode = False
Intersect(.UsedRange, .Columns("G")).Cut .Range("F1")
Intersect(.UsedRange, .Columns("H")).Cut .Range("G1")
Intersect(.UsedRange, .Columns("L")).Cut .Range("H1")
Intersect(.UsedRange, .Columns("N")).Cut .Range("I1")
Intersect(.UsedRange, .Range("B:J")).Copy wsJ.Cells(Rows.Count, "B").End(xlUp).Offset(1)
.Delete
End With
End With
LastRow = wsJ.Cells(Rows.Count, "B").End(xlUp).Row
wsJ.Range("M1:T1").Copy
wsJ.Range("B3:I" & LastRow).PasteSpecial xlPasteFormats
wsJ.Range("U1:W1").Copy wsJ.Range("J3:L" & LastRow)
wsJ.Range("X1:Y1").Copy wsJ.Range("M3:N" & LastRow)
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub
As you can see this is a three page script, BackOrder, Jobs List, and Archive, what I need to do is in Archive. The cells in Columns in J & K use TODAY() as a way to tell the rest of the script when it looks in the cell in column F how many days early or late it is.
Column J shows early, Column K shows late.
Column J’s script is:
=IF(F3-TODAY()<0,"",F3-TODAY())
Column K’s script is:
=IF(TODAY()-F3<1,"",TODAY()-F3)
Column F in this case is part of the larger script, which moves data via an import and has no formatting.
Now, when I bring row into Archive, I want it to be where the cells, instead of having TODAY() in either script, I want it to show the date it was moved over there, so it will still be today, but instead of TODAY(), it will have the date in this format ##/##/##. It will “freeze” the count of J & K so it will be accurate from that day forward, so we will know if it was shipped late, early, or on time.
Is this possible, and if so how?
You are confusing worksheet functions and VBA functions. You stopped your rant too soon although I think Word VBA is the worst version.
With the user interface, I can go to cell A1 and type “4apr12”. Excel recognises this as a date, stores the value as 41003 and sets the format to “dd-mmm-yy” which is the standard format closest to what I typed.
Alternately, I could type “41003” then go to the format menu and select or enter a date format of my choice.
Yet another choice is to type “=TODAY()”. Excel stores the value as “=TODAY()” but because of the leading equals, it evaluates this as an expression when it is entered and displays the value as a standard date (format dd/mm/yy here in the UK) and re-evaluates the expression when the workbook is opened and saved and when told to do so via the Calculate options.
VBA functions are different. Even when they do the same thing, they often have different names. The nearest equivalent to TODAY() is Now(). This function returns the current date and time as a number. The integer part is the days since 1-Jan-1900; the fraction part is (Number of seconds since midnight)/(Number of seconds in a day). So at midday today, Now() would have returned “41003.5”. This is a number; I can multiple it by two, subtract 5000 or whatever.
With VBA, I could execute:
Excel recognises this as a date just as if I had typed it from the user interface.
Alternatively, I could execute:
This is just a number and will be stored and displayed as 41003.5.
If I want it to be displayed as a date, I must execute:
The value is still saved as 41003.5 but I have told Excel to display it as 4 Apr 12.
I can execute:
This is treated just as if I had typed this formula from the interface.
But suppose I execute:
Now() returns the current date and time as a number. Excel stores it as a number but displays it as a date because I have told it to. It will never re-evaluate because it is a simple numeric value not an expression involving functions.
I hope you this makes sense and helps you get your mind around the differences between Worksheet functions and VBA functions. Excel and VBA are like learning to drive a car. The first time you KNOW you will NEVER be able to do all these different things at the same time. Yet, a month or two later, you are playing with the pedals, the gear stick, wheel and indicator while checking the mirror. I do not understand why people think patting your head and rubbing your stomach is such as big deal. All I can say is that sudenly the mist will clear and Excel and VBA will make sense.