Microsoft Office Excel 2007 running on Windows XP Pro
One of my coworkers, yesterday, was having issues with the fill-down routine in Excel. She said that prior to that day, she could make the fill method fill a series of dates, where it changed by month in decending order. So if she started on 12/5/2010, the next cell below would be 11/5/2010, then 10/5/2010 in the next one, and so on.
She said she ALWAYS did this for 12 months - one year - in the particular process. So, I built a VBA Subroutine that would take the actively selected cell's date value, and copy it down 12 rows, just the way she wanted. It works great, EXCEPT...
We tied the macro to the keyboard shortcut <Ctrl>+<Q>, for ease of access in running it. Well, if she then pressed <Ctrl>+<Q>, Excel would immediately paste some misplaced (no idea where it comes from) code after End Sub in the module, and then errors out, saying, "Only Comments can be included after the End Sub line" - paraphrased.
I haven't yet tested on my machine. She wasn't concerned about it, b/c we put a button in her Quick Access Toolbar, and it works with that. We removed the shortcut assignment, and all works fine.
I may try to test it on my machine today just to see, but it just sounds really really odd to me, and I've not the foggiest idea as to what caused it.
For reference, here's the code:
This isn't the end of the world, as it works fine w/o the keyboard shortcut.
This morning, I did a test run on my local machine. Same code, different workbook/worksheet, different computer, different shortut. I already have something assigned to Ctrl+Q, so I set it to Ctrl+M on my machine, ran a few times, not a bit of trouble. Oh, same OS, same version of office, etc.
If anybody has any clues, that'd be great. If not, it's not a big deal, but it'd be great to at least find out WHY it is happening.
One of my coworkers, yesterday, was having issues with the fill-down routine in Excel. She said that prior to that day, she could make the fill method fill a series of dates, where it changed by month in decending order. So if she started on 12/5/2010, the next cell below would be 11/5/2010, then 10/5/2010 in the next one, and so on.
She said she ALWAYS did this for 12 months - one year - in the particular process. So, I built a VBA Subroutine that would take the actively selected cell's date value, and copy it down 12 rows, just the way she wanted. It works great, EXCEPT...
We tied the macro to the keyboard shortcut <Ctrl>+<Q>, for ease of access in running it. Well, if she then pressed <Ctrl>+<Q>, Excel would immediately paste some misplaced (no idea where it comes from) code after End Sub in the module, and then errors out, saying, "Only Comments can be included after the End Sub line" - paraphrased.
I haven't yet tested on my machine. She wasn't concerned about it, b/c we put a button in her Quick Access Toolbar, and it works with that. We removed the shortcut assignment, and all works fine.
I may try to test it on my machine today just to see, but it just sounds really really odd to me, and I've not the foggiest idea as to what caused it.
For reference, here's the code:
Code:
Option Explicit
Private Sub MonthReverseYear()
On Error GoTo ErrHandle
[GREEN]'.. to copy and reverse the month from the selected date down 12 cells... for one year
'.. to give easy method vs the Autofill, since it seems to have issues.
'Create Variables[/GREEN]
Dim wb As Workbook
Dim ws As Worksheet
Dim x As Integer
Dim intRow As Integer 'Row Number
Dim intCount As Integer 'counter
IsADate:
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
x = Month(ActiveCell.Value)
[GREEN]'Check to be sure an actual date value'd cell has been selected.[/GREEN]
If IsDate(ActiveCell.Value) Then
For intCount = 1 To 12
x = Month(Trim(ActiveCell.Value))
intRow = ActiveCell.Row
If x = 1 Then
ws.Cells(intRow + 1, ActiveCell.Column).Formula = _
"12/" & _
Day(Trim(ActiveCell.Value)) & "/" & _
Year(Trim(ActiveCell.Value)) - 1
ws.Cells(intRow + 1, ActiveCell.Column).Select
Else
ws.Cells(intRow + 1, ActiveCell.Column).Formula = _
Month(Trim(ActiveCell.Value)) - 1 & "/" & _
Day(Trim(ActiveCell.Value)) & "/" & _
Year(Trim(ActiveCell.Value))
ws.Cells(intRow + 1, ActiveCell.Column).Select
End If
Next intCount
Else
If Left(ActiveCell.Formula, 1) = "'" Then
[GREEN]'Attempt some corection if possible[/GREEN]
ActiveCell.Formula = Replace(ActiveCell.Formula, "'", "")
ActiveCell.Formula = Right(ActiveCell.Formula, Len(ActiveCell.Formula) - 1)
ActiveCell.Activate
ws.Cells(ActiveCell.Row, ActiveCell.Column + 1).Activate
ws.Cells(ActiveCell.Row, ActiveCell.Column - 1).Activate
GoTo IsADate
Else
[GREEN]'If no real date value selected, give an error message, and do not proceed with the above code.
'Also give ideas as to how to fix the issue if it seems like it should have worked.[/GREEN]
MsgBox "Not a Date Value!" & Chr(10) & Chr(10) & _
"Please select a Cell that has a date value." & Chr(13) & _
"If you believe you receive this message in error, please check the cell's number format: " & _
"if the cell's number format is set to TEXT, then this code may not propertly recognize the date value. " _
, vbCritical, "Not a Date!"
End If
End If
ExitSub:
Set wb = Nothing
Set ws = Nothing
x = 0
intRow = 0
intCount = 0
Exit Sub
ErrHandle:
[GREEN]'Record what the error was for future debugging reference, if needed.[/GREEN]
Debug.Print "--------------------------------------------"
Debug.Print "------New Error------"
Debug.Print "------MonthReverseYear------"""
Debug.Print "When: " & Now()
Debug.Print "Who was Running it: " & Environ("User")
Debug.Print "Error Specifics: " & Err.Number & " " & Err.Description
Debug.Print "--------------------------------------------"
GoTo ExitSub
End Sub
This isn't the end of the world, as it works fine w/o the keyboard shortcut.
This morning, I did a test run on my local machine. Same code, different workbook/worksheet, different computer, different shortut. I already have something assigned to Ctrl+Q, so I set it to Ctrl+M on my machine, ran a few times, not a bit of trouble. Oh, same OS, same version of office, etc.
If anybody has any clues, that'd be great. If not, it's not a big deal, but it'd be great to at least find out WHY it is happening.