无法使用variablesinput
背景
当我试图在大量的csv数据文件上运行一系列技术指标时,我将3个macros打包在一起,
- macros1:运行分析>打开特定文件夹中的每个单独的Excel文件
- macros2:DoWork>运行macrosmacros3:IndicatorData>使用技术指标公式打开macros1打开的单个csv文件。
代码如下。
问题/发行
- 问题出在macros观3 [指标数据]
在填充列H时,通过vba有select地使用从同一工作簿中find的名为ControlPanel的工作表捕获值的variablesEMAWindow1的值。
在前两行公式中
ActiveSheet.Range("h1") = EMAWindow1 & " Day EMA" ActiveSheet.Range("h" & EMAWindow1 + 1) = "=average(R[-" & EMAWindow1 - 1 & "]C[-3]:RC[-3])"
你会发现macros观工作正在完善。 然而
当你到达第三线时
ActiveSheet.Range("h" & EMAWindow1 + 2 & ":h" & LastRowCheck).FormulaR1C1 = "=R[0]C[-3]*(2/(EMAWindow1 + 1)) + R[-1]C[0] * (1-(2/(EMAWindow1 + 1)))"
出于某种原因,EMAWindow1的值不被使用,并被视为一个文本造成的错误,并导致#NAME? 因为公式无法用里面的文本计算。
随后的代码遇到同样的问题。 任何人都面临同样的问题? 我一直在这个问题上,我希望有一些启发。
代码
Sub RunAnalysis() Dim Filename, Pathname As String Dim wb As Workbook Pathname = "C:\Users\gabri_000\Desktop\Trading Data\Stocks - 1 - 100\" Filename = Dir(Pathname & "*.csv") Do While Filename <> "" Set wb = Workbooks.Open(Pathname & Filename) DoWork wb wb.Close SaveChanges:=True Filename = Dir() Loop End Sub
Sub DoWork(wb As Workbook) With wb Call IndicatorData End With End Sub
Sub IndicatorData() Dim LastRowCheck As Long Dim EMAWindow1, EMAWindow2, EMAWindow3 As Long Dim Pathname, Filename, FileNameIndex, FinalFileName As String 'Converting file path into a string Pathname = "C:\Users\gabri_000\Desktop\Trading Data\Stocks - 1 - 100\" 'Full file name Filename = Dir(Pathname) 'Removing the .csv portion of the file name FileNameIndex = InStr(1, Filename, ".csv") FinalFileName = Left(Filename, FileNameIndex - 1) 'Selecting Recently Opened File Sheets(FinalFileName).Select 'Counting Number of Rows in the worksheet With Worksheets(FinalFileName) LastRowCheck = .Range("A" & .Rows.Count).End(xlUp).Row End With 'EMA Inputs 'Obtaining data from masterfile With ThisWorkbook.Sheets("ControlPanel") EMAWindow1 = .Range("EMAWindow1") EMAWindow2 = .Range("EMAWindow2") EMAWindow3 = .Range("EMAWindow3") End With 'EMA Calcuations ActiveSheet.Range("h1") = EMAWindow1 & " Day EMA" ActiveSheet.Range("h" & EMAWindow1 + 1) = "=average(R[-" & EMAWindow1 - 1 & "]C[-3]:RC[-3])" ActiveSheet.Range("h" & EMAWindow1 + 2 & ":h" & LastRowCheck).FormulaR1C1 = "=R[0]C[-3]*(2/(EMAWindow1 + 1)) + R[-1]C[0] * (1-(2/(EMAWindow1 + 1)))" ActiveSheet.Range("i1") = EMAWindow2 & " Day EMA" ActiveSheet.Range("i" & EMAWindow2 + 1) = "=average(R[-" & EMAWindow2 - 1 & "]C[-4]:RC[-4])" ActiveSheet.Range("i" & EMAWindow2 + 2 & ":i" & LastRowCheck).FormulaR1C1 = "=R[0]C[-4]*(2/(EMAWindow2 + 1)) + R[-1]C[0] * (1-(2/(EMAWindow2 + 1)))" ActiveSheet.Range("j1") = EMAWindow3 & " Day EMA" ActiveSheet.Range("j" & EMAWindow3 + 1) = "=average(R[-" & EMAWindow3 - 1 & "]C[-5]:RC[-5])" ActiveSheet.Range("j" & EMAWindow3 + 2 & ":i" & LastRowCheck).FormulaR1C1 = "=R[0]C[-5]*(2/(EMAWindow3 + 1)) + R[-1]C[0] * (1-(2/(EMAWindow3 + 1)))" End Sub