我正在运行一个VBA脚本,以便在选择的文件夹中计算每个文件的行数,然后在活动工作簿中显示它。
Option Explicit
Sub CountRows()
Dim wbSource As Workbook, wbDest As Workbook
Dim wsSource As Worksheet, wsDest As Worksheet
Dim strFolder As String, strFile As String
Dim lngNextRow As Long, lngRowCount As Long
Application.ScreenUpdating = False
Set wbDest = ActiveWorkbook
Set wsDest = wbDest.ActiveSheet
strFolder = Dir(Range("C7").Value)
strFile = Dir(strFolder & "*.xlsx")
lngNextRow = 11
Do While Len(strFile) > 0
Set wbSource = Workbooks.Open(Filename:=strFolder & strFile)
Set wsSource = wbSource.Worksheets(1)
lngRowCount = wsSource.UsedRange.Rows.Count
wsDest.Cells(lngNextRow, "F").Value = lngRowCount
wbSource.Close savechanges:=False
lngNextRow = lngNextRow + 1
strFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
选择一个文件夹,我想使用插入到活动工作簿单元格“C7”中的目录,而不是在脚本中编写目录。 我尝试进行替换:
strFolder = "C:\Users\user\Desktop\"
使用
strFolder = Dir(Range("C7").Value)
但它无法正常工作。也许有人有什么想法?谢谢!
Workbooks("wbName").Worksheets("shName").Range("C7")
- Dmitry PavlivstrFolder = Dir(Range("C7").Value)
这行代码之前,尝试添加MsgBox Range("C7").Value
。会出现什么提示信息? - Dmitry Pavliv