Excel 宏运行,但未粘贴任何数据
首先,我对excel vba非常陌生。我有一些代码在一个工作簿中运行良好,但在我需要的工作簿中不起作用。使用F8时,它跳过以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
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
?通常情况下,报告不依赖其数字公式;尤其是从其他工作表复制数据时。