I have the following macro that works perfectly within Excel but I know absolutly nothing about vbscripting other than what it has in common with vba. I have tried a few things and no longer get errors but it still does not function. The purpose of the code is to open an excel sheet full of inspection data and turn the cells Red, Amber, Or Green depending on if the feature is in out of tolerance, using > 80% of its tolerance, or in tolerance. Within excel this will loop for all files selected in the fDialog and i would like to keep that functionality if possible.
'#================================================================================
'# MakeRAG.vbs |
'#--------------------------------------------------------------------------------
'# |
'# Function:- |
'# Script will convert standard crystal reports in .xlsx format to RAG Charts|
'# Parameters:- |
'# none |
'# Returns:- |
'# nothing |
'#================================================================================
'# +---------+----------+---------------------------------------+----------------+
'# | Version | Date | Changes | By |
'# | 1.00 | 11/02/20 |First Release | -------------- |
'# | | | | |
'# | | | | |
'# +---------+----------+---------------------------------------+----------------+
'#================================================================================
Option Explicit
Sub Main()
'
Dim i 'As Integer
Dim j 'As Integer
Dim nominal 'As Double
Dim upperTol 'As Double
Dim lowerTol 'As Double
Dim upperAmber 'As Double
Dim lowerAmber 'As Double
Dim amberPercent 'As Double
Dim fDialog 'As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
Dim thing 'As Variant
Dim xl 'As Excel.Application
Dim ragChart 'As Excel.Workbook
amberPercent = 0.8 'Feature will show as amber if exceeding this percent of tolerance
Set xl = CreateObject("Excel.Application")
With fDialog
.AllowMultiSelect = True
.Title = "Select files to make into RAG Charts"
.InitialFileName = "C:\"
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsx"
End With
If fDialog.Show = -1 Then
'Loop through all files selected in the File Open Dialog
For Each thing In fDialog.SelectedItems
'Open Workbook
Set ragChart = xl.Workbooks.Open(thing)
With ragChart.Sheets(1)
'Loop through all columns starting at column 5
For i = 5 To Application.WorksheetFunction.CountA(.Range("A3").EntireRow)
nominal = Cells(5, i).Value
upperTol = Cells(4, i).Value
lowerTol = Cells(6, i).Value
upperAmber = nominal + ((upperTol - nominal) * amberPercent)
lowerAmber = nominal - ((nominal - lowerTol) * amberPercent)
'Loop through all rows in current column
For j = 7 To Application.WorksheetFunction.CountA(.Range("B7").EntireColumn) + 7
If Cells(j, i).Value = "" Then
Cells(j, i).Interior.Color = xlNone
ElseIf Cells(j, i).Value > upperTol Or Cells(j, i).Value < lowerTol Then
Cells(j, i).Interior.Color = RGB(255, 0, 0)
ElseIf Cells(j, i).Value > upperAmber Or Cells(j, i).Value < lowerAmber Then
Cells(j, i).Interior.Color = RGB(255, 191, 0)
Else
Cells(j, i).Interior.Color = RGB(0, 255, 0)
End If
Next' j
Next' i
End With
'Save and close Workbook
ragChart.Save
ragChart.Quit
Next' thing
End If
End Sub