2017-06-15 56 views
1

我的布爾函數有一個問題。它被稱爲像這樣VBA:布爾運算不正常

TxtFile True, FileDir, 1, "Hello"

而且功能

TxtFile(WritetoFile As Boolean, FileDir As String, line As Variant, What As String)

該函數的第一件事是用簡單的Ifelseif陳述來測試真或假。當我運行我的功能手錶告訴我,WritetoFile As Booleanfalse。但是,如果我調用相同的功能,並通過DeBug一步一步通過,請注意它是True。我在另一個表單上調用了8種不同方式的函數,沒有任何問題。

編輯:全功能

Option Compare Database 
Public ReadOptionsOutput As String 
Public FSO As New FileSystemObject 
Public TxtFileOutPut As String 

Public Function TxtFile(WritetoFile As Boolean, FileDir As String, line As Variant, What As String) 
Dim FileContent() As Variant 
Dim Idx As Integer 
Dim Txtstream As Object 

If WritetoFile = True Then 
Set Txtstream = FSO.OpenTextFile(FileDir, ForReading, False) 
Idx = 0 

On Error GoTo Err1 'To catch the last blank line. Dont see another way to see if .ReadLine is blank 
Do 'Build an array to edit lines in Text file 
    Idx = Idx + 1 
    ReDim Preserve FileContent(1 To Idx) 
    FileContent(Idx) = Txtstream.ReadLine 
Loop 
Err1: 
Open FileDir For Output As #1: Close #1 'Delet all text inside of File 
Set Txtstream = Nothing 
Set Txtstream = FSO.OpenTextFile(FileDir, ForAppending, False) 
FileContent(line) = What 'Edit line in the array 
    For Idx = 1 To Idx - 1 
     Txtstream.WriteLine (FileContent(Idx)) 'Write everything back to textfile 
    Next 


ElseIf WritetoFile = False Then 'Reads Line in file only 
Set Txtstream = FSO.OpenTextFile(FileDir, ForReading, False) 
NextLine = 1 

Do 'Loop thru to selected line and read it 
    TxtLine = Txtstream.ReadLine 
    If NextLine = line Then 
     TxtFileOutPut = TxtLine 
    End If 
    NextLine = NextLine + 1 
Loop Until NextLine > line 
End If 
Txtstream.Close 
End Function 
+2

你能提供整個代碼嗎?哪個應用程序(Excel,Access)? – Wernerson

+0

@Wernerson編輯完整功能和VBA訪問標籤。如果你想看到其他人,請告訴我。 – Quint

+0

你究竟是什麼意思,「當我運行我的功能時,Watch告訴我WritetoFile As Boolean is false」。當我查看手錶時(查看 - >手錶),它告訴我WritetoFile是真的... – Wernerson

回答

0

嘗試和調用該函數分變量賦值。像這樣:

Dim WritetoFile As Boolean 
WritetoFile = True 
TxtFile WritetoFile, FileDir, 1, "Hello" 

希望這會有所幫助。

+0

剛剛嘗試過,它將其稱爲假。通過它,它稱它爲真。很高興嘗試壽。感謝您的輸入。 – Quint

+0

也許是一個編譯問題?嘗試編譯並再次運行它。@Quint –

+0

只需反編譯並編譯它。問題是持續的 – Quint