将数据从一个月份的文件夹复制到一个工作表中(不打开和关闭工作簿)。

huangapple go评论130阅读模式
英文:

Copy data from a monthly folder into a sheet (without opening and closing workbooks)

问题

以下是您提供的VBA代码的翻译部分:

Sub getlatestfilename()
    Dim F As String, folder As String, currentyear As Integer, currentmonth As String, foldername As String, myfile As String
    Dim LatestFile As String, filetoopen As String
    Dim LatestDate As Date
    Dim LMD As Date
    Dim LR As Long
    Dim datawb As Workbook, thiswb As Workbook, ws As Worksheet
    
    ' 一旦确保它可以运行,请取消下面的注释
    Application.ScreenUpdating = False
    
    Set thiswb = ActiveWorkbook
    
    currentyear = Year(Date)
    currentmonth = Format(Month(Date), "00")
    
    folder = "K:\Finance\Protected Funding Sheets\Barclays cash funding\Daily Funding Calculation\" & currentyear '& "\"
    
    F = Dir(folder & "\*", vbDirectory)
    Do While F <> ""
        If InStr(F, currentmonth) > 0 Then
            foldername = F
            'Debug.Print foldername
            folder = folder & "\" & foldername & "\"
            Exit Do
        End If
        F = Dir
    Loop
    
    ' 检查是否找到了月份文件夹
    If F = "" Then
        MsgBox "未找到 " & currentmonth & " 月份文件夹..... ", vbExclamation
        Exit Sub
    End If
    'Debug.Print folder
    
    ' 确保路径以反斜杠结尾
    If Right(folder, 1) <> "\" Then folder = folder & "\"
    
    ' 从文件夹中获取第一个Excel文件
    myfile = Dir(folder & "*.xlsx", vbNormal)
    
    ' 如果没有找到文件,退出子程序
    If Len(myfile) = 0 Then
        MsgBox "未找到文件...", vbExclamation
        Exit Sub
    End If
    
    ' 遍历文件夹中的每个Excel文件
    Do While Len(myfile) > 0
        
        ' 将当前文件的日期/时间分配给一个变量
        LMD = FileDateTime(folder & myfile)
        
        ' 如果当前文件的日期/时间大于最新记录的日期,将其文件名和日期/时间分配给变量
        If LMD > LatestDate Then
            LatestFile = myfile
            LatestDate = LMD
        End If
        
        ' 获取文件夹中的下一个Excel文件
        myfile = Dir
        
    Loop
    
    'Debug.Print LatestFile, LatestDate,
    
    filetoopen = folder & LatestFile
    
    'Debug.Print filetoopen
    Set datawb = Workbooks.Open(filetoopen, Password:="barclays")
    
    ' 选择正确的工作表
    ' 将工作表名称更改为文件中使用的名称
    ' datawb.Sheets("sheetname").Activate
    
    With datawb.Sheets("Journal")
        date1 = .Range("B1")
        date2 = .Range("C1")
        bca = .Range("C16")
        bcabs41 = .Range("H19")
        bcabs42 = .Range("H20")
        csh = .Range("K15")
        cshbs42 = .Range("O15")
        cshbs43 = .Range("O16")
        cshbs432 = .Range("O18")
        cshbs44 = .Range("O19")
        ' 添加其他所需的单元格
    End With
    
    datawb.Close savechanges:=False
    Set ws = thiswb.Sheets("Postings")
    ws.Activate
    
    ' 用于理解 LR = 最后一行
    ' 将数据变量添加到最后一行 + 1
    With ws
        LR = .Cells(Rows.Count, 1).End(xlUp).Row
        ' 添加保存的变量
        .Cells(LR + 1, 1) = date1
        .Cells(LR + 1, 2) = date2
        .Cells(LR + 1, 3) = bca
        .Cells(LR + 1, 4) = bcabs41
        .Cells(LR + 1, 5) = bcabs42
        .Cells(LR + 1, 6) = csh
        .Cells(LR + 1, 7) = cshbs42
        .Cells(LR + 1, 8) = cshbs43
        .Cells(LR + 1, 9) = cshbs432
        .Cells(LR + 1, 10) = cshbs44
        ' 添加其他所需的单元格
    End With
    
    Application.ScreenUpdating = True
End Sub

希望这对您有所帮助!如果您有任何其他问题,请随时提问。

英文:

I have a monthly journal I complete each month where I copy data from a daily workbook to another sheet.

I have code for the current day. It opens and closes the current day's sheet.

I would like to not open and close the file and also do a month's worth of data instead, so it takes all the data from all the sheets in that month's folder.

Sub getlatestfilename()
    Dim F As String, folder As String, currentyear As Integer, currentmonth As String, foldername As String, myfile As String
    Dim LatestFile As String, filetoopen As String
    Dim LatestDate As Date
    Dim LMD As Date
    Dim LR As Long
    Dim datawb As Workbook, thiswb As Workbook, ws As Worksheet
    
    &#39; uncomment below once happy it runs
    Application.ScreenUpdating = False
  
    Set thiswb = ActiveWorkbook
  
    currentyear = Year(Date)
    currentmonth = Format(Month(Date), &quot;00&quot;)
  
    folder = &quot;K:\Finance\Protected Funding Sheets\Barclays cash funding\Daily Funding Calculation\&quot; &amp; currentyear &#39;&amp; &quot;\&quot;
  
    F = Dir(folder &amp; &quot;\*&quot;, vbDirectory)
    Do While F &lt;&gt; &quot;&quot;
        If InStr(F, currentmonth) &gt; 0 Then
            foldername = F
            &#39;Debug.Print foldername
            folder = folder &amp; &quot;\&quot; &amp; foldername &amp; &quot;\&quot;
            Exit Do
        End If
        F = Dir
    Loop
  
    &#39; check the month folder has been found
    If F = &quot;&quot; Then
        MsgBox &quot;No &quot; &amp; currentmonth &amp; &quot; folder found..... &quot;, vbExclamation
        Exit Sub
    End If
    &#39;Debug.Print folder
    
    &#39;Make sure that the path ends in a backslash
    If Right(folder, 1) &lt;&gt; &quot;\&quot; Then folder = folder &amp; &quot;\&quot;
    
    &#39;Get the first Excel file from the folder
    myfile = Dir(folder &amp; &quot;*.xlsx&quot;, vbNormal)
    
    &#39;If no files were found, exit the sub
    If Len(myfile) = 0 Then
        MsgBox &quot;No files were found...&quot;, vbExclamation
        Exit Sub
    End If
    
    &#39;Loop through each Excel file in the folder
    Do While Len(myfile) &gt; 0
    
        &#39;Assign the date/time of the current file to a variable
        LMD = FileDateTime(folder &amp; myfile)
        
        &#39;If the date/time of the current file is greater than the latest
        &#39;recorded date, assign its filename and date/time to variables
        If LMD &gt; LatestDate Then
            LatestFile = myfile
            LatestDate = LMD
        End If
        
        &#39;Get the next Excel file from the folder
        myfile = Dir
        
    Loop
    
    &#39;Debug.Print LatestFile, LatestDate,
    
    filetoopen = folder &amp; LatestFile
    
    &#39;Debug.Print filetoopen
    Set datawb = Workbooks.Open(filetoopen, Password:=&quot;barclays&quot;)
    
    &#39;select the correct sheet
    &#39;change sheetname to what is used in the file
    &#39;datawb.Sheets(&quot;sheetname&quot;).Activate
    
    With datawb.Sheets(&quot;Journal&quot;)
        date1 = .Range(&quot;B1&quot;)
        date2 = .Range(&quot;C1&quot;)
        bca = .Range(&quot;C16&quot;)
        bcabs41 = .Range(&quot;H19&quot;)
        bcabs42 = .Range(&quot;H20&quot;)
        csh = .Range(&quot;K15&quot;)
        cshbs42 = .Range(&quot;O15&quot;)
        cshbs43 = .Range(&quot;O16&quot;)
        cshbs432 = .Range(&quot;O18&quot;)
        cshbs44 = .Range(&quot;O19&quot;)
        &#39;add the other required cells
    End With
    
    datawb.Close savechanges = False
    Set ws = thiswb.Sheets(&quot;Postings&quot;)
    ws.Activate
    
    &#39;For understanding LR = Last Row
    &#39;add variables data to the last row + 1
     With ws
         LR = .Cells(Rows.Count, 1).End(xlUp).Row
        &#39;add the saved variables
        .Cells(LR + 1, 1) = date1
        .Cells(LR + 1, 2) = date2
        .Cells(LR + 1, 3) = bca
        .Cells(LR + 1, 4) = bcabs41
        .Cells(LR + 1, 5) = bcabs42
        .Cells(LR + 1, 6) = csh
        .Cells(LR + 1, 7) = cshbs42
        .Cells(LR + 1, 8) = cshbs43
        .Cells(LR + 1, 9) = cshbs432
        .Cells(LR + 1, 10) = cshbs44
    
        &#39;add the other required cells
    End With
    
    Application.ScreenUpdating = True
End Sub    

答案1

得分: 1

您已经有一个循环遍历文件夹中所有文件的代码部分:

'循环遍历文件夹中的每个Excel文件
Do While Len(myfile) > 0
   ...    
Loop

不仅在此循环内分配LatestFile,还将所有复制/粘贴活动放在此循环内。

英文:

You already have a section of code that loops through all files in the folder:

&#39;Loop through each Excel file in the folder
Do While Len(myfile) &gt; 0
   ...    
Loop

Instead of just assigning the LatestFile inside this loop, put all of your copy / paste activity inside this loop.

 Sub getlatestfilename()
  Dim F As String, folder As String, currentyear As Integer, currentmonth As String, foldername As String, myfile As String
  Dim LatestFile As String, filetoopen As String
  Dim LatestDate As Date
  Dim LMD As Date
  Dim LR As Long
  Dim datawb As Workbook, thiswb As Workbook, ws As Worksheet
  
  &#39; uncomment below once happy it runs
  Application.ScreenUpdating = False
  
  
  Set thiswb = ActiveWorkbook
  
  currentyear = Year(Date)
  currentmonth = Format(Month(Date), &quot;00&quot;)
  
  
  
  folder = &quot;K:\Finance\Protected Funding Sheets\Barclays cash funding\Daily Funding Calculation\&quot; &amp; currentyear &#39;&amp; &quot;\&quot;
  
  F = Dir(folder &amp; &quot;\*&quot;, vbDirectory)
  Do While F &lt;&gt; &quot;&quot;
    If InStr(F, currentmonth) &gt; 0 Then
        foldername = F
        &#39;Debug.Print foldername
        folder = folder &amp; &quot;\&quot; &amp; foldername &amp; &quot;\&quot;
        Exit Do
    End If
    F = Dir
  Loop
  
  &#39; check the month folder has been found
  If F = &quot;&quot; Then
    MsgBox &quot;No &quot; &amp; currentmonth &amp; &quot; folder found..... &quot;, vbExclamation
    Exit Sub
  End If
  &#39;Debug.Print folder
      
    
    &#39;Make sure that the path ends in a backslash
    If Right(folder, 1) &lt;&gt; &quot;\&quot; Then folder = folder &amp; &quot;\&quot;
    
    &#39;Get the first Excel file from the folder
    myfile = Dir(folder &amp; &quot;*.xlsx&quot;, vbNormal)
    
    &#39;If no files were found, exit the sub
    If Len(myfile) = 0 Then
        MsgBox &quot;No files were found...&quot;, vbExclamation
        Exit Sub
    End If
    
    &#39;Loop through each Excel file in the folder
    Do While Len(myfile) &gt; 0
    

        filetoopen = folder &amp; myfile
        
        &#39;Debug.Print filetoopen
        Set datawb = Workbooks.Open(filetoopen, Password:=&quot;barclays&quot;)
        
        &#39;select the correct sheet
        &#39;change sheetname to what is used in the file
        &#39;datawb.Sheets(&quot;sheetname&quot;).Activate
        
        With datawb.Sheets(&quot;Journal&quot;)
        date1 = .Range(&quot;B1&quot;)
        date2 = .Range(&quot;C1&quot;)
        bca = .Range(&quot;C16&quot;)
        bcabs41 = .Range(&quot;H19&quot;)
        bcabs42 = .Range(&quot;H20&quot;)
        csh = .Range(&quot;K15&quot;)
        cshbs42 = .Range(&quot;O15&quot;)
        cshbs43 = .Range(&quot;O16&quot;)
        cshbs432 = .Range(&quot;O18&quot;)
        cshbs44 = .Range(&quot;O19&quot;)
        &#39;add the other required cells
        End With
        
        
        datawb.Close savechanges = False
        Set ws = thiswb.Sheets(&quot;Postings&quot;)
        ws.Activate
        
        &#39;For understanding LR = Last Row
        &#39;add variables data to the last row + 1
         With ws
         LR = .Cells(Rows.Count, 1).End(xlUp).Row
        &#39;add the saved variables
        .Cells(LR + 1, 1) = date1
        .Cells(LR + 1, 2) = date2
        .Cells(LR + 1, 3) = bca
        .Cells(LR + 1, 4) = bcabs41
        .Cells(LR + 1, 5) = bcabs42
        .Cells(LR + 1, 6) = csh
        .Cells(LR + 1, 7) = cshbs42
        .Cells(LR + 1, 8) = cshbs43
        .Cells(LR + 1, 9) = cshbs432
        .Cells(LR + 1, 10) = cshbs44
        
        
        &#39;add the other required cells
        End With
        
        &#39;Get the next Excel file from the folder
        myfile = Dir
        
    Loop
    
    
    
    Application.ScreenUpdating = True
End Sub

huangapple
  • 本文由 发表于 2023年6月19日 22:15:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定