我已经更新了我的代码,以解释更多关于我试图做什么......
我正在生成一个带有数字和字母的随机字符串,并使用随机生成的字符串的名称创建一个文本文件,在本例中,我调用我的字符串:strPassword
现在,一旦创建了文本文件,我想执行检查以查看此文件名是否存在。因此,这意味着检索创建并保存为文件名的随机字符串。
如果文件存在,我应该收到一条消息说好的,否则它应该说不正常。
我正在尝试通过调用strPassword
来检索文件名
出于某种原因,它告诉我该文件不存在,所以我只能假设它在获取 strPassword
的值时遇到问题。 一旦生成了一个随机数,它就会忘记生成的内容,那么有没有办法将strPassword保存到变量或会话或某种内存中,以便稍后检查文件名?
单击我的单元格 A1 时,它会执行检查以查看文件名是否存在于单元格 S32 中,如果不存在,则会显示一条消息,并使用随机字符串创建文件strPassword
并保存。
然后,如果我想在单元格 S32 中键入此字符串名称,它应该说文件存在,因为它刚刚创建了它,但它不是它所说的文件不存在。
例如,如果我有一个文本文件,其文件名是由位于以下位置的usiung strPassword随机生成的:
P:XBQILGHD.txt
所以在 excel 中的单元格中,如果我要输入:
XBQILGHD (which is the strPassword value)
然后我会收到一条消息,说"OK",否则我会收到一条消息,说"不好"
有人可以告诉我我在这里做错了什么吗?谢谢
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 保存了您的 pwd
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