Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA将新文件名添加到电子表格中_Vba_Excel - Fatal编程技术网

VBA将新文件名添加到电子表格中

VBA将新文件名添加到电子表格中,vba,excel,Vba,Excel,有人能帮忙吗?(VBA新增) 我正在尝试创建一个VBA,它查看电子表格中当前的文件名列表,查找不在列表中的文件名,并用新文件名填充电子表格 例如: 我在“I”列中有一个文件名列表。我需要我的VBA将此列表与我的文件位置(在本例中为“Z:\CLIENTNAME\Waiting\Non Priority”)进行比较,并添加新的文件名。但是VBA不能删除/覆盖已有的内容,只能在下一行添加新值。如果可以,VBA也不应包括文档类型,例如.docx(不是必需的,因为我可以使用公式来消除这种情况)。我还需要V

有人能帮忙吗?(VBA新增)

我正在尝试创建一个VBA,它查看电子表格中当前的文件名列表,查找不在列表中的文件名,并用新文件名填充电子表格

例如:

我在“I”列中有一个文件名列表。我需要我的VBA将此列表与我的文件位置(在本例中为“Z:\CLIENTNAME\Waiting\Non Priority”)进行比较,并添加新的文件名。但是VBA不能删除/覆盖已有的内容,只能在下一行添加新值。如果可以,VBA也不应包括文档类型,例如.docx(不是必需的,因为我可以使用公式来消除这种情况)。我还需要VBA在工作簿关闭时运行(不确定如何运行)

我一直在使用的代码如下,但我面临的问题是:

  • 仅将文件名添加到A列,我似乎无法更改它
  • 添加文档类型
  • 下面的代码将替换现有值,并且不会将当前值与文件夹进行比较
  • 工作簿关闭时不运行
VBA代码:

Sub GetFileNames()
    Dim sPath As String
    Dim sFile As String
    Dim iRow As Integer
    Dim iCol As Integer
    Dim splitFile As Variant

   'specify directory to use - must end in "\"
   sPath = "Z:\NAME\Waiting\Non_Priority\"

   iRow = 1
   sFile = Dir(sPath)
    Do While sFile <> ""
        iRow = iRow + 1
        splitFile = Split(sFile, "-")
        For iCol = 0 To UBound(splitFile)
            Sheet1.Cells(iRow, iCol + 1) = splitFile(iCol)
       Next iCol
       sFile = Dir     ' Get next filename
  Loop
End Sub
Sub-GetFileNames()
像细绳一样暗淡
将文件设置为字符串
Dim iRow作为整数
作为整数的Dim-iCol
Dim splitFile作为变量
'指定要使用的目录-必须以“\”结尾
sPath=“Z:\NAME\Waiting\Non\u Priority\”
iRow=1
sFile=Dir(sPath)
在sFile“”时执行此操作
iRow=iRow+1
splitFile=Split(sFile,“-”)
对于iCol=0到UBound(拆分文件)
Sheet1.单元格(iRow,iCol+1)=拆分文件(iCol)
下一个iCol
sFile=Dir'获取下一个文件名
环
端接头

谢谢

如注释中所述,当文件关闭时,您无法运行VBA,但如果您的文件已打开,并且您运行以下代码,它将为您提供预期的结果:

Sub GetFileNames()
Dim sPath As String, sFile As String
Dim iRow As Long, iCol As Long
Dim ws As Worksheet: Set ws = Sheet1
'declare and set the worksheet you are working with, amend as required

sPath = "Z:\NAME\Waiting\Non_Priority\"
'specify directory to use - must end in "\"

sFile = Dir(sPath)
Do While sFile <> ""
    LastRow = ws.Cells(ws.Rows.Count, "I").End(xlUp).Row 'get last row on Column I
    Filename = Left(sFile, InStrRev(sFile, ".") - 1) 'remove extension from file
    Set FoundFile = ws.Range("I1:I" & LastRow).Find(What:=Filename, Lookat:=xlWhole) 'search for existing filename
    If FoundFile Is Nothing Then ws.Cells(LastRow + 1, "I") = Filename 'if not found then add it
    sFile = Dir     ' Get next filename
Loop
End Sub
Sub-GetFileNames()
尺寸sPath为字符串,sFile为字符串
暗淡的iRow和长的一样,iCol和长的一样
将ws设置为工作表:设置ws=Sheet1
'声明并设置正在使用的工作表,根据需要进行修改
sPath=“Z:\NAME\Waiting\Non\u Priority\”
'指定要使用的目录-必须以“\”结尾
sFile=Dir(sPath)
在sFile“”时执行此操作
LastRow=ws.Cells(ws.Rows.Count,“I”).End(xlUp).Row'获取列I上的最后一行
Filename=Left(sFile,InStrRev(sFile,“.”-1)”从文件中删除扩展名
设置FoundFile=ws.Range(“I1:I”&LastRow).Find(What:=Filename,Lookat:=xlother)搜索现有文件名
如果FoundFile为Nothing,则ws.Cells(LastRow+1,“I”)=Filename'如果未找到,则添加它
sFile=Dir'获取下一个文件名
环
端接头

VBA在工作簿关闭时无法运行,因为VBA在工作簿中。为此,您可能想看看VBScript,它的工作原理类似于一个独立的程序;但是是用VBA类代码编程的如果我想包括文件上次保存的日期和上次由哪个用户保存的日期,我将如何包括这个?