A
Austin
Hi, I am running the code below and two questions coming up:
a) There is still an instance of Excel running in my processes once it is
complete
b) Every other time I run it, I get run-time 462 'The remote server machine
does not exist or is unavailable'
If you can help me out I would really appreciate it, thanks a lot.
Option Explicit
Private Sub Command1_Click()
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlWorkbook As Excel.Workbook
Dim strDate As Date
Dim xlThreeMo As Excel.Range
Dim xlOneYr As Excel.Range
Dim xlThreeYr As Excel.Range
Dim xlFiveYr As Excel.Range
Dim xlTenYr As Excel.Range
Dim xlrng2 As Excel.Range
Dim xlDate As Excel.Range
Dim objRST As Recordset
Dim intnumber As Integer
Dim strQuery As String
Dim strSheetName As String
'Queries the data
strQuery = "TRANSFORM Sum([Month Value.return]+1) As Return " & _
"SELECT Fund.Fund " & _
"From Dates, Fund INNER JOIN [Month Value] ON Fund.[Fund ID] =
[Month Value].[Fund ID] " & _
"WHERE (((IIF([Month Value.End Date]>[Dates.Ten Year] AND " & _
"[Month Value.End Date]<=[Dates.This Month],1,0))=1)) " & _
"GROUP BY Fund.Fund " & _
"PIVOT [Month Value].[End Date];"
strSheetName = Left(strQuery, 10)
strSheetName = Trim(strSheetName)
'Creates the worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlWorkbook = xlApp.Workbooks.Add
Set objRST = Application.CurrentDb.OpenRecordset(strQuery)
strDate = Me.Text2.Value
With objRST
If Not .EOF Then .MoveLast
intnumber = objRST.RecordCount
.MoveFirst
End With
Set xlSheet = xlWorkbook.Sheets(1)
With xlSheet
.Range("A2").CopyFromRecordset objRST
.Name = strSheetName
End With
With xlSheet
Set xlThreeMo = xlSheet.Range(.Cells(2, 123), .Cells(intnumber + 1, 123))
Set xlOneYr = xlSheet.Range(.Cells(2, 124), .Cells(intnumber + 1, 124))
Set xlThreeYr = xlSheet.Range(.Cells(2, 125), .Cells(intnumber + 1, 125))
Set xlFiveYr = xlSheet.Range(.Cells(2, 126), .Cells(intnumber + 1, 126))
Set xlTenYr = xlSheet.Range(.Cells(2, 127), .Cells(intnumber + 1, 127))
End With
xlThreeMo.FormulaR1C1 =
"=IF(Countblank((RC[-4]:RC[-2]))>0,-500,Product(RC[-4]:RC[-2])-1)"
xlOneYr.FormulaR1C1 =
"=IF(Countblank((RC[-14]:RC[-3]))>0,-500,Product(RC[-14]:RC[-3])-1)"
xlThreeYr.FormulaR1C1 =
"=IF(Countblank((RC[-39]:RC[-4]))>0,-500,Product(RC[-39]:RC[-4])^(1/3)-1)"
xlFiveYr.FormulaR1C1 =
"=IF(Countblank((RC[-64]:RC[-5]))>0,-500,Product(RC[-64]:RC[-5])^(1/5)-1)"
xlTenYr.FormulaR1C1 =
"=IF(countblank((RC[-125]:RC[-6]))>0,-500,Product(RC[-125]:RC[-6])^(1/10)-1)"
With xlSheet
.Range("A1:ZZ5000").Select
.Range("A1:ZZ5000").Copy
.Range("A1:ZZ5000").Activate
.Range("A1:ZZ5000").PasteSpecial (xlPasteValues)
End With
With xlSheet
.Columns("BQ").Select
.Columns("BQ").Delete Shift:=xlToLeft
End With
With xlSheet
Set xlDate = xlSheet.Range(.Cells(2, 2), .Cells(intnumber + 1, 2))
End With
xlDate.Value = strDate
With xlSheet
Range("A1").Select
ActiveCell.FormulaR1C1 = "fund"
Range("B1").Select
ActiveCell.FormulaR1C1 = "End Date"
Range("C1").Select
ActiveCell.FormulaR1C1 = "3Mo"
Range("D1").Select
ActiveCell.FormulaR1C1 = "1Yr"
Range("E1").Select
ActiveCell.FormulaR1C1 = "3Yr"
Range("F1").Select
ActiveCell.FormulaR1C1 = "5Yr"
Range("G1").Select
ActiveCell.FormulaR1C1 = "10Yr"
End With
With xlApp
Excel.Application.DisplayAlerts = False
.Workbooks(1).SaveAs "C:\Users\Austin Meier\Desktop\File1.xlsx"
.Quit
End With
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Output
Table", _
"c:\Users\Austin Meier\Desktop\File1.xlsx", True
Set xlApp = Nothing
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlThreeMo = Nothing
Set xlOneYr = Nothing
Set xlThreeYr = Nothing
Set xlFiveYr = Nothing
Set xlTenYr = Nothing
Set xlrng2 = Nothing
Set xlDate = Nothing
Set objRST = Nothing
End Sub
a) There is still an instance of Excel running in my processes once it is
complete
b) Every other time I run it, I get run-time 462 'The remote server machine
does not exist or is unavailable'
If you can help me out I would really appreciate it, thanks a lot.
Option Explicit
Private Sub Command1_Click()
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlWorkbook As Excel.Workbook
Dim strDate As Date
Dim xlThreeMo As Excel.Range
Dim xlOneYr As Excel.Range
Dim xlThreeYr As Excel.Range
Dim xlFiveYr As Excel.Range
Dim xlTenYr As Excel.Range
Dim xlrng2 As Excel.Range
Dim xlDate As Excel.Range
Dim objRST As Recordset
Dim intnumber As Integer
Dim strQuery As String
Dim strSheetName As String
'Queries the data
strQuery = "TRANSFORM Sum([Month Value.return]+1) As Return " & _
"SELECT Fund.Fund " & _
"From Dates, Fund INNER JOIN [Month Value] ON Fund.[Fund ID] =
[Month Value].[Fund ID] " & _
"WHERE (((IIF([Month Value.End Date]>[Dates.Ten Year] AND " & _
"[Month Value.End Date]<=[Dates.This Month],1,0))=1)) " & _
"GROUP BY Fund.Fund " & _
"PIVOT [Month Value].[End Date];"
strSheetName = Left(strQuery, 10)
strSheetName = Trim(strSheetName)
'Creates the worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlWorkbook = xlApp.Workbooks.Add
Set objRST = Application.CurrentDb.OpenRecordset(strQuery)
strDate = Me.Text2.Value
With objRST
If Not .EOF Then .MoveLast
intnumber = objRST.RecordCount
.MoveFirst
End With
Set xlSheet = xlWorkbook.Sheets(1)
With xlSheet
.Range("A2").CopyFromRecordset objRST
.Name = strSheetName
End With
With xlSheet
Set xlThreeMo = xlSheet.Range(.Cells(2, 123), .Cells(intnumber + 1, 123))
Set xlOneYr = xlSheet.Range(.Cells(2, 124), .Cells(intnumber + 1, 124))
Set xlThreeYr = xlSheet.Range(.Cells(2, 125), .Cells(intnumber + 1, 125))
Set xlFiveYr = xlSheet.Range(.Cells(2, 126), .Cells(intnumber + 1, 126))
Set xlTenYr = xlSheet.Range(.Cells(2, 127), .Cells(intnumber + 1, 127))
End With
xlThreeMo.FormulaR1C1 =
"=IF(Countblank((RC[-4]:RC[-2]))>0,-500,Product(RC[-4]:RC[-2])-1)"
xlOneYr.FormulaR1C1 =
"=IF(Countblank((RC[-14]:RC[-3]))>0,-500,Product(RC[-14]:RC[-3])-1)"
xlThreeYr.FormulaR1C1 =
"=IF(Countblank((RC[-39]:RC[-4]))>0,-500,Product(RC[-39]:RC[-4])^(1/3)-1)"
xlFiveYr.FormulaR1C1 =
"=IF(Countblank((RC[-64]:RC[-5]))>0,-500,Product(RC[-64]:RC[-5])^(1/5)-1)"
xlTenYr.FormulaR1C1 =
"=IF(countblank((RC[-125]:RC[-6]))>0,-500,Product(RC[-125]:RC[-6])^(1/10)-1)"
With xlSheet
.Range("A1:ZZ5000").Select
.Range("A1:ZZ5000").Copy
.Range("A1:ZZ5000").Activate
.Range("A1:ZZ5000").PasteSpecial (xlPasteValues)
End With
With xlSheet
.Columns("BQ").Select
.Columns("BQ").Delete Shift:=xlToLeft
End With
With xlSheet
Set xlDate = xlSheet.Range(.Cells(2, 2), .Cells(intnumber + 1, 2))
End With
xlDate.Value = strDate
With xlSheet
Range("A1").Select
ActiveCell.FormulaR1C1 = "fund"
Range("B1").Select
ActiveCell.FormulaR1C1 = "End Date"
Range("C1").Select
ActiveCell.FormulaR1C1 = "3Mo"
Range("D1").Select
ActiveCell.FormulaR1C1 = "1Yr"
Range("E1").Select
ActiveCell.FormulaR1C1 = "3Yr"
Range("F1").Select
ActiveCell.FormulaR1C1 = "5Yr"
Range("G1").Select
ActiveCell.FormulaR1C1 = "10Yr"
End With
With xlApp
Excel.Application.DisplayAlerts = False
.Workbooks(1).SaveAs "C:\Users\Austin Meier\Desktop\File1.xlsx"
.Quit
End With
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Output
Table", _
"c:\Users\Austin Meier\Desktop\File1.xlsx", True
Set xlApp = Nothing
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlThreeMo = Nothing
Set xlOneYr = Nothing
Set xlThreeYr = Nothing
Set xlFiveYr = Nothing
Set xlTenYr = Nothing
Set xlrng2 = Nothing
Set xlDate = Nothing
Set objRST = Nothing
End Sub