因此,我仍在努力从以前询问过的同一张纸中提取和分析数据,但有人要求我重新评估我的方法。
请参考此处,了解我在做什么:Setting Excel Worksheet to a Variable, and calling the variable in a different sub
现在,我必须找到可以在我的列中找到的最小值,然后复制出现在该列之后的该列中的所有数据。
这是我当前拥有的代码:
Public Path As String
Public Counter As Integer
Public NameFile As Workbook
Public Celltxt As String 'Checks cell value in D2, used to compare to Strings to confirm part type
Public MyFolder As String 'Path collected from the folder picker dialog
Public MyFile As String 'Filename obtained by DIR function
Public wbk As Workbook 'Used to loop through each workbook
Public thisWb As Workbook
Public MasterFile As String
Public Min As Variant
Sub Consolidate_Diagramms_Data()
Dim wb As Workbook
Dim TestStr As String
TestStr = ""
TestStr = Dir("C:\DataAnalyzation\Consolidated Diagramm Data.xlsx")
Application.DisplayAlerts = False
If TestStr = "" Then
Set NameFile = Workbooks.Add
NameFile.SaveAs Filename:="C:\DataAnalyzation\Consolidated Diagramm Data.xlsx"
Range("A1").Value = "Part Number"
Range("B1").Value = "Date"
Range("C1").Value = "Time"
Range("D1").Value = "Part Type"
Range("E1").Value = "Comment"
Range("F1").Value = "Zero"
Else
Workbooks.Open Filename:="C:\DataAnalyzation\Consolidated Diagramm Data.xlsx"
Range("A1").Value = "Part Number"
Range("B1").Value = "Date"
Range("C1").Value = "Time"
Range("D1").Value = "Part Type"
Range("E1").Value = "Comment"
Range("F1").Value = "Zero"
End If
MasterFile = "C:\DataAnalyzation\Consolidated Diagramm Data.xlsx"
Call AllWorkbooks
End Sub
Sub AllWorkbooks()
Dim LastRow As Long
Dim minRange As Variant
Set thisWb = ActiveWorkbook
'On Error Resume Next
Application.ScreenUpdating = False 'Opens the folder picker dialog to allow user selection
MsgBox "Please select the folder from which you wish to consolidate your data."
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
Counter = 0
LHCounter = 0
RHCounter = 0
FeedshaftCounter = 0
MyFile = Dir(MyFolder) 'DIR gets the first file of the folder
'Loop through all files in a folder until DIR cannot find anymore
Do While MyFile <> ""
LastRow = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
Counter = Counter + 1
Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)
Application.ScreenUpdating = False
EventState = Application.EnableEvents
Application.EnableEvents = False
CalcState = Application.Calculation
Application.Calculation = xlCalculationManual
PageBreakState = ActiveSheet.DisplayPageBreaks
ActiveSheet.DisplayPageBreaks = False
'Copy Part Number, Date, Time, Part Type, and Comment
Workbooks(MyFile).Activate 'Activates the Data Sheet
If Range("B1").Value = "" Then
GoTo Nd
End If
ActiveSheet.Range("A2:E2").Copy 'Copies the Part Number, Date, Time and Part Type
'Paste Part Number, Date, Time, Part Type, and Comment
Workbooks("Consolidated Diagramm Data.xlsx").Activate 'Activates the final Workbook
Range("A" & LastRow).PasteSpecial Paste:=xlPasteAll 'Pastes the Date into "A2"
Application.CutCopyMode = False
'Copy Force
Workbooks(MyFile).Activate
Range("D4").Activate
minRange = Application.WorksheetFunction.Min(Sheets(1).Range("D4:D500"))
minRange.Activate
ActiveCell.End(xlDown).Copy
Workbooks("Consolidated Diagramm Data.xlsx").Activate
Range("F" & LastRow).Activate
ActiveCell.PasteSpecial Paste:=xlPasteAll, Transpose:=True
Application.CutCopyMode = False
GoTo Nd
'End of Copy/Paste coding
Nd:
wbk.Close savechanges:=False
MyFile = Dir 'DIR gets the next file in the folder
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox ("A total of " & Counter & " files have been consolidated.")
End Sub
我遇到的问题是我似乎无法找到最小值,激活该单元格并复制其后该列中的所有数据。
在线:
minRange = Application.WorksheetFunction.Min(Sheets(1).Range("D4:D500"))
minRange.Activate
我遇到“运行时错误'424':必需对象”错误。
下面是我正在搜索最小值的代码的更新部分。我当前正在获得“运行时1004:无法获取WorksheetFunction类的Match属性”。
Workbooks(MyFile).Activate
Range("D4").Activate
Set myRng = Range("D4:D" & Rows.Count)
minValue = Application.WorksheetFunction.Min(myRng)
myRow = Application.WorksheetFunction.Match(minValue, myRng, 0)
Range(myRow, myRng).Activate
ActiveCell.End(xlDown).Copy
Workbooks("Consolidated Diagramm Data.xlsx").Activate
Range("F" & LastRow).Activate
ActiveCell.PasteSpecial Paste:=xlPasteAll, Transpose:=True
Application.CutCopyMode = False
GoTo Nd
答案 0 :(得分:1)
以下内容将为您提供最小值的行(假设只有一行):
Dim myRng As Range
Dim myRow as Long
Dim minValue as Long
Set myRng = Range("A1:A" & Rows.Count)
minValue = Application.WorksheetFunction.Min(myRng)
MyRow = Application.WorksheetFunction.Match(minValue, myRng, 0)