How can I run VBA Worksheet_BeforeDoubleClick on a (new) daily extract.csv
Hi,
My situation is that I’ve trying for days now to find VBA code that will insert time stamp into a specified range of cells and if the user Double Clicks the cell again it will remove the time stamp. Each time a new daily exported.csv opens
The reason is that the users receive daily a newly created exported.csv file, so I need the VBA to be called/running from the users Personal.xlsb.
Now, I know that I could use the keyboard shorts cut, but believe it or not some of my users will never remember this method. So I need a method to do this using VBA, (the vba needs to be able to run from …\XLSTART\ personal.xlsb only if the new export.csv s worksheet name is named “ Whatever-Exports” .
After trawling the big “www”, I have found heaps of examples on how to insert the time stamp into a range of cells but they all use the Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) event .
Which will never be normally available to the new daily exported.csv.
So this is where I am having the difficulty as I don’t know how to call the Worksheet_BeforeDoubleClick when a new workbook is opened as it seems, even when the VBA is residing within the users “ personal.xlsb”
I finally found this sample:
http://stackoverflow.com/questions/1...ate-workbook-t
I am sure this'll work, but after reading a few times, and a few attempts, I just can’t get my head around the instructions to suit it my needs.
So my Question is, is this achievable? and is there a more simple way and if so how?
Any help in simple (laymans terms) would be great , thanks in advance
VBA Code below is what I am attempting to get to run from any new workbook/sheet with a specific name
Code:
Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim MyRange As Range
Dim IntersectRange As Range
Dim EndRow As Long
Dim TheWorksheetname As String
TheWorksheetname = ActiveSheet.Name
‘Application.ScreenUpdating = False
If TheWorksheetname = "ExportedWork_Sheet1" Then
EndRow = Range("D" & Rows.Count).End(xlUp).Row
Set MyRange = Range("G2:J" & EndRow) 'last row
Set IntersectRange = Intersect(Target, MyRange)
On Error GoTo SkipIt
If IntersectRange Is Nothing Then
Exit Sub
Else
Target = Format(Now, "ttttt") ’still to find a way to format to lose the secs or …=(left(A1,5)
'Application.ScreenUpdating = True
End If
ActiveCell.Offset(, 1).Select ‘ move curser away from cell
SkipIt:
Exit Sub
Else
'MsgBox "File does not exist"
exit sub
End If
End Sub
Attempted your suggestion but to no avail as yet
SamT many thanks for responding.
Ok, I have added the following VBA to the users Personal.xlsb under : - VBAPRoject(PERSONAL.XLSB) > ThisWorkbook
Then saved and debug > compile , which all went fine.
Then I closed down all the excel applications > then re-opened excel with a new blank worksheet
and attempted to double clicked within the range G2:G1000 and nothing happens
Code:
Sub ThisApp_SheetBeforeDoubleClick(ByVal Sht As Object, ByVal Target As Range, Cancel As Boolean)
If Sht.Parent Is ThisWorkbook Then Exit Sub
'If all Workbook will have the same Root
'If not Sht.Parent.Path = ??? Then exit Sub
'If all have a similar name part
'If not InStr(Sht.Parent.Name, NamePart), then Exit Sub
'Do Stuff
Dim MyRange As Range
Dim IntersectRange As Range
''''Dim EndRow As Long
'
'
'''''Application.ScreenUpdating = False
'
'''''EndRow = Range("D" & Rows.Count).End(xlUp).Row
'''''Set MyRange = Range("G2:J" & EndRow) 'last row
Set MyRange = Range("G2:G1000") 'last row
Set IntersectRange = Intersect(Target, MyRange)
On Error GoTo SkipIt
If IntersectRange Is Nothing Then
Exit Sub
Else
'
'
'
Target = Format(Now, "ttttt")
MsgBox " still need to format cells with ...custom macro hh:mm"
'
'''''Application.ScreenUpdating = True
End If
ActiveCell.Offset(, 1).Select
'
'
SkipIt:
Exit Sub
'
End Sub
runs on all workbook.sheet not good
Well it sure works, not that I totally understand why, only snag is I and users double click on any workbook sheet and it put the time in, which is not so good, I know the name of the workbook.sheet.csv that I need this to work on and would really like it to only work for that workbook_name. sheet1Name, as there heaps of other macros in their personal.xlsb, so obviously this user can't use those other ones ,like all the other users at the moment (with 1X keyboard shortcut for all of the different exported.csvs presently all users just need to only remember the keyboard short cut key to auto run the appropriate macro for each *.csv based on the name of the exported file they have open. such as :
Code:
Sub DiscoverWhatRptRan()
'
' Keyboard Shortcut: Ctrl+g
'
'To Automatically choose the correct
'VBA based on sheet name
'using option/Case Statement
Dim TheWorksheetname As String
TheWorksheetname = ActiveSheet.Name
If TheWorksheetname = "Exported Account1234 - " Then
'do something
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Call macro1
Else
MsgBox "File does not exist"
End If
If TheWorksheetname = "Exported Account4321 - " Then
Call macro2
Else etc etc!
can this code you gave be amended to only work on a named worksheet like the other macros
SamT lose the time stamp secs
Quote:
Originally Posted by
SamT
To lose the seconds in the TimeStamp, the Format string is "mm, dd, yyyy hh:mm"
I'm assuming you know how to edit that code for use your range for the TimeStamp. And please, in the future, when someone asks fore more information (that you already provided,) don't say, "I already posted that," or something to that affect. Just tell us again, so we don't have to carefully read every post you made. :banghead:
Apologies, once again, I was only referring to your coment "Since you won't tell us anything about the Range Specified", it wasn't meant to be condescending in anyway or form.
I had to amend your posted code (ever so slightly) by removing the If's from the or's and adding a missing ")" , but it finally does what I was originally asking for apart from one thing which is losing the secs in the time stamp (not date stamp), but that's not the worst thing in the world to deal with.
Thanks to god and this forum(from everyone who supplied some input), I now super happy and more importantly have a complete understand how your code works.
...
Code:
Sub ThisApp_SheetBeforeDoubleClick(ByVal Sht As Object, ByVal Target As Range, Cancel As Boolean)
'stop
Const WkBkNamePart As String = "Daily Fin_Exports.xlsx" '??? Edited to suit
Const ShtNamePart As String = "XYZ" '??? Edited to suit
'Decision Tree
If InStr(Sht.Parent.Name, WkBkNamePart) = 0 Or _
InStr(Sht.Name, ShtNamePart) = 0 Or _
Intersect(Target, Range(Range("G2"), Cells(Rows.Count, "G").End(xlUp))) Is Nothing Then
Exit Sub
End If
Cancel = True
If Target = "" Then
Target = Format(Time, "hh:mm")
'
'Target = Format(Now, "ttttt")
Else
Target = ""
End If
' Target = Format(Now, "mm, dd, yyyy")
End Sub