连续寻找最高价值,并使用VBA在excel中复制

时间:2018-10-23 16:02:40

标签: excel vba

我正在尝试在一行中搜索最高值,并将特定的单元格从该列(基于具有最高值的行)复制到另一张工作表中。 Scipt工作正常,但是最近它没有选择最高值,我无法弄清楚。任何帮助将不胜感激。

Sub DailyBH()
    Dim dailySht As Worksheet 'worksheet storing latest store activity
    Dim recordSht As Worksheet 'worksheet to store the highest period of each day
    Dim lColDaily As Integer ' Last column of data in the store activity sheet
    Dim lCol As Integer ' Last column of data in the record sheet
    Dim maxCustomerRng2 As Range ' Cell containing the highest number of customers
    Dim maxCustomerCnt As Double ' value of highest customer count


Set dailySht = ThisWorkbook.Sheets("hourly KPI")
Set recordSht = ThisWorkbook.Sheets("@BH KPI")
With recordSht
    lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With


With dailySht
    lColDaily = .Cells(59, .Columns.Count).End(xlToLeft).Column
    maxCustomerCnt = Round(Application.Max(.Range(.Cells(59, 1), .Cells(59, lColDaily))), 2)

     If Not maxCustomerRng2 Is Nothing Then

            .Cells(32, maxCustomerRng2.Column).Copy
            recordSht.Cells(32, lCol + 1).PasteSpecial xlPasteValues
            recordSht.Cells(32, lCol + 1).PasteSpecial xlPasteFormats


            .Cells(14, maxCustomerRng2.Column).Copy
            recordSht.Cells(14, lCol + 1).PasteSpecial xlPasteValues
            recordSht.Cells(14, lCol + 1).PasteSpecial xlPasteFormats

            .Cells(59, maxCustomerRng2.Column).Copy
            recordSht.Cells(59, lCol + 1).PasteSpecial xlPasteValues
            recordSht.Cells(59, lCol + 1).PasteSpecial xlPasteFormats

End If
End With

 Set maxCustomerRng = Nothing
   Set dailySht = Nothing
   Set recordSht = Nothing
End Sub

0 个答案:

没有答案