2017-06-15 57 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