Hi,
I'm running into a lot of problems with this macro: at times, it will, with no problems, paste the appropriate range into the specified location. Other times, nothing will be pasted. Several others have looked at this, and no one can figure it out.
I know for a fact the "TransposeArray" function works and the "SendArraytoFile" sub works (they have been tested separately). However, the array is lost somewhere between the above sub and the below one. (Specifically, in the below sub, after "Open sFileName For Output As #fNum", it jumps straight to "close #fnum".Public Sub OutputData() Dim vData As Variant Dim myRange As Range Dim OutputName As String Dim OutputDate As Long Dim OutputDate2 As Long Dim StrMonth As String Dim StrYear As String Dim Sheet As Worksheet Dim calculatorname As String Dim CSVFileName As String Dim OutputArr As Variant calculatorname = ThisWorkbook.Name For Each Sheet In Worksheets If UCase(Left(Sheet.Name, 2)) = "EV" Then OutputName = Workbooks(calculatorname).Sheets(Sheet.Name).Range("C11") OutputDate = Workbooks(calculatorname).Sheets(Sheet.Name).Range("C15") StrMonth = Right("0" & Month(OutputDate), 2) StrYear = Right(Year(OutputDate), 2) OutputDate2 = StrMonth & StrYear CSVFileName = "C:\" & OutputName & OutputDate2 & ".csv" Set myRange = Application.Range("Q144:Q264") vData = myRange Call TransposeArray(vData, OutputArr) Call SendArrayToFile(CSVFileName, OutputArr, "") End If Next Sheet End Sub
I can simplify this code if need be, but I'm running out of ideas, and thought I should put up the entire code in the event it's something I wouldn't have thought of.Public Sub SendArrayToFile(sFileName As String, OutputArr As Variant, sComment As Variant, Optional nPrecision As Long = 7) Dim i As Long, j As Long, lTop1 As Long, lTop2 As Long, fNum As Integer, dValue As Double Dim CurrentIter As String CurrentIter = Evaluate("RiskCurrentIter()") On Error GoTo Failed: If (nPrecision <= 0) Then nPrecision = 7 lTop1 = UBound(OutputArr, 1) lTop2 = UBound(OutputArr, 2) fNum = FreeFile Open sFileName For Output As #fNum 'Print #fNum, "// " + sComment For i = 1 To lTop1 For j = 1 To lTop2 - 1 dValue = IIf(IsError(OutputArr(i, j)), 0#, CDbl(OutputArr(i, j))) Write #fNum, Round(dValue, nPrecision), Cells(CurrentIter, 1); Next j dValue = IIf(IsError(OutputArr(i, lTop2)), 0#, CDbl(OutputArr(i, lTop2))) Print #fNum, Round(dValue, nPrecision) ' write out the last item and CRLF Next i Failed: Close #fNum End Sub
Thank you!!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks