脚本上的复制和粘贴功能出错

aab

我的脚本查找最高值dailySht并将这些值粘贴到单独的工作表中recordSht,这通常工作正常,但有时我会收到错误Object variable or With block variable not set下面是返回错误的代码部分。

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(1, .Columns.Count).End(xlToLeft).Column
        maxCustomerCnt = Round(Application.Max(.Range(.Cells(58, 1), .Cells(58, lColDaily))), 2)
        Set maxCustomerRng2 = .Range(.Cells(58, 1), .Cells(58, lColDaily)).Find(What:=maxCustomerCnt, LookIn:=xlValues)

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

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

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

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

        .Cells(58, maxCustomerRng2.Column).Copy
        recordSht.Cells(58, lCol + 1).PasteSpecial xlPasteValues
        recordSht.Cells(58, lCol + 1).PasteSpecial xlPasteFormats
    End With

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

有人可以帮我找出问题所在,因为代码在某些单元格而不是其他单元格上工作(复制并粘贴正确的值)。

Pᴇʜ

我建议使用Match而不是直接Find使用结果Max而不将其转换为,Double以避免浮点不准确。

With dailySht
    lColDaily = .Cells(1, .Columns.Count).End(xlToLeft).Column
    Dim SearchRange As Range
    Set SearchRange = .Range(.Cells(58, 1), .Cells(58, lColDaily))

    Dim MaxCol As Long
    On Error Resume Next 'next line throws error if nothing matched
    MaxCol = Application.WorksheetFunction.Match(Application.WorksheetFunction.Max(SearchRange), SearchRange, 0)
    On Error GoTo 0 're-enable error reporting !!!

    If MaxCol = 0 Then
        'nothing was found
        Exit Sub
    End If

    .Cells(4, MaxCol).Copy
    'your stuff here

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Excel VBA复制和粘贴(不使用复制+粘贴功能)在空行上

来自分类Dev

Excel VBA复制和粘贴(不使用复制+粘贴功能)在空行上

来自分类Dev

.csv文件的特殊复制和粘贴脚本

来自分类Dev

复制和粘贴会导致脚本损坏?

来自分类Dev

AHK复制和粘贴脚本无法正确复制

来自分类Dev

在 VBA excel 上复制和粘贴

来自分类Dev

脚本失败,但是可以从脚本复制和粘贴

来自分类Dev

使用对象在Excel上复制粘贴时出错

来自分类Dev

脚本复制/粘贴条件

来自分类Dev

使用活动的x按钮复制和粘贴时出错

来自分类Dev

Google Apps 脚本:仅复制和粘贴公式

来自分类Dev

用于在工作簿中的多个工作表上复制和粘贴最后一列数据的 VBA 脚本

来自分类Dev

Android:如何完全禁用Edittext中的复制和粘贴功能

来自分类Dev

在Mac上的Android Studio中,复制&粘贴&粘贴/自动完成功能无法正常工作

来自分类Dev

禁用复制和粘贴

来自分类Dev

在Mac上重新映射复制和粘贴快捷方式

来自分类Dev

在 HTML 文本上复制和粘贴 URL 参数

来自分类Dev

复制和粘贴不同的格式

来自分类Dev

如何复制和粘贴文件?

来自分类Dev

CKeditor格式复制和粘贴

来自分类Dev

正确的复制和粘贴方式

来自分类Dev

如何复制和粘贴文件?

来自分类Dev

如何分别复制和粘贴?

来自分类Dev

使用URxvt复制和粘贴

来自分类Dev

复制和粘贴不同的格式

来自分类Dev

Excel宏复制和粘贴

来自分类Dev

循环进行复制和粘贴

来自分类Dev

复制和粘贴数据匹配

来自分类Dev

复制和粘贴 Vim 效果