Como usar o caminho do arquivo de uma célula no VBA?
Estou executando um script VBA para contar o número de linhas em cada arquivo em uma pasta selecionada e exibi-lo em uma pasta de trabalho ativa.
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
Ao escolher uma pasta, eu gostaria de usar o diretório inserido em uma célula ativa do WorkBook "C7" em vez de escrever um diretório em um script. Eu tentei substituir:
strFolder = "C:\Users\user\Desktop\"
com
strFolder = Dir(Range("C7").Value)
mas não funciona. Talvez alguém tenha alguma ideia? Obrigado!