[英]How to open certain excel folders in a file with a common name using vba
My problem is that I need to open some excel files using VBA (for excel 2007) and extract the data. 我的问题是我需要使用VBA打开某些excel文件(对于excel 2007)并提取数据。 All the files I want to open are called "profit for January.xlsx", "profit for February.xlsx", and so on with only the month name changing, so I think I want to open a file called "profit for*". 我要打开的所有文件都被称为“ January.xlsx的利润”,“ February.xlsx的利润”,依此类推,仅更改月份名称,所以我想打开一个名为“ profit for *”的文件。 。 There is another file in the folder called "total revenue.xlsx" that I do not want to open. 我不想打开文件夹中的另一个文件“ total income.xlsx”。
If possible, I need the code to extract the data from the files in the folder, wherever the folder may be because I am sending this code to my colleagues to put into their own folders, which have the same file name formats etc but different paths. 如果可能的话,我需要代码从文件夹中的文件夹中提取数据,无论文件夹是在什么地方,因为我将此代码发送给同事放入自己的文件夹中,这些文件夹具有相同的文件名格式等,但路径不同。
I have the code to extract the data, which works, but it either imports all the data in the folder or none at all! 我有提取数据的代码,这行得通,但是它要么导入文件夹中的所有数据,要么根本不导入任何数据!
Any help on this would be much appreciated as I am an intern trying to get his foot in the door, and this would be quite a big break for me! 在这方面的任何帮助将不胜感激,因为我是一名实习生,试图让他站起来,这对我来说是一个很大的突破!
Further Information 更多信息
So far I have the code below (I haven't included the dim's because I felt they may be unnecessary?), which I have drawn from other websites. 到目前为止,我有下面的代码(我没有包括暗淡的代码,因为我认为它们可能是不必要的吗?),我从其他网站获得了此代码。 I'm also finding that, in trying to open all the files in the folder, it is trying to open itself! 我还发现,在尝试打开文件夹中的所有文件时,它正在尝试自行打开! If anyone could suggest how to improve this, I would be very grateful. 如果有人可以提出改进建议,我将不胜感激。 I haven't been using VBA for very long, and have been finding this assignment quite tough! 我使用VBA已有很长时间了,并且发现这项任务非常艰巨!
The error box that comes up sometimes says that I need an 'object' for the variable sfilename, and I'm not sure how to do that without messing up another part of the code. 出现的错误框有时表示我需要变量sfilename的“对象”,而且我不确定如何在不弄乱代码另一部分的情况下执行此操作。
sub import data ()
ChDir ThisWorkbook.Path
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set sfolder = objFSO.GetFolder(ThisWorkbook.Path)
For Each sfilename In sfolder.Files
If sfilename <> "Total Revenue.xlsx" Then
Workbooks.Open Filename:= _
sfilename *'open the file*
Set sfilename = ActiveWorkbook *'set the file name as sfilename, so the single piece of code will work with the copy-loop*
b = Sheets.Count *'for the data-import loop*
Call ImportData *'call in the loop*
sfilename.Close *'close the file*
End If
Next
end sub
What are you using at the moment? 您现在在用什么? For each file in folder? 对于文件夹中的每个文件?
Possibilities include 可能包括
EDIT 编辑
Sub import_data()
sPath = ThisWorkbook.Path
sTemplate = "\profit for qqq.xls"
For i = 1 To 12
sFileName = Replace(sTemplate, "qqq", MonthName(i))
''Just checking
If Dir(sPath & sFileName) <> "" Then
Workbooks.Open Filename:= _
sPath & sFileName
'open the file*
Set sFileName = ActiveWorkbook
'set the file name as sfilename, so the single
'piece of code will work with the copy-loop*
b = Sheets.Count
'*'for the data-import loop*
''Call ImportData
'*'call in the loop*
sFileName.Close
'*'close the file*
End If
Next
End Sub
So far I have the code below (I haven't included the dim's because I felt they may be unnecessary?), which I have drawn from other websites. 到目前为止,我有下面的代码(我没有包括暗淡的代码,因为我认为它们可能是不必要的吗?),我从其他网站获得了此代码。 I'm also finding that, in trying to open all the files in the folder, it is trying to open itself! 我还发现,在尝试打开文件夹中的所有文件时,它正在尝试自行打开! If anyone could suggest how to improve this, I would be very grateful. 如果有人可以提出改进建议,我将不胜感激。 I haven't been using VBA for very long, and have been finding this assignment quite tough! 我使用VBA已有很长时间了,并且发现这项任务非常艰巨!
The error box that comes up sometimes says that I need an 'object' for the variable sfilename, and I'm not sure how to do that without messing up another part of the code. 出现的错误框有时表示我需要变量sfilename的“对象”,而且我不确定如何在不弄乱代码另一部分的情况下执行此操作。
Many thanks and kindest regards, Mark 马克,非常感谢和亲切的问候
sub import data () 子导入数据()
ChDir ThisWorkbook.Path ChDir ThisWorkbook.Path
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set sfolder = objFSO.GetFolder(ThisWorkbook.Path)
For Each sfilename In sfolder.Files
If sfilename <> "Total Revenue.xlsx" Then
Workbooks.Open Filename:= _
sfilename *'open the file*
Set sfilename = ActiveWorkbook *'set the file name as sfilename, so the single piece of code will work with the copy-loop*
b = Sheets.Count *'for the data-import loop*
Call ImportData *'call in the loop*
sfilename.Close *'close the file*
End If
Next
end sub 结束子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.