【问题标题】:Add information to an Excel file将信息添加到 Excel 文件
【发布时间】:2021-02-23 09:43:29
【问题描述】:

我制作了一个 VBA 宏,允许我从文件夹中的特定文件中获取信息。

名字的格式是Name_Timestamp.extension 我得到了时间戳。

我需要做的:将column G 中的名称文件、column B 中的时间戳和column J 中的一个单词添加到带有标题的 Excel 中。

你对我该怎么做有什么想法吗?

     
    Dim Chemin As String, Fichier As String, timeStamp As String
     
    'Définit le répertoire contenant les fichiers
    Chemin = "PATH"
    Fichier = Dir(Chemin & "*.*")
    timeStamp = Split(Fichier, "_")(2)
    timeStamp = Split(timeStamp, ".")(0)


    Do While Len(Fichier) > 0
        
        MsgBox (Fichier & "___" & timeStamp)
        Fichier = Dir
    Loop

End Sub

【问题讨论】:

  • 嗨,这真的很基础,只是谷歌一些初学者的 VBA 教程
  • G 列中的名称是否应该是文件名或带有或不带有时间戳的文件名,和/或带有或不带有文件扩展名? J 列中的单词是什么?请说清楚。

标签: excel vba file add


【解决方案1】:

按时间戳分割

  • 调整常量部分中的值。

守则

Option Explicit

Sub splitByTimeStamp()
     
    Const wsName As String = "Sheet1"
    Const FirstRow As Long = 2
    Const FolderPath As String = "C:\Test"
    Const aWord As String = "a word"
    Const TimeStampDelimiter As String = "_" ' don't use a dot ('.').
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
    
    Dim pSep As String: pSep = Application.PathSeparator
    Dim FileName As String: FileName = Dir(FolderPath & pSep & "*.*")
    Dim cRow As Long: cRow = FirstRow
    
    Dim sArr() As String
    Dim cString As String
    Dim NoStamp As String
    Dim TimeStamp As String
    Dim n As Long
    
    Application.ScreenUpdating = False
    Do While Len(FileName) > 0
        sArr = Split(FileName, TimeStampDelimiter)
        Select Case UBound(sArr)
        Case 0
            cString = sArr(0)
            NoStamp = Left(cString, InStrRev(cString, ".") - 1)
            TimeStamp = ""
        Case 1
            NoStamp = sArr(0)
            cString = sArr(1)
            TimeStamp = Left(cString, InStrRev(cString, ".") - 1)
        Case Else
            For n = 0 To UBound(sArr) - 1
                NoStamp = NoStamp & sArr(n) & TimeStampDelimiter
            Next n
            NoStamp = Left(NoStamp, Len(NoStamp) - Len(TimeStampDelimiter))
            cString = sArr(UBound(sArr))
            TimeStamp = Left(cString, InStrRev(cString, ".") - 1)
        End Select
        ws.Cells(cRow, "B").Value = TimeStamp
        ws.Cells(cRow, "G").Value = NoStamp
        ws.Cells(cRow, "J").Value = aWord
        cRow = cRow + 1
        FileName = Dir
    Loop
    Application.ScreenUpdating = True

    MsgBox "Files Found: " & cRow - FirstRow, vbInformation, "Success"

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2012-06-27
    • 2011-08-16
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多