我在一张表单B2:ZY191中获取数据,我想将每一行(B2:ZY2,B3:ZY3,依此类推,直到B191:ZY191)复制到另一个工作簿工作表进行分析。在这样做的同时,我有时需要停止并在两者之间标记我的结果,然后从我离开的地方继续。例如,我启动了宏,它从B2:ZY2复制到B52:ZY52然后我暂停宏和&标记我的结果。现在我想继续从B52:ZY52开始然后再次如果我想在复制数据之后停止直到B95:ZY95我应该能够暂停宏,标记我的结果并从B95:ZY95继续。我应该能够按照自己的意愿多次这样做。 如果提供启动,暂停和恢复等按钮将非常有用。
答案 0 :(得分:1)
您可以采用以下解决方法:
选择您希望将数据范围虚拟划分为
的“集合”让我们说:
设置#1 =第1行到第20行
设置#2 =第21至30行
......等等
用“A”列中的任何字符标记所有选定集合的最后一行
所以你要在“A”栏的下列单元格中放置一个“1”(或“| I |”或“| E |”以外的任何其他字符 - 见下文)(即数据之前的单元格)范围):
A21
A31
......等等
(因为你的数据从第2行开始,然后第i行在工作表第I + 1行)
然后将以下代码放在数据范围工作簿的任何模块中:
Option Explicit
Sub DoThings()
Dim dataRng As Range, rngToCopy As Range
'assuming Analysis.xlsx is already open
Set dataRng = Worksheets("BZ").Range("B2:ZY191") '<--| this is the whole data range. you can change it (both worksheet name and range address) but be sure to have a free column preceeding it
Set rngToCopy = GetCurrentRange(dataRng) '<--| try and set the next "set" range to copy
If rngToCopy Is Nothing Then '<--| if no "set" range has been found...inform the user and exit sub!
MsgBox "There's an '|E|' at cell " _
& vbCrLf & vbCrLf & vbTab & dataRng(dataRng.Rows.Count, 1).Offset(, -1).Address _
& vbCrLf & vbCrLf & " marking data has already been entirely copied" _
& vbCrLf & vbCrLf & vbCrLf & "Remove it if you want to start anew", vbInformation
Exit Sub
End If
With rngToCopy
Workbooks("Analysis").Worksheets("Sheet1").Range(.Address).value = .value
End With
End Sub
Function GetCurrentRange(dataRng As Range) As Range
Dim f As Range
Dim iniRow As Long, endRow As Long
With dataRng
With .Offset(, -1)
Set f = .Resize(, 1).Find(what:="|E|", lookat:=xlWhole, LookIn:=xlValues) '<--| look for the "all copied" mark ("|E|")
If Not f Is Nothing Then Exit Function '<--| if "all copied" mark was there then exit function
Set f = .Resize(, 1).Find(what:="|I|", lookat:=xlWhole, LookIn:=xlValues) '<--| look for any "initial" mark put by a preceeding sub run
If f Is Nothing Then '<--|if there was no "initial" mark ...
iniRow = 1 '<--| ...then assume first row as initial one
Else
iniRow = f.row - .Cells(1).row + 1 '<--| ... otherwise assume "marked" row as initial one
f.ClearContents '<--| and clear it not to found it the next time
End If
endRow = .Cells(iniRow, 1).End(xlDown).row - .Cells(1).row + 1 '<--| set the last row as the next one with any making in column "A"
If endRow >= .Rows.Count Then '<--| if no mark has been found...
endRow = .Rows.Count '<--| ...set the last row as data last row...
.Cells(endRow, 1).value = "|E|" '<--|... and put the "all copied" mark in it
Else
.Cells(endRow, 1).ClearContents '<--| ...otherwise clear it...
.Cells(endRow + 1, 1).value = "|I|" '<--| ... and mark the next one as initial for a subsequent run
End If
End With
Set GetCurrentRange = .Rows(iniRow).Resize(endRow - iniRow + 1) '<--| finally, set the range to be copied
End With
End Function
并根据需要多次运行:每次结束后,您可以标记结果,然后再次运行,它将从左侧重新开始
答案 1 :(得分:0)
当您置于代码中时,可以使用Stop
和Debug.Print
来获得所需的结果。例如,如果您循环遍历范围,请使用if语句添加选择语句:
for a = 1 to 150
if a = 20 or a = 40 then
debug.Print "The value of a is: " & a.value 'or whatever you want to see
end if
next
这将打印到immediates窗口,或使用stop以相同的方式在战略位置暂停代码。
我不明白你的按钮是什么意思?它们肯定不是一个好主意,因为代码运行得太快了?