Thread: Excel crash
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Excel crash

revision2...

Public Sub GenericoLast_R1()
'Dim App As New Excel.Application,
On Error GoTo RigaErrore
Dim wkbSource As Workbook, wksSource As Worksheet
Dim rngTarget1 As Range, rngTarget2 As Range, rngTarget3 As Range,
rng As Range
Dim sSourceFile As String
sSourceFile = "C:\Users\PC\Documenti\generico.xls"

'check if file exist
'If Not FileExists(sSourceFile) Then
' MsgBox "File not found", vbExclamation, "Attention..."
' GoTo RigaErrore
'Else

' Definitions
' -----------------------------------------
Set rngTarget1 = [Daily!A7:E7] ' Penultimate values
Set rngTarget2 = [Daily!A8:E8] ' last value
Set rngTarget3 = [Daily!B20:H33] ' TC2
' -----------------------------------------
Set wkbSource = Workbooks.Open(sSourceFile)
Set wksSource = wkbSource.Sheets("generico")

Set rng = wksSource.Range("A1")
If Not IsEmpty(rng(2, 1)) Then
Set rng = rng.Resize(rng.End(xlDown).Row - rng.Row + 1, 1)
' End If
'Import penultimate value
rngTarget1 = _
rng(rng.Rows.Count - 1).Resize(rngTarget1.Rows.Count, _
rngTarget1.Columns.Count).Value
'Import last value
rngTarget2 = _
rng(rng.Rows.Count - 0).Resize(rngTarget2.Rows.Count, _
rngTarget2.Columns.Count).Value
'Import TC2 values
rngTarget3 = _
rng(rng.Rows.Count - 13).Resize(rngTarget3.Rows.Count, _
rngTarget3.Columns.Count).Value
End If
wkbSource.Close
' App.Quit

'End If
' Kill sSourceFile
Exit Sub

RigaErro
MsgBox Err.Number & vbCr & Err.Description
End Sub

Note that this revision eliminates using the hard-coded values for
source range resizing. If the source/target ranges change change size
for any reason you won't have to update the code beyond revising the
range addresses to suit.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc