VBA Excel 2007:循环插入到错误的位置行。
我编写了两个循环来比较两个列表,并将一个列表中缺少的信息插入到另一个列表中。 不幸的是,宏只在运行时的中间插入第一行以下的行。我试图用If语句来避免这种情况,但是错误将从第二行开始 代码如下:VBA Excel 2007:循环插入到错误的位置行。,vba,excel,excel-2007,Vba,Excel,Excel 2007,我编写了两个循环来比较两个列表,并将一个列表中缺少的信息插入到另一个列表中。 不幸的是,宏只在运行时的中间插入第一行以下的行。我试图用If语句来避免这种情况,但是错误将从第二行开始 代码如下: Sub CopyData() Dim dl_length As Integer Dim oa_length As Integer Dim dl_count As Integer Dim oa_count As Integer dl_length = Worksheets("download").Cel
Sub CopyData()
Dim dl_length As Integer
Dim oa_length As Integer
Dim dl_count As Integer
Dim oa_count As Integer
dl_length = Worksheets("download").Cells(Rows.Count, 1).End(xlUp).Row + 1
oa_length = Worksheets("overall").Cells(Rows.Count, 1).End(xlUp).Row + 1
For dl_count = 1 To dl_length
For oa_count = 1 To oa_length
If Worksheets("download").Range("F" & dl_count) = Worksheets("overall").Range("C" & oa_count) Then
Worksheets("overall").Range("C" & oa_count).Select
ActiveCell.Offset(1).EntireRow.Insert
Worksheets("overall").Range("A" & oa_count + 1) = "Search and replace"
Worksheets("overall").Range("E" & oa_count + 1) = Worksheets("download").Range("L" & dl_count)
End If
oa_length = Worksheets("overall").Cells(Rows.Count, 1).End(xlUp).Row + 1
Next oa_count
Next dl_count
End Sub
当我试图
你能帮我改进代码吗?替换
Worksheets("overall").Range("C" & oa_count).Select
ActiveCell.Offset(1).EntireRow.Insert
与
关于good fella MSDN的一点ActiveCell
:
返回表示活动单元格中活动单元格的范围对象
窗口(顶部的窗口)或指定窗口中的窗口。如果窗户
未显示工作表,此属性失败。只读
插入一行时,不需要
oa_length=工作表(“总体”)。单元格(Rows.Count,1)。结束(xlUp)。row+1
相反,oa_length=oa_length+1
会工作得更快
如果,它也应该在结束之前
但是仍然更新oa_长度不会使
的循环多运行一行
为此,必须使用While
或重复,直到循环
Option Explicit
Sub CopyData()
Dim dl_length&
Dim oa_length&
Dim dl_count&
Dim oa_count&
Dim Sh_oa As Worksheet
Dim Sh_dl As Worksheet
With ThisWorkbook
Set Sh_oa = .Sheets("overall")
Set Sh_dl = .Sheets("download")
End With
With Sh_oa
oa_length = .Cells(.Rows.Count, 1).End(xlUp).Row 'i removed the +1, wich is a blank cell
End With
With Sh_dl
dl_length = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
With Application 'this part is to make things faster...
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
For dl_count = 1 To dl_length
While oa_count <= oa_length
oa_count = oa_count + 1
If Sh_dl.Range("F" & dl_count).Value2 = Sh_oa.Range("C" & oa_count).Value2 Then
oa_count = oa_count + 1 'if you insert a line, you'll need to read a line after that later
With Sh_oa
.Rows(oa_count).Insert
.Cells(oa_count, 1).Value2 = "Search and replace"
.Range("E" & oa_count).Value2 = Sh_dl.Range("L" & dl_count).Value2
End With
oa_length = oa_length + 1 'wider the scope of checks
End If
Wend
Next dl_count
Set Sh_oa = Nothing
Set Sh_dl = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub
选项显式
子副本数据()
暗dl_长度&
暗弧长度&
模糊dl_计数&
模糊计数&
将SHU oa设置为工作表
将SHU dl设置为工作表
使用此工作簿
设置Sh_oa=.Sheets(“整体”)
设置Sh_dl=.Sheets(“下载”)
以
和希欧
oa_length=.Cells(.Rows.Count,1).End(xlUp).Row'i删除了+1,这是一个空白单元格
以
用shu-dl
dl_长度=.Cells(.Rows.Count,1).End(xlUp).Row
以
“应用程序”这一部分是为了让事情变得更快。。。
.ScreenUpdate=False
.EnableEvents=False
.Calculation=xlCalculationManual
以
对于dl_计数=1到dl_长度
当oa_count查看偏移量(1)
时,您想在这里做什么?非常感谢。代码现在对我有效:),但你忘了这件小事:在之前和之后对于你需要:oa\u count=1
是的,很好地发现了,但事实上oa\u count=0
,所以它在第一个循环中从1开始(而不是2)。
Option Explicit
Sub CopyData()
Dim dl_length&
Dim oa_length&
Dim dl_count&
Dim oa_count&
Dim Sh_oa As Worksheet
Dim Sh_dl As Worksheet
With ThisWorkbook
Set Sh_oa = .Sheets("overall")
Set Sh_dl = .Sheets("download")
End With
With Sh_oa
oa_length = .Cells(.Rows.Count, 1).End(xlUp).Row 'i removed the +1, wich is a blank cell
End With
With Sh_dl
dl_length = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
With Application 'this part is to make things faster...
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
For dl_count = 1 To dl_length
While oa_count <= oa_length
oa_count = oa_count + 1
If Sh_dl.Range("F" & dl_count).Value2 = Sh_oa.Range("C" & oa_count).Value2 Then
oa_count = oa_count + 1 'if you insert a line, you'll need to read a line after that later
With Sh_oa
.Rows(oa_count).Insert
.Cells(oa_count, 1).Value2 = "Search and replace"
.Range("E" & oa_count).Value2 = Sh_dl.Range("L" & dl_count).Value2
End With
oa_length = oa_length + 1 'wider the scope of checks
End If
Wend
Next dl_count
Set Sh_oa = Nothing
Set Sh_dl = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub