如何複製含有這些列標題名稱「刀具刀具」和「保持器」的列(僅限數據)並將它們粘貼(作爲附加在一列中每個具有相同的列標題名稱)放入VBA代碼(Sheet Module)所在的另一個工作簿表單中。謝謝。 列標題HOLDER出現在F10中(最好寫爲(10,6),而TOOL CUTTER在G10(10,11)中,但最好讓它搜索標題名稱並打印該列中的任何內容,直到它是完全空的(可能會出現空格) 非常感謝任何幫助!!搜索列標題,複製列並粘貼到主工作簿
工作代碼:打開循環中的文件夾中的文件 - 打開文件,將文件的名稱打印到Masterfile表中,從文件打印項目J1到主文件表,關閉文件,文件夾中,直到所有已通過循環打開下一個文件。我正在
Option Explicit
Sub LoopThroughDirectory()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim MyFolder As String
Dim Sht As Worksheet, ws As Worksheet
Dim WB As Workbook
Dim i As Integer
Dim LastRow As Integer, erow As Integer
Dim Height As Integer
Application.ScreenUpdating = False
MyFolder = "C:\Users\trembos\Documents\TDS\progress\"
Set Sht = Workbooks("masterfile.xlsm").Sheets("Sheet1")
'create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'get the folder object
Set objFolder = objFSO.GetFolder(MyFolder)
i = 1
'loop through directory file and print names
For Each objFile In objFolder.Files
If LCase(Right(objFile.Name, 3)) = "xls" Or LCase(Left(Right(objFile.Name, 4), 3)) = "xls" Then
'print file name
Workbooks.Open Filename:=MyFolder & objFile.Name
Set WB = ActiveWorkbook
With WB
For Each ws In .Worksheets
Sht.Cells(i + 1, 1) = objFile.Name
With ws
.Range("J1").Copy Sht.Cells(i + 1, 4)
End With
i = i + 1
Next ws
.Close SaveChanges:=False
End With
End If
Next objFile
Application.ScreenUpdating = True
End Sub
代碼嘗試打印值在支架和TOO大號刀具支柱(返回錯誤工具變量不符合For Each Tool In TOOLList
在與評論開始塊定義「粘貼工具列表中找到回這片:
Option Explicit
Sub LoopThroughDirectory()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim MyFolder As String
Dim StartSht As Worksheet, ws As Worksheet
Dim WB As Workbook
Dim i As Integer
Dim LastRow As Integer, erow As Integer
Dim Height As Integer
'Application.ScreenUpdating = False
MyFolder = "C:\Users\trembos\Documents\TDS\progress\"
Set StartSht = ActiveSheet
'create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'get the folder object
Set objFolder = objFSO.GetFolder(MyFolder)
i = 2
'loop through directory file and print names
For Each objFile In objFolder.Files
If LCase(Right(objFile.Name, 3)) = "xls" Or LCase(Left(Right(objFile.Name, 4), 3)) = "xls" Then
'print file name
StartSht.Cells(i, 1) = objFile.Name
Dim NewWb As Workbook
Set NewWb = Workbooks.Open(Filename:=MyFolder & objFile.Name)
'print TDS values
With WB
For Each ws In .Worksheets
StartSht.Cells(i + 1, 1) = objFile.Name
With ws
.Range("J1").Copy StartSht.Cells(i + 1, 4)
End With
i = i + 1
Next ws
.Close SaveChanges:=False
End With
End If
'print CUTTING TOOL and HOLDER lists
Dim k As Long
Dim width As Long
Dim TOOLList As Object
Dim count As Long
Set TOOLList = CreateObject("Scripting.Dictionary")
Dim ToolRow As Integer 'set as As Long if more than 32767 rows
' search for all on other sheets
' Assuming header means Row 1
If objFile.Name <> "masterfile.xls" Then 'skip any processing on "Masterfile.xls"
For Each ws In NewWb.Worksheets 'assuming we want to look through the new workbook
With ws
width = .Cells(10, .Columns.count).End(xlToLeft).Column
For k = 1 To width
If Trim(.Cells(10, k).Value) = "CUTTING TOOL" Then
Height = .Cells(.Rows.count, k).End(xlUp).Row
If Height > 1 Then
For ToolRow = 2 To Height
If Not TOOLList.exists(.Cells(ToolRow, k).Value) Then
TOOLList.Add .Cells(ToolRow, k).Value, ""
End If
Next ToolRow
End If
End If
Next
End With
Next
End If
' paste the TOOL list found back to this sheet
With StartSht
width = .Cells(10, .Columns.count).End(xlToLeft).Column
For k = 1 To width
If Trim(.Cells(10, k).Value) = "CUTTING TOOL" Then
Height = .Cells(.Rows.count, k).End(xlUp).Row
count = 0
For Each Tool In TOOLList
count = count + 1
.Cells(Height + count, k).Value = Tool
Next
End If
Next
End With
'close current file, do not save changes
NewWb.Close SaveChanges:=False
i = i + 1
'move to next file
Next objFile
'Application.ScreenUpdating = True
End Sub
是包含所謂的 「masterfile.xlsm」 代碼的工作簿?從代碼中弄清楚有點困難。 –
@TimWilliams對不起,這是一個難以解釋的問題。隨意問很多問題!是的,包含代碼的工作簿稱爲「masterfile.xlsm」。我試圖從位於文件夾MyFolder =「C:\ Users \ trembos \ Documents \ TDS \ progress \」'' – Taylor
「中的文件向該」masterfile.xlsm「寫入信息。您正在使用顯式選項,沒有看到「工具」的Dim行。這就是爲什麼你會收到未定義的錯誤。 – thunderblaster