I have a workbook with many named cells in different worksheets. I'm trying to write a VBA script that will read an external .csv file to extract a variable name and variable value, so that I can update the named cell values in my workbook.
I'm able to read and loop through the data in the CSV file but I am unable to update the named values.
Ideally, the script would check the variable name is a valid named cell in the workbook then update with the new value as defined in the .csv file.
I've run a number of iterations but the gist of the code is:
Public Sub readCSV()
'
' VBA script to read external CSV file
'
'
Dim filePath As String
Dim inFilePath As String
Dim inCase As String
strWorkBook = ActiveWorkbook.Name
filePath = Range("aString").Value
tmpsep = InStrRev(filePath, "\")
inCase = Right(filePath, Len(filePath) - tmpsep)
inFilePath = Left(filePath, Len(filePath) - Len(inCase))
' Check that path is valid and exit if not
Range("aString").Select
If IsEmpty(ActiveCell.Value) Then
MsgBox "ERROR! No Input File Defined - Exiting!"
Range("H7").Select
End
End If
' Open data file
Workbooks.Open Filename:=filePath
' Loop through variable names in input file
varNamCol = "C"
varColNum = "D"
' Ensure we're in input file
Windows(inCase).Activate
' Find last row input file - Call separate routine (working)
Call FindLastRow.FindLastRow(lRow)
i = 1
imax = lRow
Do While i <= imax
Windows(inCase).Activate
' Read Variable Name and Value from csv
inVarName = Range(varNamCol & I).Value
inVarValue = Range(varColNum & I).Value
If IsEmpty(inVarName) Then
MsgBox " Variable is empty - Moving On"
GoTo NextIteration
Else
Windows(strWorkBook).Activate
Range(inVarName).Value = inVarValue
End If
NextIteration:
i = i + 1
Loop
End Sub