Maybe I didn't explain everything fully. I have data comeing into row
automatically throught an opc server. I have vba written so tha
everytime new data comes in on row three the old data keeps droppin
down through the rows. Well as everyone knows excell starts to ge
sloggish with more and more data comeing in. What I would like to do i
save the entire workbook, under todays date, at a certain time of th
day. Then on the origional workbook clear all the data from rows 4 an
down. Then on the next day at the preset time save the workbook agai
with the date. Can someone add to my code to have this possible? I'v
enclosed the code below..
Thanks,
Mike
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A3").Value <> 1 Then
Exit Sub
End If
If Not Intersect(Range(Target.Address), Me.Range("A3")) _
Is Nothing Then
Me.Cells(Me.Range("A:A").Rows.Count, Target.Column).Clear
Dim rgOldValues As Range
Dim iLastRow As Long
iLastRow = Me.Cells(Columns(Target.Column).Rows.Count, Target.Column
_
.End(xlUp).row
Application.EnableEvents = False
Select Case iLastRow
Case 1
Case 2
Case 3
Range("A4:H4").Value = Range("A3:H3").Value
Range("C4").Value = Now
Cells(4, Target.Column).Value = Cells(3, Target.Column).Value
Case Else
vaOldValues = Me.Range("A4:H" & _
IIf(iLastRow = 4, 5, iLastRow))
Range("A5:H5").Resize(UBound(vaOldValues, 1), 6).Value = _
vaOldValues
Range("A4:H4").Value = Range("A3:H3").Value
Range("C4").Value = Now
Set rgOldValues = Me.Range(Cells(Target.row + 2, Target.Column), _
Cells(iLastRow, Target.Column))
Cells(4, Target.Column).Value = Cells(3, Target.Column).Value
End Select
Application.EnableEvents = True
End If
Exit Sub
End Su
JLatham - 22 Aug 2006 14:01 GMT
Thanks for the clarification. That makes things much easier to deal with in
coming up with a solution for you. I'll be looking at this later, have to
rush off right now, and quite possibly someone else will add the few lines to
your existing code needed to accomplish this before I even get back. It's
actually probably only going to be about half-a-dozen lines of code or so.
> Maybe I didn't explain everything fully. I have data comeing into row 3
> automatically throught an opc server. I have vba written so that
[quoted text clipped - 44 lines]
> Exit Sub
> End Sub
JLatham - 22 Aug 2006 22:05 GMT
> Maybe I didn't explain everything fully. I have data comeing into row 3
> automatically throught an opc server. I have vba written so that
[quoted text clipped - 44 lines]
> Exit Sub
> End Sub
JLatham - 22 Aug 2006 22:13 GMT
Trying again - sorry if ends up semi-duplicate posting.
Try putting this code inside of your current _Change() event handler. Down
near the bottom, probably just ahead of the Exit Sub statement would be a
good place (after that statement wouldn't do much good <g>).
Change the line defining the time of day for the save to be whatever time
you want it to be.
'variables for the workbook save operations
Dim originalFullName As String
Dim newFullName As String
Dim TimeToSave As Date ' date/time
TimeToSave = "10:30:00 AM" ' change as you want
'put this somewhere down inside of your current _Change code
If Format(Now(), "hh:mm") > TimeToSave Then
originalFullName = ThisWorkbook.FullName
newFullName = Left(originalFullName, Len(originalFullName) -
Len(ThisWorkbook.Name))
newFullName = newFullName & Left(ThisWorkbook.Name,
InStr(ThisWorkbook.Name, ".xls") - 1)
newFullName = newFullName & "_" & Month(Now()) & "_" & Day(Now()) &
"_" & Right(Year(Now()), 2) & ".xls"
If Dir(newFullName) = "" Then
' haven't done this yet, so do it now
Application.DisplayAlerts = False
'save with the new name
ActiveWorkbook.SaveAs Filename:=newFullName
'clear out the existing data
If Selection.SpecialCells(xlCellTypeLastCell).Row > 3 Then
Rows("4:" &
Selection.SpecialCells(xlCellTypeLastCell).Row).Select
Selection.Delete shift:=xlUp
Range("A3").Select
End If
'revert to the old name
ActiveWorkbook.SaveAs Filename:=originalFullName
Application.DisplayAlerts = True
End If
End If
What it does: if the current time is later than the time you've coded into
it, then it creates a new filename (for use in same folder) and tests to see
if that file already exists, if it does not exist, saves current workbook
with the new name, clears out any information in rows 4:n, where n is last
row used, and then gives the workbook its original name back.
If the modified file already exists in the folder, then it none of this
happens - waits until the next day to write another copy.
I split the build up of the new file name over 3 instructions, just to keep
line breaks here in the forum to a minimum, obviously those could be written
as a single instruction.
> Maybe I didn't explain everything fully. I have data comeing into row 3
> automatically throught an opc server. I have vba written so that
[quoted text clipped - 44 lines]
> Exit Sub
> End Sub