检查列中的最小值,并复制该最小值之后的所有数据

Checking for Minimum Value in column, and copying all the data after that minimum value

所以我仍在努力从我之前询问过的相同工作表中提取和分析数据,但我被要求重新评估我的方法。

请参阅此处了解我正在做的事情的一些背景信息: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

我遇到 "Runtime Error '424': Object Required" 错误。

下面是我的代码的更新部分,我在其中搜索最小值。我目前收到 "Run-time 1004: Unable to get the Match property of the WorksheetFunction class"。

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

下面给你最小值的那一行(假设只有一个):

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)