Quantcast
Channel: OSIsoft Users Community
Viewing all articles
Browse latest Browse all 1120

Populating Access w/ Excel PI data VBA

$
0
0

Hello,

 

I'm new to the PI system but fairly experienced with VBA. First some background on the application.

 

I have been tasked with creating an Access database to store production data from our plant. The production data can be gathered directly with the PIAdvCalcVal tag. I have written VBA code in an Excel spreadsheet to automatically update the data when the spreadsheet is opened. This works fine when I run it by itself. I then created a linked table in Access referencing this spreadsheet. So far, so good. I have an AutoExec macro in Access that opens the Excel spreadsheet when Access is opened. In this scenario, Excel is acting somewhat like a backend database for Access (I know this is backwards to the way it should be).

 

I run into a problem when I open Access and the macro in Excel tries to update the PI data. An error appears in the cells that have the PI data that says the formula contains unrecognizable text in the formula. When I step through the calculations Excel does not recognize the PIAdvCalcVal( portion of the formula. I get a #Name? error in each cell with the PI data. I think what's happening is that the macro runs before the PI add-in can load.

 

Code in Excel:

Sub GetPIData()
Dim today, datediff As Long
Dim i, daterow, formulastartdaterow, formulaenddaterow As Integer
Dim formula As String
Dim daterange As Range
Dim xlapp As Excel.Application

today = Date

'date of last production entry
daterow = Range("B1").End(xlDown).row

'difference between today's date and last entered production date. i.e. Over the weekend.
datediff = today - Cells(daterow, 1).Value
      
formulastartdaterow = daterow + 1
formulaenddaterow = daterow + datediff - 1

'Check if the calculations have already been done today
'Might capture this in the Access VBA code?
If datediff > 1 Then
    'copy dates and target production down to today
    Cells(daterow, 1).Select
    Selection.AutoFill Destination:=Range("A" & daterow & ":A" & daterow + datediff), Type:=xlFillDefault
   
    Cells(daterow, 9).Select
    Selection.AutoFill Destination:=Range("I" & daterow & ":I" & daterow + datediff - 1), Type:=xlFillDefault
   
    For i = formulastartdaterow To formulaenddaterow
       
        Cells(i, 2).FormulaR1C1 = "=ROUND(PIAdvCalcVal(""FI5038.PV"",Summary!RC[-1],Summary!R[1]C[-1],""average"",""time-weighted"",0,1,0,""grnrvr-pi"")*24,0)"
        Cells(i, 3).FormulaR1C1 = "=RC[6]-RC[-1]"
        Cells(i, 8).FormulaR1C1 = "=RC[-5]-SUM(RC[-4]:RC[-1])"
   
    Next i
Application.CalculateFullRebuild

 

Code in Access:

Public Function GetProductionFromExcel()
Dim Path As String
Dim objXL As Excel.Application
Dim xlWB As Excel.Workbook

 
Set objXL = New Excel.Application
objXL.Visible = True

Path = "S:\Granger Reliability\Loss Accounting\PI Data for Loss Accounting.xlsm"
Set xlWB = objXL.Workbooks.Open(Path)

End Function

 

 

Any help would be greatly appreciated.

 


Viewing all articles
Browse latest Browse all 1120

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>