Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 检查列中的最小值,并复制该最小值之后的所有数据_Excel_Vba_Variables_Worksheet Function - Fatal编程技术网

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

Excel 检查列中的最小值,并复制该最小值之后的所有数据,excel,vba,variables,worksheet-function,Excel,Vba,Variables,Worksheet Function,所以我仍然在从我之前询问过的同一张表中提取和分析数据,但我被要求重新评估我的方法 请参阅此处了解我所做工作的一些背景: 现在我必须找到在我的列中可以找到的最小值,然后复制该列后面显示的所有数据 以下是我目前拥有的代码: Public Path As String Public Counter As Integer Public NameFile As Workbook Public Celltxt As String 'Checks cell value in D2, used to compa

所以我仍然在从我之前询问过的同一张表中提取和分析数据,但我被要求重新评估我的方法

请参阅此处了解我所做工作的一些背景:

现在我必须找到在我的列中可以找到的最小值,然后复制该列后面显示的所有数据

以下是我目前拥有的代码:

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
我收到一个“运行时错误'424':需要对象”错误

下面是我的代码的更新部分,我在其中搜索最小值。我当前收到一个“运行时1004:无法获取WorksheetFunction类的Match属性”


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


这就是您建议的错误:“运行时错误'1004':对象'\u Global'的方法'Range'失败”。当我点击Debug时,它会转到“Set myRng=Range”(“D4:D500”&Rows.Count)这一行,它给了我一个无法获取WorksheetFunction类的Match属性的错误。这是我听说的非破坏性错误吗?不确定为什么会出现错误:我的代码段在这里工作正常。我刚刚注意到您更改了
Range(“A1:a”&Rows.Count)
范围(“D4:D500”和Rows.Count)
(应该是
范围(“D4:D”和Rows.Count)
范围(“D4:D500”)
您是否做了任何其他更改?您可能想使用更新后的代码是的,当您说您有打字错误时,我做了更改。我通过发布编辑后的代码来编辑我的问题以反映更改。您是否尝试过单步执行代码?另外,我注意到您
激活了很多内容。您可能想看看
    minRange = Application.WorksheetFunction.Min(Sheets(1).Range("D4:D500"))

    minRange.Activate
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)