У меня есть форма Excel, которую я использую, которую я унаследовал. Я получаю и ошибку индекса вне диапазона и не знаю почему. Это позволит экспортировать суммы бюджета на листе «Счета» в блокнот. Строка LinetoWriteArray подсвечивается.
Dim I, J As Long ' I counts the row numbers in the MODELBPL sheet, J counts the column numbers in the MODELBPL sheet
Dim LineToWrite As String ' is the string that represents the entire comma delimited line to be added to the end of the export file
I = 2 'start with row 2
LineToWrite = "" 'start with a blank line to write
Do Until Sheets("MODELBPL").Range("A" & I).Value = CostCode And Sheets("MODELBPL").Range("B" & I).Value = CostType 'loop through rows until the cost code and cost type are found
I = I + 1 'step to the next row
If I > Range("AA6").Value Then 'stop this loop and exit the subroutine if the entire list has been searched and the values not found.
MsgBox (CostCode & " cost code, and " & CostType & " cost type values not found in MODELBPL.") ' warns the user the cost type and code were not found
Exit Sub 'exit this subroutine and continue to the next budget line
End If 'end if for not found
Loop 'continue loop to next row
For J = 1 To 42 ' loop through the columns in the MODELBPL sheet
'DJM 4/17/2015: GLT Project - Added to print NewCostType instead of original CostType found in MODELBPL
If J = 2 Then ' when at CostType column, insert NewCostType
LineToWrite = LineToWrite & NewCostType 'add the NewCostType amount to the LineToWrite
ElseIf J = 11 Then ' when at the budget amount column, insert the budget amount
LineToWrite = LineToWrite & BudgetAmount 'add the budget amount to the LineToWrite
'DJM 4/17/2015: GLT Project - Added to print "" in first column, rather than "old" CostCode
ElseIf J = 1 Then ' when at the CostCode column, insert blank
LineToWrite = LineToWrite & "" 'add blank to the LineToWrite
Else
'The Following code added by Rama Kattunga to add Alternate Cost Code at column 13 in CSV file
If J = 13 Then
LineToWrite = LineToWrite & AltCostCode
End If
LineToWrite = LineToWrite & Format(Sheets("MODELBPL").Cells(I, J).Value) 'write whatever is in the MODELBPL sheet
End If 'end if to find the budget column
If J < 42 Then LineToWrite = LineToWrite & "," 'adds a comma between the values for all except the last column
Next J 'loop to the next column
'DJM 4/17/2015: GLT Project - removed the line below to print - will print in calling sub after sorting and combining by CostType
'Print #1, LineToWrite ' write the string to the end of the file
LineToWriteArray(L, L + 2) = LineToWrite 'DJM 4/17/2015: GLT Project - Writing line detail to array