我想找到「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
告訴我們你在哪一行發生了什麼錯誤。或者會發生什麼。簡單地說它不起作用太寬泛。 – Luuklag
@Luuklag在比較第二行以外的任何其他行的值時,超出代碼的工作範圍是我正在嘗試做的。比較第二行中的值時沒有任何反應。當它處於調試模式時,它顯示在第二行中選擇了正確的值,但沒有超過其列。 – aab
@Luuklag是的,我做了正確的值匹配。它只是不復制它的列 – aab