2017-09-22 28 views
2

我想找到「2G」頁第二行中的最高值,並將其粘貼的整列到「Daily2G」表。 '2G'表的第一行有日期和時間(24小時)。Excel VBA中的數據複製到不同的片

代碼還會比較日期,並且只在日期不同時複製數據。

過去兩天的代碼工作正常,但它今天不工作。我無法弄清楚問題所在。如果有人可以看一下代碼並告訴我哪裏出錯,我將不勝感激。

的代碼,如果我在任何其他行比較值,但我只希望在第二排,檢查工作值。此外,重複支票也沒有工作,這是在今天之前。

Sub Daily2G() 
    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 maxCustomerRng As Range ' Cell containing the highest number of customers 
    Dim CheckForDups As Range ' Used to find duplicate dates on the record Sheet 
    Dim maxCustomerCnt As Long ' value of highest customer count 

    Set dailySht = ThisWorkbook.Sheets("2G") 
    Set recordSht = ThisWorkbook.Sheets("Daily 2G") 
    With recordSht 
     lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 
    End With 
    With dailySht 
     lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column 
     maxCustomerCnt = Application.Max(.Range(.Cells(2, 1), .Cells(2, lColDaily))) 
     Set maxCustomerRng = .Range(.Cells(2, 1), .Cells(2, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues) 
     If Not maxCustomerRng Is Nothing Then 
     ' Check the Record Sheet to ensure the data is not already there 
      Set CheckForDups = recordSht.Range(recordSht.Cells(1, 1), recordSht.Cells(1, lCol)).Find(What:=maxCustomerRng.Offset(-1, 0).Value, LookIn:=xlValues) 
     ' If CheckForDups is Nothing then the date was not found on the record sheet. Therefore, copy the column 
      If CheckForDups Is Nothing Then 
       maxCustomerRng.EntireColumn.Copy 
       recordSht.Cells(1, lCol + 1).PasteSpecial xlPasteValues 
       recordSht.Cells(1, lCol + 1).PasteSpecial xlPasteFormats 
      End If 
     End If 
    End With 

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

告訴我們你在哪一行發生了什麼錯誤。或者會發生什麼。簡單地說它不起作用太寬泛。 – Luuklag

+0

@Luuklag在比較第二行以外的任何其他行的值時,超出代碼的工作範圍是我正在嘗試做的。比較第二行中的值時沒有任何反應。當它處於調試模式時,它顯示在第二行中選擇了正確的值,但沒有超過其列。 – aab

+0

@Luuklag是的,我做了正確的值匹配。它只是不復制它的列 – aab

回答

2

不知道如何和什麼是你想找到,因爲複製的代碼,所以改變了一點,這樣如果按照示例文件,3488.95不會在2行中Daily2G表發現,該代碼將將最大值的列複製到Daily2G表中,否則它將跳過。

而且,在示例文件,工作表名稱是「Daily2G」不「每日2G」,所以改變了它的代碼,並可以根據需要改變它在你的實際工作簿。

與您的代碼的問題是你已經聲明maxCustomerCnt只要而在2G片2行的值是十進制值,因此NaxCustomerRng永遠是一無所有。

請試試這個...

Sub Daily2G() 
    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 maxCustomerRng As Range ' Cell containing the highest number of customers 
    Dim CheckForDups As Range ' Used to find duplicate dates on the record Sheet 
    Dim maxCustomerCnt As Double ' value of highest customer count 

    Set dailySht = ThisWorkbook.Sheets("2G") 
    Set recordSht = ThisWorkbook.Sheets("Daily2G") 
    With recordSht 
     lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 
    End With 
    With dailySht 
     lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column 
     maxCustomerCnt = Round(Application.Max(.Range(.Cells(2, 1), .Cells(2, lColDaily))), 2) 
     Set maxCustomerRng = .Range(.Cells(2, 1), .Cells(2, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues) 
     If Not maxCustomerRng Is Nothing Then 
     ' Check the Record Sheet to ensure the data is not already there 
      Set CheckForDups = recordSht.Range(recordSht.Cells(2, 1), recordSht.Cells(2, lCol)).Find(What:=Round(maxCustomerRng.Value, 2), LookIn:=xlValues) 
     ' If CheckForDups is Nothing then the date was not found on the record sheet. Therefore, copy the column 
      If CheckForDups Is Nothing Then 
       maxCustomerRng.EntireColumn.Copy 
       recordSht.Cells(1, lCol + 1).PasteSpecial xlPasteValues 
       recordSht.Cells(1, lCol + 1).PasteSpecial xlPasteFormats 
      End If 
     End If 
    End With 

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

運行在您提供的樣本文件上面的代碼,如果它工作得很好,使需要更改後與您的實際文件測試。

+0

謝謝!但是檢查重複項不起作用 – aab

+0

根據您的示例文件,如果您再次運行代碼,那麼一旦該列被複制到Daily2G表單中,您認爲重複的內容是什麼?建議的代碼將在Daily2G工作表的第2行的2G頁面(本例中爲3488.95)的row2中查找第2行的最大值,如果發現它,它將認爲它是重複的,並且不會將同一整個列再次複製到Daily2G表。當您檢查重複項目時,您是否想要實現其他功能? – sktneer

+0

例如如果您使用相同的示例文件多次運行建議的代碼而不對2G工作表中的第2行進行任何更改,它將不會多次複製H列。 – sktneer

相關問題