|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
请教各位学友,我想把许多文件的文件名填入到这个文件的地址列,做了一个VBA,不知道应该怎么改,请各位学友指教
'用文件名作地址名
Sub NametoAdrress() '文件名保存到地址
Dim bt As Range, r As Long, c As Long, myrow As Long
r = 1
c = 9
'Range(Cells(r + 3, "A"), Cells(65536, c)).ClearContents '清除汇总表中原表数据
Application.ScreenUpdating = False
Dim filename As String, wb As Workbook, Erow As Long, fn As String, arr As Variant, sht As Worksheet
filename = Dir(ThisWorkbook.Path & "\*.xls")
Do While filename <> ""
If filename <> ThisWorkbook.Name Then '判断文件是否本工作簿
fn = ThisWorkbook.Path & "\" & filename
Workbooks.Open (fn)
With ActiveWorkbook.Worksheets(1)
Set wb = GetObject(fn) '将fn 代表的工作簿对象赋给变量
Set sht = wb.Worksheets(1)
myrow = sht.Range("A1").CurrentRegion.Rows.Count - 2
For r = 1 To myrow
'Erow = Range("a1").CurrentRegion.Rows.Count + 1 '取得汇总表中第一条空行行号
'汇总的是第1张工作表
'将数据表中的记录保存在数组中
sht.Cells(r + 2, "L") = filename
'将数组中的数据写入工作表
'Cells(Erow, "A").Resize(UBound(arr, 1), UBound(arr, 2)) = arr '行、列
Next
wb.Close False
End With
'ActiveWorkbook.Close savechanges:=True
End If
filename = Dir '用dir函数取得其他文件名,并赋给变量
Loop
Application.ScreenUpdating = True
End Sub
|
|