Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 宏运行,但未粘贴任何数据_Excel_Vba - Fatal编程技术网

Excel 宏运行,但未粘贴任何数据

Excel 宏运行,但未粘贴任何数据,excel,vba,Excel,Vba,首先,我对excel vba非常陌生。我有一些代码在一个工作簿中运行良好,但在我需要的工作簿中不起作用。使用F8时,它跳过以If Cells(i,4)=customername开始的代码部分,然后通过End If 我已经搜索了好几天,试图在网上找到答案。当我运行宏时,屏幕闪烁,但数据消失。这是我试图使用的代码,任何帮助都将不胜感激 Sub CustomerReport() Dim datasheet As Worksheet Dim reportsheet As Workshee

首先,我对excel vba非常陌生。我有一些代码在一个工作簿中运行良好,但在我需要的工作簿中不起作用。使用F8时,它跳过以
If Cells(i,4)=customername开始的代码部分,然后通过
End If

我已经搜索了好几天,试图在网上找到答案。当我运行宏时,屏幕闪烁,但数据消失。这是我试图使用的代码,任何帮助都将不胜感激

Sub CustomerReport()
    Dim datasheet As Worksheet
    Dim reportsheet As Worksheet
    Dim customername As String
    Dim finalrow As Integer
    Dim i As Integer

    Set datasheet = Sheet3
    Set reportsheet = Sheet8
    customername = reportsheet.Range("D6").Value

    reportsheet.Range("C8:M500").ClearContents

    datasheet.Select
    finalrow = Cells(Rows.Count, 1).End(xlUp).Row

    For i = 8 To finalrow
        If Cells(i, 4) = customername Then
            Range(Cells(i, 3), Cells(i, 13)).Copy
            reportsheet.Select
            Range("C200").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
            datasheet.Select
            End If
    Next i
    reportsheet.Select

    Range("D6").Select
End Sub

会让你开始的。我相信,如果您避免使用select并完全限定对象,那么您将不会面临问题:)我的意思是,请参阅您如何在
reportsheet.Range(“D6”).Value中使用
在行
finalrow=Cells(Rows.Count,1)。End(xlUp)。row
,显示什么?@SiddharthRout-
如果Cells(i,4)=customername,则
固有地引用了
数据表
工作表。是否确实要
xlpasteFormulas和numberFormats
?通常情况下,报告不依赖其数字公式;尤其是从其他工作表复制数据时。会让你开始的。我相信,如果您避免使用select并完全限定对象,那么您将不会面临问题:)我的意思是,请参阅您如何在
reportsheet.Range(“D6”).Value中使用
在行
finalrow=Cells(Rows.Count,1)。End(xlUp)。row
,显示什么?@SiddharthRout-
如果Cells(i,4)=customername,则
固有地引用了
数据表
工作表。是否确实要
xlpasteFormulas和numberFormats
?通常情况下,报告不依赖其数字公式;尤其是从其他工作表复制数据时。