Excel 宏从特定行/单元格开始,并连续粘贴到下一个空行/单元格
我正在处理一个宏,它将复制数据列中的所有其他值,并连续粘贴到新工作表中的新列。在下面的代码中,i=4 To LastRowC步骤2的Excel 宏从特定行/单元格开始,并连续粘贴到下一个空行/单元格,excel,vba,Excel,Vba,我正在处理一个宏,它将复制数据列中的所有其他值,并连续粘贴到新工作表中的新列。在下面的代码中,i=4 To LastRowC步骤2的循环正在运行,因为它确定要粘贴的第一个空行位于正确的位置 但是,对于i=4 To LastRowC Step 2的循环,宏发现一行向下太远,因为有另一个填充行将其丢弃,我需要指定它在特定单元格开始向上粘贴。但是在for循环期间,它仍然需要寻找空行以便在之后粘贴。这可能吗 Option Explicit Sub copyRange() Application
循环正在运行,因为它确定要粘贴的第一个空行位于正确的位置
但是,对于i=4 To LastRowC Step 2的循环,宏发现一行向下太远,因为有另一个填充行将其丢弃,我需要指定它在特定单元格开始向上粘贴。但是在for循环期间,它仍然需要寻找空行以便在之后粘贴。这可能吗
Option Explicit
Sub copyRange()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim wkbDest As Workbook
Dim wkbSource As Workbook
Set wkbDest = ThisWorkbook
Dim strExtension As String
Dim LastRowC As Long
Const strPath As String = "C:\Users\NGiuliano\Desktop\UPLOADS2\"
ChDir strPath
strExtension = Dir(strPath & "*.xls*")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath & strExtension)
With wkbSource.Sheets("Sheet1")
LastRowC = wkbSource.Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
For i = 4 To LastRowC Step 2
wkbSource.Worksheets("Sheet1").Range("A" & i).Copy
wkbDest.Worksheets("WIP").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Next i
For j = 4 To LastRowC Step 2
wkbSource.Worksheets("Sheet1").Range("B" & j).Copy
wkbDest.Worksheets("WIP").Cells(Rows.Count, 16).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Next j
Application.CutCopyMode = False
End With
wkbSource.Close savechanges:=False
strExtension = Dir
Loop
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
选项显式
子复制范围()
Application.ScreenUpdating=False
Application.Calculation=xlCalculationManual
作为整数的Dim i
作为整数的Dim j
将k变为整数
将wkbDest设置为工作簿
将wkbSource设置为工作簿
设置wkbDest=thishworkbook
作为字符串的Dim strExtension
暗淡的最后一行
Const strPath As String=“C:\Users\NGiuliano\Desktop\UPLOADS2”
ChDir strPath
strExtension=Dir(strPath&“*.xls*”)
当strExtension“”时执行此操作
设置wkbSource=Workbooks.Open(strPath&strExtension)
使用wkbSource.Sheets(“Sheet1”)
LastRowC=wkbSource.Worksheets(“Sheet1”)。单元格(Rows.Count,“A”)。结束(xlUp)。行
对于i=4到最后一个ROWC步骤2
wkbSource.工作表(“Sheet1”).范围(“A”和“i”).副本
wkbDest.Worksheets(“WIP”).单元格(Rows.Count,1).结束(xlUp).偏移量(1,0).粘贴特殊粘贴:=xlPasteValuesAndNumberFormats
接下来我
对于j=4到最后一行c步骤2
wkbSource.工作表(“Sheet1”).范围(“B”和“j”).副本
wkbDest.Worksheets(“WIP”).单元格(Rows.Count,16).结束(xlUp).偏移量(1,0).粘贴特殊粘贴:=xlPasteValuesAndNumberFormats
下一个j
Application.CutCopyMode=False
以
wkbSource.Close savechanges:=False
strExtension=Dir
环
Application.ScreenUpdating=True
Application.Calculation=xlCalculationAutomatic
端接头
将以我在最后一个问题中提供的代码为基础:
Dim nextrow as Long 'Option 1; this dimension isn't appropriate for Option 2
nextrow = 2 'starting row for pasting, used for Option 1
For i = 4 to LastRowC Step 2
'Use Cells(i,"B") or Range("B" & i)
'Option 1, use a counter (declare "nextrow" as Long and define before the loop, e.g., 2)
wkbDest.Worksheets("WIP").Cells(nextrow,16).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
nextrow = nextrow + 1
'Option 2, find next cell each time using "end(xldown)"
set nextcell = wkbDest.Worksheets("WIP").Cells(2,16).End(xlDown).Offset(1)
nextcell.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Next i
您可以使用任一选项,其中nextrow
是行号,nextcell
是实际使用的下一个单元格
在您的代码中,您试图使用标准的lastrow
语法(.End(xlUp)
),如果需要,这非常好。。。你熟悉这个脚本的实际工作原理吗
With Workbooks("Name").Sheets("Name")
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Option 1
set nextcell = .Cells(.Rows.Count, 1).End(xlUp) 'Option 2
End With
按顺序:
- 查找工作簿
- 查找工作表
- 找到牢房
- 专栏发现
- 找到的列中所有行的计数(即1048576)
- 从最后一个计数行向上移动到下一个包含数据的单元格
现在,您可以将其设置为一个范围,也可以.row
/.column
来查找所需的参数。在您的示例中,两者都没有,而且实际上在.pastespecial
行中有不适当的语法
花些时间阅读代码中每一行的功能,这可能会帮助您前进 啊,好的,谢谢你。是的,我用过End xlUp,也从未用过End xlDown。我也试过几次End xlDown,但似乎我没有使用正确的逻辑。再次感谢。