gpt4 book ai didi

vba - Excel VBA行复制和粘贴错误

转载 作者:行者123 更新时间:2023-12-04 21:15:02 26 4
gpt4 key购买 nike

以下代码是根据第一张工作表的 E 列中满足的关键字条件,将行从 sheet1 复制并粘贴到 sheet2。

在代码行 5 到 10 之间,我试图将整个第十行复制到 sheet2 以用作此工作表的列标题,其中将创建一个数据透视表。

每当我尝试运行任何操作来复制和粘贴此行时,我都会收到“应用程序定义或对象定义错误”。

任何人都可以帮忙吗?

Sub mileStoneDateChanger()
Dim r As Long, pasteRowIndex As Long, v() As Long, i As Long
Dim lastRow As Long
Dim lCol As Long
Sheets("Sheet1").Select
Rows("10:10").Select
Selection.Copy
Sheets("Sheet2").Select
Range("A1").Select
Selection.Paste
lastRow = Cells.Find(What:="*",
After:=Range("A1"),
LookAt:=xlPart,
LookIn:=xlFormulas,
SearchOrder:=xlByRows,
SearchDirection:=xlPrevious,
MatchCase:=False).Row
MsgBox "Last Row: " & lastRow
pasteRowIndex = 1

With Sheets("Sheet1")
For r = 1 To lastRow
If .Cells(r, "E").Value Like "Milestone*" Then
If UBound(Split(.Cells(r, "E"), ",")) > 0 Then
i = i + 1
ReDim v(1 To i)
v(i) = pasteRowIndex
End If
Sheets("Sheet1").Rows(r).Copy Sheets("Sheet2").Rows(pasteRowIndex)
pasteRowIndex = pasteRowIndex + 1
End If
Next r
End With

With Sheets("Sheet2")
newLastRow = pasteRowIndex
If IsArray(v) Then
.Columns(6).Insert shift:=xlToRight
For i = 1 To newLastRow
If InStr(1, .Cells(i, "E"), ",") Then
.Cells(i, "F").Value = Split(.Cells(i, "E"), ",")(1)
End If
Next i
End If
End With
End Sub

最佳答案

这是Sheet1中第10行到Sheet2中A1的副本

Sheets("Sheet1").Rows("10").Copy Sheets("Sheet2").Range("A1")

关于vba - Excel VBA行复制和粘贴错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47395918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com