Excel VBA将数据复制到不同的工作表中

我想在“2G”表格的第二行find最高值,并把整个列粘贴到“Daily2G”表格中。 “2G”表的第一行有date和时间(24小时)。

代码还会比较date,如果date不同,则只复制数据。

过去两天的代码工作正常,但今天不工作。 我找不出什么问题。 如果有人能看看代码并告诉我哪里出错,我将不胜感激。

代码的作品,如果我比较其他行的值,但我想检查只在第二行的值。 此外重复支票也不工作,这是在今天之前。

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 

不知道如何以及如何find作为重复,所以在代码中进行了一些修改,以便如果根据示例文件在Daily2G工作表的第2行中找不到3488.95,则代码会将Max列的值复制到Daily2G表格,否则会跳过。

另外,在示例文件中,工作表名称是“Daily2G”而不是“Daily 2G”,因此在代码中进行了更改,并根据需要在实际工作簿中对其进行了更改。

你的代码的问题是你声明了maxCustomerCnt为long,而在2G页面上的row2中的值是十进制值,所以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 

在你提供的示例文件中运行上面的代码,如果运行良好,在进行所需的更改后,用你的实际文件进行testing。