将从文件收集的数据数组输出到主工作簿中的特定工作表

时间:2016-01-24 15:12:11

标签: excel excel-vba vba

以下代码一次打开一个选定的文件;如果文件包含ArrCopy中的特定文本字符串(有四种变体:LS2A,LS1PRA,LS1A和LSM12),则将每个文件的Sheet(1)中的指定数据复制到一个数组中。搜索由在主程序中调用的函数“SearchFor”执行。

数组B11填充了每个文件的数据,并应输出到主工作簿(SABI,SABII,LSM或LPRI& II)中的四个工作表之一。输出表由每个文件的 Option Explicit Function SearchFor(output As Worksheet) Dim rowsCount As Long Dim NCBead1 As Long, NCBead2 As Long, PCBead1 As Long, PCBead2 As Long Dim IniString As String, IniVar As String Dim rngCell As Range, rngCell2 As Range Dim ArrCopy(1 To 9) As Variant Dim LastRow As Long Dim aCell As Range LastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row 'extract initial after last underscore IniString = ActiveWorkbook.Sheets(1).Range("B6").Value IniVar = Right(IniString, Len(IniString) - InStrRev(IniString, "_", , 1)) Debug.Print IniVar 'Debug.Print "LastRow = " & LastRow Set aCell = ActiveSheet.Range("B1:B" & LastRow).Find(What:="Trimmed Mean", LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) 'Debug.Print "Trimmed Mean can be found in Row # " & aCell.Row 'wb.Sheets(1).Select For Each rngCell In ActiveSheet.Range("B" & aCell.Row & ":B" & LastRow) If InStr(rngCell, "NC") > 0 Then Debug.Print rngCell.Row NCBead1 = rngCell.Offset(0, 1).Value NCBead2 = rngCell.Offset(0, 2).Value 'End If Exit For End If Next rngCell For Each rngCell2 In ActiveSheet.Range("B" & aCell.Row & ":B" & LastRow) If InStr(rngCell2, "PC") > 0 Then Debug.Print rngCell2.Row PCBead1 = rngCell2.Offset(0, 1).Value PCBead2 = rngCell2.Offset(0, 2).Value 'End If Exit For End If Next rngCell2 'Next searched Debug.Print NCBead2 ArrCopy(1) = ActiveSheet.Range("B3").Value ArrCopy(2) = IniVar ArrCopy(3) = NCBead1 ArrCopy(4) = NCBead2 ArrCopy(5) = PCBead1 ArrCopy(6) = PCBead2 ArrCopy(7) = ActiveSheet.Range("B6").Value ArrCopy(8) = NCBead1 ArrCopy(9) = NCBead1 ' one row spanning several columns Debug.Print "ArrCopy" & ArrCopy(1) Debug.Print "ArrCopy" & ArrCopy(2) Debug.Print "ArrCopy" & ArrCopy(3) Dim Destination As Range Set Destination = output.Range("A" & output.Range("A" & Rows.Count).End(xlUp).Row + 1) Set Destination = Destination.Resize(1, UBound(ArrCopy)) Destination.Value = ArrCopy End Function Sub openselectedfiles() Dim SaveDriveDir As String, MyPath As String, FnameInLoop As String Dim mybook As Workbook, thisWb As Workbook Dim N As Long, LstUnderSc As Long, ExtPer As Long, Varin As Long Dim Fname As Variant, ArrCopy(1 To 9) As Variant Dim output As Worksheet Dim inLS2A As Boolean, inLS1PRA As Boolean, inLS1A As Boolean, inLSM12 As Boolean Set thisWb = ThisWorkbook ' Save the current directory. SaveDriveDir = CurDir ' Set the path to the folder that you want to open. MyPath = Application.DefaultFilePath ' Change drive/directory to MyPath. ChDrive MyPath ChDir MyPath ' Open GetOpenFilename with the file filters. Fname = Application.GetOpenFilename( _ FileFilter:="CSV Files (*.csv),*.csv", _ Title:="Select a file or files", _ MultiSelect:=True) ' Perform some action with the files you selected. If IsArray(Fname) Then With Application .ScreenUpdating = False .EnableEvents = False End With For N = LBound(Fname) To UBound(Fname) ' Get only the file name and test to see if it is open. FnameInLoop = Right(Fname(N), Len(Fname(N)) - InStrRev(Fname(N), Application.PathSeparator, , 1)) If bIsBookOpen(FnameInLoop) = False Then Set mybook = Nothing On Error Resume Next Set mybook = Workbooks.Open(Fname(N)) On Error GoTo 0 If Not mybook Is Nothing Then mybook.Sheets(1).Select With ActiveSheet.Range("B11") inLS2A = InStr(1, .Value, "LS2A", 1) > 0 inLS1PRA = InStr(1, .Value, "LS1PRA", 1) > 0 inLS1A = InStr(1, .Value, "LS1A", 1) > 0 inLSM12 = InStr(1, .Value, "LSM12", 1) > 0 End With If inLS2A Then Set output = thisWb.Sheets("SABII") SearchFor output ElseIf inLS1PRA Then Set output = thisWb.Sheets("LPRI&II") SearchFor output ElseIf inLS1A Then Set output = thisWb.Sheets("sabI") SearchFor output ElseIf inLSM12 Then Set output = thisWb.Sheets("LSM") SearchFor output End If 'End If mybook.Close SaveChanges:=False Set mybook = Nothing End If Else MsgBox "We skipped this file : " & Fname(N) & " because it is already open." End If Next N With Application .ScreenUpdating = True .EnableEvents = True End With End If ' Change drive/directory back to SaveDriveDir. ChDrive SaveDriveDir ChDir SaveDriveDir End Sub Function bIsBookOpen(ByRef szBookName As String) As Boolean ' Contributed by Rob Bovey On Error Resume Next bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing) End Function 中的文本字符串确定。

由于某种原因,我无法将数据输出到Master工作簿。我已经尝试了Debug.Print每个数组项目填充后,我可以看到数组中填充了正确的数据,但我无法将值转移到主工作簿。代码运行但工作表上没有输出任何内容。

请建议如何完成这项工作。感谢

/admin/login

0 个答案:

没有答案