[英]Search for specific column header, copy column and paste to another workbook
How can I copy the columns (data only) with these column header names "TOOL CUTTER" and "HOLDER" and paste them (as an append in just one column each with the same column header name) into another workbook sheet where the VBA code ( Sheet Module) is. 我如何复制具有这些列标题名称“ TOOL CUTTER”和“ HOLDER”的列(仅数据),并将它们(作为追加内容添加到同一列标题名称中的每个列中),粘贴到另一个工作簿工作表中,其中VBA代码(工作表模块)是。 Thanks. 谢谢。
The line "If Sht <> "masterfile.xls" Then
is where the problem occurs. I got help from another online source where this line was If ws.name <> me.name Then
clearly I was meant to put a different name here but I cannot figure out what. "If Sht <> "masterfile.xls" Then
这行是问题所在。我从另一在线来源获得帮助, If ws.name <> me.name Then
我显然应该在此处输入其他名称但我不知道是什么。
Does not need to be this method of solving, this is just what I currently have. 不需要是这种解决方法,这就是我目前所拥有的。
I am opening multiple files which is why I mostly use ActiveSheet methods not Sheet1 Sheet2. 我正在打开多个文件,这就是为什么我主要使用ActiveSheet方法而不是Sheet1 Sheet2的原因。 The file that my code is in is called "masterfile.xls" 我的代码所在的文件称为“ masterfile.xls”
Any help is greatly appreciated!! 任何帮助是极大的赞赏!!
Previous code outline help found here: Search for specific column header names, copy columns and paste to append to another wookbooksheet 在此处找到以前的代码概述帮助: 搜索特定的列标题名称,复制列并粘贴以追加到另一个wookbooksheet
Sub LoopThroughDirectory()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim MyFolder As String
Dim Sht As Worksheet
Dim i As Integer
Dim LastRow As Integer, erow As Integer
'Speed up process by not updating the screen
'Application.ScreenUpdating = False
MyFolder = "C:\Users\trembos\Documents\TDS\progress\"
Set Sht = ActiveSheet
'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" And LCase(Left(Right(objFile.Name, 4), 3)) <> "xls" Then
Else
'print file name
Sht.Cells(i + 1, 1) = objFile.Name
i = i + 1
Workbooks.Open fileName:=MyFolder & objFile.Name
End If
Dim k As Long
Dim width As Long
Dim ws As Worksheet
Dim TOOLList As Object
Dim count As Long
Set TOOLList = CreateObject("Scripting.Dictionary")
' search for all tel/number list on other sheets
' Assuming header means Row 1
For Each ws In Worksheets
If Sht <> "masterfile.xls" Then
With ActiveSheet
.Activate
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 j = 2 To Height
If Not TOOLList.exists(.Cells(j, k).Value) Then
TOOLList.Add .Cells(j, k).Value, ""
End If
Next j
End If
End If
Next
End With
End If
Next
' paste the TOOL list found back to this sheet
With masterfile.xls
.Activate
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
'Range("J1").Select
'Selection.Copy
'Windows("masterfile.xlsm").Activate
'Range("D2").Select
'ActiveSheet.Paste
ActiveWorkbook.Close SaveChanges:=False
Next objFile
'Application.ScreenUpdating = True
End Sub
sht
refers to the active worksheet in the workbook in which this code resides, because Set Sht = ActiveSheet
sht
引用此代码所在的工作簿中的活动工作表,因为Set Sht = ActiveSheet
sht
is an object variable, and will never be equal to the string value "masterfile.xls"
sht
是一个对象变量,永远不会等于字符串值"masterfile.xls"
sht.name
will give you the (string) name of the worksheet which you could compare to the string value "masterfile.xls"
, but that still won't tell you what you're after because: sht.name
将为您提供工作表的(字符串)名称,您可以将其与字符串值"masterfile.xls"
进行比较,但这仍然无法告诉您所要查找的内容,因为:
WorkSheet
( sht.name
) with the file name of the WorkBook
( masterfile.xls
). 你混淆的名称WorkSheet
( sht.name
)用的文件名WorkBook
( masterfile.xls
)。 If LCase(Right(objFile.Name, 3)) <> "xls" And Case(Left(Right(objFile.Name, 4), 3)) <> "xls" Then Else
is a very awkward construct. If LCase(Right(objFile.Name, 3)) <> "xls" And Case(Left(Right(objFile.Name, 4), 3)) <> "xls" Then Else
是一个非常尴尬的构造。 Change that to: 更改为:
If LCase(Right(objFile.Name, 3)) = "xls" or Case(Left(Right(objFile.Name, 4), 3)) = "xls" Then
and eliminate the else
clause. If LCase(Right(objFile.Name, 3)) = "xls" or Case(Left(Right(objFile.Name, 4), 3)) = "xls" Then
消除else
子句。 it will make it much more readable 它将使它更具可读性 I presume that If Sht <> "masterfile.xls" Then
is intended to skip the processing of the WorkBook masterfile.xls
if that's the case then: 我认为, If Sht <> "masterfile.xls" Then
, If Sht <> "masterfile.xls" Then
则打算跳过WorkBook masterfile.xls
的处理,然后:
If Sht.Cells(i, 1) <> "masterfile.xls" Then
should do the trick, since you stored the file name earlier in your code. If Sht.Cells(i, 1) <> "masterfile.xls" Then
就可以解决这个问题,因为您早先在代码中存储了文件名。 (Note: you immediately increment i
after using it, so you have to use one smaller value here.) (注意:使用i
后,您立即将i
递增,因此您必须在此处使用一个较小的值。) Workbooks.Open fileName:=MyFolder & objFile.Name
will open the new workbook, but makes it too easy to get confused on which workbook you're looking at. Workbooks.Open fileName:=MyFolder & objFile.Name
将打开新的工作簿,但是很容易混淆您正在查看的工作簿。 Try Set NewWb = Workbooks.Open fileName:=MyFolder & objFile.Name
, now you have a firm handle with which to reference this one. 尝试Set NewWb = Workbooks.Open fileName:=MyFolder & objFile.Name
,现在您有了一个牢固的句柄来引用此文件。 With ActiveSheet .Activate
is simply redundant. With ActiveSheet .Activate
只是多余的。 ActiveSheet
is the active sheet, there's no need to activate it. ActiveSheet
是活动工作表,无需激活它。 With masterfile.xls
is a totally non-functional statement. With masterfile.xls
是一个完全不起作用的语句。 With
is expecting some sort of collection object to work with, which masterfile.xls
is not. With
期望使用某种收集对象,而masterfile.xls
则不能。 It's not a string (no quotes), it's not a variable of any sort (never declared), it's not an object (masterfile) with a method or property (xls). 它不是字符串(不带引号),也不是任何类型的变量(从未声明),不是具有方法或属性(xls)的对象(主文件)。 This indicates that you don't have Option Explicit
set at the top of your code. 这表明您没有在代码顶部设置Option Explicit
。 You should always do this, as it will make this a compile-time error instead of a run-time error. 您应该始终这样做,因为这会使它成为编译时错误,而不是运行时错误。 ActiveWorkbook.Close SaveChanges:=False
would have closed the workbook you're running from, because you would have activated it. 如果上述工作过 , ActiveWorkbook.Close SaveChanges:=False
会关闭你正在运行的工作簿,因为你已经激活它。 Try this code, it's probably not 100%, it should at least get you closer to what you're after: 试试下面的代码,它可能不是100%,它至少应该使您更接近所追求的目标:
Option Explicit
Sub LoopThroughDirectory()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim MyFolder As String
Dim StartSht As Worksheet
Dim i As Integer
Dim LastRow As Integer, erow As Integer
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)
End If
Dim k As Long
Dim width As Long
Dim ws As Worksheet
Dim TOOLList As Object
Dim count As Long
Set TOOLList = CreateObject("Scripting.Dictionary")
' search for all tel/number list 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 j = 2 To Height
If Not TOOLList.exists(.Cells(j, k).Value) Then
TOOLList.Add .Cells(j, k).Value, ""
End If
Next j
End If
End If
Next
End With
Next
End If
' paste the TOOL list found back to this sheet
With StartSheet
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
NewWb.Close SaveChanges:=False
i = i + 1
Next objFile
'Application.ScreenUpdating = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.