![](/img/trans.png)
[英]Excel: Creating a macro to copy&paste active selection into another worksheet
[英]Excel Macro, read a worksheet, select range of data, copy selection
我需要编写一个宏,该宏读取GeoTechnical数据的工作表,根据特定行中的值选择数据,选择该行并继续读取直到工作表末尾。 选中所有行后,然后需要将这些行复制到新的工作表中。 我已经有大约10年没有做VBA了,所以只是想重新了解一下。
例如,我希望宏读取工作表,当“ I”列在特定行上包含单词“ Run”时,我想从该行中选择A:AM。 继续通读工作表,直到结束。 该文档的末尾比较棘手,因为有时工作表中的数据组之间最多有10-15个空白行。 如果多于25个空白行,则文档将在末尾。 选中所有内容后,我需要将所选内容复制并粘贴到新的工作表中。 这是我到目前为止的代码,但是我无法选择:
Option Explicit
Sub GeoTechDB()
Dim x As String
Dim BlankCount As Integer
' Select first line of data.
Range("I2").Select
' Set search variable value and counter.
x = "Run"
BlankCount = 0
' Set Do loop to read cell value, increment or reset counter and stop loop at end 'document when there
' is more then 25 blank cells in column "I", copy final selection
Do Until BlankCount > 25
' Check active cell for search value "Run".
If ActiveCell.Value = x Then
'select the range of data when "Run" is found
ActiveCell.Range("A:AM").Select
'set counter to 0
BlankCount = 0
'Step down 1 row from present location
ActiveCell.Offset(1, 0).Select
Else
'Step down 1 row from present location
ActiveCell.Offset(1, 0).Select
'if cell is empty then increment the counter
BlankCount = BlankCount + 1
End If
Loop
End Sub
Sub GeoTechDB()
Const COLS_TO_COPY As Long = 39
Dim x As String, c As Range, rngCopy As Range
Dim BlankCount As Integer
Set c = Range("I2")
x = "Run"
BlankCount = 0
Do Until BlankCount > 25
If Len(c.Value) = 0 Then
BlankCount = BlankCount + 1
Else
BlankCount = 0
If c.Value = x Then
If rngCopy Is Nothing Then
Set rngCopy = c.EntireRow.Cells(1) _
.Resize(1, COLS_TO_COPY)
Else
Set rngCopy = Application.Union(rngCopy, _
c.EntireRow.Cells(1) _
.Resize(1, COLS_TO_COPY))
End If
End If
End If
Set c = c.Offset(1, 0)
Loop
If Not rngCopy Is Nothing Then rngCopy.Copy Sheet2.Range("A2")
End Sub
我发现您的代码有很多错误。 如果我正确理解了您想要的内容,则此代码应该可以实现:
' Set Do loop to read cell value, increment or reset counter and stop loop at end 'document when there
' is more then 25 blank cells in column "I", copy final selection
Dim x As String
Dim BlankCount As Integer
Range("I2").Select
x = "Run"
BlankCount = 0
Dim found As Boolean
Dim curVal As String
Dim rowCount As Long
Dim completed As Boolean
rowCount = 2
Dim allRanges(5000) As Range
Dim rangesCount As Long
rangesCount = -1
notFirst = False
Do Until completed
rowCount = rowCount + 1
curVal = Range("I" & CStr(rowCount)).Value
If curVal = x Then
found = True
BlankCounter = 0
rangesCount = rangesCount + 1
Set allRanges(rangesCount) = Range("A" & CStr(rowCount) & ":AM" & CStr(rowCount))
ElseIf (found) Then
If (IsEmpty(Range("I" & CStr(rowCount)).Value)) Then BlankCount = BlankCount + 1
If BlankCount > 25 Then Exit Do
End If
If (rowCount >= 5000) Then Exit Do 'In the safest-side condition to avoid an infinite loop in case of not of finding what is intended. You can delete this line
Loop
If (rangesCount > 0) Then
Dim curRange As Variant
Dim allTogether As Range
Set allTogether = allRanges(0)
For Each curRange In allRanges
If (Not curRange Is Nothing) Then Set allTogether = Union(curRange, allTogether)
Next curRange
allTogether.Select
End If
它从I2开始遍历I列,直到找到单词“ Run”。 在这一刻,它开始对单元格进行计数,直到达到25(退出循环并选择了由最后一行和“运行”处定义的相应范围)。 您正在谈论空白单元格,但是您的代码没有对此进行检查,而且我不确定在找到非空白单元格时该怎么做(重新启动计数器?)。 请对此进行详细说明。
我喜欢短代码:
Sub column_I_contains_run()
If ActiveSheet.FilterMode Then Selection.Autofilter 'if an autofilter already exists this is removed
ActiveSheet.Range("$I$1:$I$" & ActiveSheet.Cells(1048576, 9).End(xlUp).Row).Autofilter Field:=1, Criteria1:="*run*"
Range("A1:AM" & ActiveSheet.Cells(1048576, 9).End(xlUp).Row).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
End Sub
现在您只需将其粘贴到新的工作表中,也可以将其自动化...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.