我在文本文件中打印值時遇到問題。每當我有一個日期或數字,我會得到一個「錯誤13」......我想它與我的變量有關,但我不明白問題出在哪裏。VBA - 將文本行打印到.txt文件
Sub SX_Externe_Generate_TXT()
Dim WsCib As Worksheet
Dim LastCol, LastRow, i, j As Byte
Application.ScreenUpdating = False
Set Ws = ThisWorkbook.Sheets("Externe")
Set WsCib = ThisWorkbook.Sheets("SX_Externe")
LastCol = WsCib.Cells(1, 1).SpecialCells(xlCellTypeLastCell).Column
LastRow = WsCib.Cells(1, 1).SpecialCells(xlCellTypeLastCell).Row
WsCib.Activate
Open "C:\Users\xizx692\Desktop\SX_Externe_Temp.txt" For Output As #1 'Tu crées le fichier text
i = 1
For i = 1 To LastRow
j = 1
For j = 1 To LastCol
Print #1, WsCib.Cells(i, j).Value + ";"; 'Séparateur = ;
Next j
Print #1, WsCib.Cells(i, j + 1).Value 'pas de séparateur (Saut de ligne)
Next i
Close #1
Ws.Activate
Application.ScreenUpdating = False
End Sub
的錯誤出現在這個線路印刷#1,WsCib.Cells(I,J)。價值+ 「;」; 'Séparateur=;每當單元格(i,j).value是日期或數字格式。
感謝您的幫助提前。
BR,
讓
灰,非常感謝您的回答。它幫助我解決我的問題,而且:它教會了我很棒的東西。我之所以使用「字節」是因爲我知道將有不到250行,我認爲我的代碼會更快......對我感到羞恥嗎?我不知道j,我會在循環結尾回到「1」,這就是爲什麼我要強制j = 1!你想參加我提出的另一個問題嗎? [去這裏](https://stackoverflow.com/questions/44592776/action-when-linked-cell-of-option-button-changes/44593266#44593266)如果是! **需要解決所有問題; D ** –
想過,爲什麼將LastCol,LastRow和我定義爲變體?謝謝! –
@ Jean-FIC我的意思是你的初始聲明將它們聲明爲Variant,而不是Byte。對所有的整型變量使用'Long',因爲它是32位的,所以它會最快,並且不易發生溢出。唯一可以考慮'Integer'或'Byte'的地方是當你有*巨大的數組*可能會導致內存問題。 –