Excel VBA复制范围和单元格

我正试图从表单A复制数据到表单B.

我想复制从第4行到第13行的第X列的单元格范围(X =具有最高值的第13行的单元格的列,并将复制的值粘贴到第4行到第B行的第13行。

运行代码不会复制数据,我没有任何错误,但没有任何粘贴。

有人可以看看代码,看看我的错误在哪里:

Sub Daily() 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("Sheet A") Set recordSht = ThisWorkbook.Sheets("Sheet B") 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(13, 1), .Cells(13, lColDaily))), 2) Set maxCustomerRng = .Range(.Cells(13, 1), .Cells(13, 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(13, 1), recordSht.Cells(13, 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 Range(Cells(4, maxCustomerRng), Cells(13, maxCustomerRng)).Copy recordSht.Cells(4, lCol + 1).PasteSpecial xlPasteValues recordSht.Cells(4, lCol + 1).PasteSpecial xlPasteFormats End If End If End With Set maxCustomerRng = Nothing Set dailySht = Nothing Set recordSht = Nothing End Sub 

您正试图在一组未整理的数据中find四舍五入的值。 CheckForDups总是没有

你有这个:

 With dailySht 

然后这些隐含引用的ActiveSheetWith块中:

 Range(Cells(4, maxCustomerRng), Cells(13, maxCustomerRng)).Copy 

这些隐式的ActiveSheet引用使得你的代码能够工作, 取决于哪个工作表当前处于活动状态 ,这可能不是你想要的 – FWIW Rubberduck (我pipe理的一个开源的VBIDE插件项目)可以帮助你轻松地find你的任何地方项目:

Rubberduck检查

该解决scheme可能会限定这些调用点,以便他们的dailySht工作表对象工作:

 .Range(.Cells(4, maxCustomerRng), .Cells(13, maxCustomerRng)).Copy