0% found this document useful (0 votes)
0 views

Excel Vba Module

This document contains VBA macros for Excel that fetch PCR data from a Python script, schedule hourly updates, and format cells based on PCR values. It includes functions to retrieve the latest PCR value and provides instructions for using the module effectively. The macros enhance data integration and visualization for the NIFTY BCKTESTING.xlsx file.

Uploaded by

bdaychotu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Excel Vba Module

This document contains VBA macros for Excel that fetch PCR data from a Python script, schedule hourly updates, and format cells based on PCR values. It includes functions to retrieve the latest PCR value and provides instructions for using the module effectively. The macros enhance data integration and visualization for the NIFTY BCKTESTING.xlsx file.

Uploaded by

bdaychotu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sub FetchNSEPCRData()

' This macro runs the Python script to fetch PCR data and updates the Excel
sheet
' Specifically designed for NIFTY BCKTESTING.xlsx

' Show status message


Application.StatusBar = "Fetching PCR data from NSE..."

' Define the path to the Python script


Dim scriptPath As String
scriptPath = "C:\Users\DOGA\Documents\NIFTY PCR\excel_integration.py"

' Define the command to run the Python script


Dim command As String
command = "python """ & scriptPath & """"

' Run the command using Shell


Dim returnValue As Variant
returnValue = Shell("cmd.exe /c " & command, vbMinimized)

' Wait for a moment to allow the script to complete


Application.Wait Now + TimeValue("00:00:05")

' Refresh data


ActiveWorkbook.RefreshAll

' Clear status bar


Application.StatusBar = False

' Show completion message


MsgBox "PCR data has been updated in NIFTY BCKTESTING.xlsx!", vbInformation,
"NSE PCR Data"
End Sub

Sub ScheduleDataRefresh()
' This macro sets up a timed automatic refresh for PCR data

' Set up a timer to run every hour


Application.OnTime Now + TimeValue("01:00:00"), "FetchNSEPCRData"

MsgBox "Data refresh scheduled for hourly updates. " & _


"The first update will run in 1 hour." & vbCrLf & vbCrLf & _
"Note: Excel must remain open for automatic updates to work.", _
vbInformation, "NSE PCR Data Scheduler"
End Sub

Function FormatColorByPCR(value As Variant) As Variant


' This function formats a cell based on PCR value
' Apply in Conditional Formatting or as a worksheet function

' Check if value is a number


If IsNumeric(value) Then
If value > 1.2 Then
' Very bullish - Green
FormatColorByPCR = RGB(0, 128, 0)
ElseIf value > 1 Then
' Slightly bullish - Light green
FormatColorByPCR = RGB(144, 238, 144)
ElseIf value >= 0.8 Then
' Neutral - Yellow
FormatColorByPCR = RGB(255, 255, 0)
Else
' Bearish - Red
FormatColorByPCR = RGB(255, 0, 0)
End If
Else
' Return black for non-numeric values
FormatColorByPCR = RGB(0, 0, 0)
End If
End Function

Function GetLatestPCR() As Variant


' This function returns the latest PCR value from the data sheet
' Useful for dashboard or summary sheets

' Specify the sheet name and column index


Dim sheetName As String
Dim pcrColumn As Integer

sheetName = "PCR_Data" ' Change to your actual sheet name


pcrColumn = 3 ' Column C (Weekly_PCR) - adjust as needed

' Find the sheet


On Error Resume Next
Dim dataSheet As Worksheet
Set dataSheet = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0

' Check if the sheet exists


If dataSheet Is Nothing Then
GetLatestPCR = "Sheet not found"
Exit Function
End If

' Find the last row with data


Dim lastRow As Long
lastRow = dataSheet.Cells(dataSheet.Rows.Count, 1).End(xlUp).Row

' Check if data exists


If lastRow <= 1 Then
GetLatestPCR = "No data"
Exit Function
End If

' Return the latest PCR value


GetLatestPCR = dataSheet.Cells(lastRow, pcrColumn).Value
End Function

' HOW TO USE THIS MODULE:


' 1. Open your Excel file
' 2. Press Alt+F11 to open the VBA Editor
' 3. Insert a new module (Insert > Module)
' 4. Copy and paste this entire code
' 5. Save your Excel file as a macro-enabled workbook (.xlsm)
' 6. Create a button on your worksheet (Developer tab > Insert > Button)
' 7. Assign the "FetchNSEPCRData" macro to the button
'
' For automatic updates:
' 1. Create another button on your worksheet
' 2. Assign the "ScheduleDataRefresh" macro to this button
' 3. Click the button to set up hourly updates
'
' Custom formatting:
' - Use the FormatColorByPCR function in conditional formatting to color cells
' based on PCR values
'
' Dashboard integration:
' - Use the GetLatestPCR function to display the latest PCR value on a dashboard
' Example: =GetLatestPCR()

You might also like