【问题标题】:Making the column header the file name using VBA使用 VBA 使列标题成为文件名
【发布时间】:2018-06-07 13:38:58
【问题描述】:

我真的是 VBA 新手,所以这是我在网上找到并组合起来的一些代码。现在有 3 个部分(最后一部分对我的问题并不重要)。第一部分“编译”循环遍历文件夹中的所有文件,并调用第二部分“copydata”,它复制标题为“direction”或“instruction”的列下的数据并将其粘贴到新的工作表“Summary”中。现在,代码将数据粘贴到下一个空列中。如何更新我的代码,以便每次将数据放入新列时,将标题“方向”或“指令”替换为数据对应的文件名

Sub Compile()
Dim xsource As Workbook
Dim NewWS As Worksheet
Dim original As Worksheet
Dim FileNeeded As String
Dim xPath As String

'clear contents from previous sheet
Sheets("summary").Cells.ClearContents
' Initialize some variables and get the folder path that has the files
Set NewWS = ThisWorkbook.Sheets("summary")
xPath = GetPath
' Make sure a folder was picked.
If Not xPath = vbNullString Then

' Get all the files from the folder
FileNeeded = Dir$(xPath & "*.xlsm", vbNormal)
Do While Not FileNeeded = vbNullString

' Open the file and get the source sheet
    Set xsource = Workbooks.Open(xPath & FileNeeded)
    Set original = xsource.Sheets("sum")

    Call CopyData(original, NewWS)

    'Close the workbook and move to the next file.
    xsource.Close False
    FileNeeded = Dir$()

    Loop
End If
End Sub

Sub CopyData(original As Worksheet, NewWS As Worksheet)
Dim title As Range
Dim LastCol As Long

With original.Rows(1)
Set title = .Find("direction")
If title Is Nothing Then Set title = .Find("instruction")
End With

'Get last used column, and add 1 (for next one)
LastCol = NewWS.Cells(1, Columns.Count).End(xlToLeft).Column + 1

If Not title Is Nothing Then
    title.EntireColumn.Copy
    NewWS.Cells(1, LastCol).PasteSpecial (xlPasteValues)
    Application.CutCopyMode = xlCopy
    NewWS.Columns(LastCol).RemoveDuplicates Columns:=1, Header:=xlNo
  Else
    MsgBox "Error"
End If
End Sub

Function GetPath() As String
With Application.FileDialog(msoFileDialogFolderPicker)
    .ButtonName = "Select a folder"
    .title = "Folder Picker"
    .AllowMultiSelect = False
    If .Show Then GetPath = .SelectedItems(1) & "\"
    End With
End Function

【问题讨论】:

  • 将文件名传递给CopyData函数(这样你就会有Sub CopyData(original As Worksheet, NewWS As Worksheet, TheFileName as String)),在粘贴数据后添加对应的名称和NewWS.Cells(1, LastCol)=TheFileName
  • 我需要代码从用户选择的文件夹中的文件中提取数据(显示在最后一个函数 GetPath 中),所以我如何确保列标题是获取数据的文件的名称从?

标签: excel vba


【解决方案1】:

未测试

Set xsource = Workbooks.Open(xPath & FileNeeded)
Set original = xsource.Sheets("sum")
FileName= xsource.Name 'add this line

Call CopyData(original, NewWS, FileName) 'add the file name into the parameters of your sub

然后在你的潜艇中

 Sub CopyData(original As Worksheet, NewWS As Worksheet, TheFileName as String)

然后

(...)
NewWS.Columns(LastCol).RemoveDuplicates Columns:=1, Header:=xlNo 'this is your code, just to indicate where to add the next line
NewWS.Cells(1, LastCol)=TheFileName 'add this line

【讨论】:

  • 我收到 Call CopyData(original, NewWS, FileName) 行的 ByRef 不匹配错误
  • 在子编译中添加Dim FileName as String
  • 我尝试实施您的建议,但列中的第一行变为空白,并且仅将数据粘贴到列 b 中,因此所有数据都被覆盖
  • 我试过了,它对我有用......而且我的建议不应该让代码总是在 b 列中写入数据。其他地方发生了变化。
  • NewWS.Columns(LastCol).RemoveDuplicates Columns:=1, Header:=xlNo 替换为NewWS.Columns(LastCol).RemoveDuplicates Columns:=LastCol, Header:=xlYes。我没有测试,因为我的 excel 版本(史前 2003 年)不支持 RemoveDuplicates 功能......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-22
相关资源
最近更新 更多