如果文件存在,我正在尝试使用Y或N填充excel中的单元格。我认为我需要一个宏,因为我不确定公式是否有效。如果我的文件名是\ xyzmedia \ 08242018abcapp \ wyoming \ archive,那么它将在单元格C10中;我可以用什么代码用Y或N填充特定的excel单元格?在这个特定的例子中,假设单元格D10。我将每天为大约8个文件执行此操作,因此需要更新的8个单元格。理想情况下,我希望它们都通过相同的宏更新。
如果我还可以在工作表上嵌入“搜索文件”按钮来运行宏,那将是令人惊讶的。
日期每天都在变化,所以我猜我必须每周手动更改5个宏中的日期,这很好。但有解决方法吗?再次,如果没有,完全没问题。
我不知道我在做什么,因为我对宏很新...但经过一些研究,也许代码会是这样的? (Check if the file exists using VBA):
Sub test()
thesentence = InputBox("\\xyzmedia\08242018abcapp\wyoming\archive", "Raw Data File")
Range("A1").Value = thesentence
If Dir("thesentence") <> "" Then
D10 = "Y"
Else
D10 = "N"
End If
End Sub
请帮忙!谢谢!!
这里有一些代码给你......帮助一下。
'To check if a particular file exists
'excelFile = False, if it is not an Excel file that is being checked
Public Function isAnExistingFile(ByVal fileNameStr As Variant, Optional ByVal excelFile As Boolean = True) As Boolean
Dim wb As Workbook
isAnExistingFile = True
On Error Resume Next
If Not VarType(fileNameStr) = vbString Then
isAnExistingFile = False
ElseIf Len(fileNameStr) = 0 Then
isAnExistingFile = False
ElseIf Len(Dir(fileNameStr)) = 0 Then
isAnExistingFile = False
ElseIf ((GetAttr(fileNameStr) And vbDirectory) <> vbDirectory) = False Then
isAnExistingFile = False
Else
If excelFile Then
Set wb = Application.Workbooks.Open(Filename:=fileNameStr, UpdateLinks:=0, ReadOnly:=True)
If wb Is Nothing Then isAnExistingFile = False
If Not wb Is Nothing Then
wb.Close False
Set wb = Nothing
End If
End If
End If
Err.Clear: On Error GoTo 0
End Function