vba如果单元格包含与文件名相匹配的值,那么说好吧,别说不好?

我已经更新我的代码,以解释更多关于它是什么,即时尝试做…

我生成一个随机的数字和字母的string,并创build一个随机生成的string的名称的文本文件,在这种情况下,即时通讯调用我的string: strPassword

现在,一旦文本文件被创build,我想执行检查,看看这个文件名是否存在。 所以这意味着检索创build并保存为文件名的随机string。

如果文件存在,我应该得到一个消息说,否则应该说不行。

我正在尝试通过调用strPassword来检索文件的名称

由于某种原因,它告诉我该文件不存在,所以我只能假设它无法获得strPassword的值。 它会出现一旦一个随机数已经生成它忘记了什么是生成的,所以有没有办法我可以将strPassword保存到一个variables或会话或某种内存来执行检查文件名后?

单击单元格A1时,将检查单元格S32中是否存在文件名,如果不存在则显示一条消息,并使用随机stringstrPassword创build并保存该文件。

然后如果我想要input这个string名称到单元格S32中,它应该说文件存在,因为它刚创build它,但它不是它的说明文件不存在。

例如,如果我有一个文本文件的文件名是由usiung strPassword随机生成位于:

 P:\XBQILGHD.txt 

所以在我的单元格中,如果我要键入:

 XBQILGHD (which is the strPassword value) 

那么我会得到一个消息,说'好',否则我会得到一个消息,说'不好'

有人可以告诉我在这里做错了吗? 谢谢

  Dim strPassword As String Dim i As Integer For i = 1 To 10 If i Mod 2 = 0 Then strPassword = Chr(Int((90 - 65 + 1) * Rnd + 65)) & strPassword Else strPassword = Int((9 * Rnd) + 1) & strPassword End If Next i If Dir("P:\" & strPassword & ".txt") <> "" Then MsgBox "File exists" Else MSG3 = MsgBox("Hi " & Range("N9").Value & "," & vbNewLine & vbNewLine & "Unfortunately you have used up your entire allowance for this month." & vbNewLine & vbNewLine & "You will not be able to continue with this request. Please wait until next month, when your request allowance will be reset." & vbNewLine & vbNewLine & "Think this is an Error?" & vbNewLine & "Contac us at NewSuppliers@Hewden.co.uk to make an appeal.", vbOKOnly, "Important Notice!") Set fs = CreateObject("Scripting.FileSystemObject") Set a = fs.CreateTextFile("P:\" & strPassword & ".txt", True) a.WriteLine (strPassword) a.Close End If 

如果我明白你的问题尝试:在文件ttp.txt被保存你的密码

  Dim strPassword As String Dim i As Integer For i = 1 To 10 If i Mod 2 = 0 Then strPassword = Chr(Int((90 - 65 + 1) * Rnd + 65)) & strPassword Else strPassword = Int((9 * Rnd) + 1) & strPassword End If Next i If Dir("P:\ttp.txt") <> "" Then MsgBox "File exists" Else MSG3 = MsgBox("Hi " & Range("N9").Value & "," & vbNewLine & vbNewLine & "Unfortunately you have used up your `enter code here`entire allowance for this month." & vbNewLine & vbNewLine & "You will not be able to continue with this request. Please wait until next month, when your request allowance will be reset." & vbNewLine & vbNewLine & "Think this is an Error?" & vbNewLine & "Contac us at NewSuppliers@Hewden.co.uk to make an appeal.", vbOKOnly, "Important Notice!") Set fs = CreateObject("Scripting.FileSystemObject") Set a = fs.CreateTextFile("P:\ttp.txt", True) a.WriteLine (strPassword) a.Close End If