[英]VBA Coding to pull data
I have 50 .xls files saved on a shared drive by the name of users. 我用用户名在共享驱动器上保存了50个.xls文件。 Eg: "Rahul Goswami.xls", "Rohit Sharma.xls", etc.
例如:“ Rahul Goswami.xls”,“ Rohit Sharma.xls”等
Each Excel file contains 2 worksheets: "Case Tracker" and "Pending Tracker". 每个Excel文件包含2个工作表:“ Case Tracker”和“ Pending Tracker”。
In the "Case Tracker" worksheet users put their daily data/ daily production. 用户在“ Case Tracker”工作表中输入每日数据/每日产量。
I wanted VBA code to pull the entire "Case Tracker" worksheet from all 50 Excel files in one separate Excel workbook, one below the other. 我希望VBA代码从一个单独的Excel工作簿中的所有50个Excel文件中提取整个“案例跟踪器”工作表,一个在另一个工作簿下。
Currently I am copy-pasting the data from the Excel files to the master workbook to "Sheet1". 目前,我正在将数据从Excel文件复制粘贴到主工作簿中,然后粘贴到“ Sheet1”。
Can there be something where I put the date and the data will come automatically for that date from all the 50 files? 我可以在其中放置日期,并且所有50个文件中的该日期的数据都会自动出现吗?
Column A to J contains the data provided below. A到J列包含下面提供的数据。 This example is given for 1 user.
该示例仅针对1位用户。
Date Advisor Userid BP URN Stage Case Type Previous Status Current status Category 10-Apr Rahul Goswami goswami 123456 98765431 1 URN New Pend abc
Sub Beachson()
Dim z As Long, e As Long, d As Long, G As Long, h As Long Dim f As String
d = 2
Cells(1, 1) = "=cell(""filename"")"
Cells(1, 2) = "=left(A1,find(""["",A1)-1)"
Cells(2, 1).Select
f = Dir(Cells(1, 2) & "*.xls")
Do While Len(f) > 0
## Heading ##
ActiveCell.Formula = f
ActiveCell.Offset(1, 0).Select
f = Dir()
Loop
z = Cells(Rows.Count, 1).End(xlUp).Row
For e = 2 To z
If Cells(e, 1) <> ActiveWorkbook.Name Then
Cells(d, 2) = Cells(e, 1)
Cells(1, 4) = "=Counta('" & Cells(1, 2) & "[" & Cells(e, 1) & "]Case Tracker'!I:I)"
For h = 10 To Cells(1, 4)
For G = 1 To 10
Cells(1, 3) = "='" & Cells(1, 2) & "[" & Cells(e, 1) & "]Case Tracker'!" & Chr(G + 64) & h
Cells(d, G + 2) = Cells(1, 3)
Next G
d = d + 1
Next h
End If
d = d + 1
Next e
MsgBox "collating is complete."
End Sub
I would avoid storing information in sheet, then going to VBA, then again to sheet, etc. 我会避免将信息存储在表格中,然后再转到VBA,再到表格中,依此类推。
As for your problem of not being able to pull data when a file is open, I would suggest creating another instance of Excel.Application
and opening files from it in ReadOnly
mode. 至于打开文件时无法拉数据的问题,我建议创建另一个
Excel.Application
并以ReadOnly
模式从中打开文件。
This is the code which worked for me (the ability to find particular dates is also implemented): 这是对我有用的代码(还可以找到特定日期):
Sub Beachson2()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
Dim App As Object
Set App = CreateObject("Excel.Application")
Dim wsSource As Worksheet
Dim sFold As String
sFold = ThisWorkbook.Path & "\"
Dim sFile As String
Dim i As Long, j As Long
Dim cell As Range
' Setting date
Dim sInput As String, dInput As Date
sInput = Application.InputBox("Enter A Date")
If IsDate(sInput) Then
dInput = DateValue(sInput)
Else
MsgBox "Invalid date. Exiting..."
Exit Sub
End If
Application.ScreenUpdating = False
' Pulling data
i = 1
sFile = Dir(sFold & "\*.xls")
Do While sFile <> ""
If sFile <> sFold & ThisWorkbook.Name Then
Set wsSource = App.Workbooks.Open(Filename:=sFold & sFile, ReadOnly:=True).Sheets("Case Tracker")
For Each cell In wsSource.Range("A1:A" & wsSource.UsedRange.Rows.Count)
If cell.Value = CStr(dInput) Then
With ws.Cells(Rows.Count, 1).End(xlUp)
If IsEmpty(.Value2) Then
.Value2 = sFile
ElseIf .Value2 <> sFile Then
.Offset(1).Value2 = sFile
Else
'do nothing
End If
End With
If ws.Cells(Rows.Count, 2).End(xlUp).Value2 <> sFile Then
ws.Cells(i, 2).Value2 = sFile
End If
For j = 3 To 12
ws.Cells(i, j).Value = wsSource.Cells(cell.Row, j - 2).Value
Next
i = i + 1
End If
Next
wsSource.Parent.Close
End If
sFile = Dir()
Loop
Application.ScreenUpdating = True
App.Quit
MsgBox "collating is complete."
Set App = Nothing
End Sub
The code is stored in the master file. 该代码存储在主文件中。
Even in the code there is no one specific Date format defined, but I still think it is capable of causing problems. 即使在代码中也没有定义任何一种特定的Date格式,但是我仍然认为它能够引起问题。 If you find problems regarding date formats, please post your used date format.
如果您发现有关日期格式的问题,请发布您使用的日期格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.