Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA中的循环?我想使用循环选择并复制到最后一个单元格_Vba_Loops - Fatal编程技术网

VBA中的循环?我想使用循环选择并复制到最后一个单元格

VBA中的循环?我想使用循环选择并复制到最后一个单元格,vba,loops,Vba,Loops,我试图从范围K1开始选择K行中的每个连续单元格,对于向下的每个单元格,复制值并将其粘贴到单元格M10中。但是,按照当前写入宏的方式,宏选择范围K中最后一个单元格正下方的单元格,从而将空白复制到M10中。相反,我希望循环一次运行一个单元格。我希望一次选择一个单元格并将其复制,即循环将选择K1并将其复制到M10,然后选择K2并将其复制到M10,依此类推,然后在范围K的最后一个单元格之后停止循环 谁能帮我一下吗 Sub test() lastcell = Range("K" & Cells

我试图从范围K1开始选择K行中的每个连续单元格,对于向下的每个单元格,复制值并将其粘贴到单元格M10中。但是,按照当前写入宏的方式,宏选择范围K中最后一个单元格正下方的单元格,从而将空白复制到M10中。相反,我希望循环一次运行一个单元格。我希望一次选择一个单元格并将其复制,即循环将选择K1并将其复制到M10,然后选择K2并将其复制到M10,依此类推,然后在范围K的最后一个单元格之后停止循环

谁能帮我一下吗

Sub test() 

lastcell = Range("K" & Cells.Rows.Count).End(xlUp) 

Range("K2").Select 
Do 
ActiveCell.Offset(1, 0).Select 
Selection.Copy 
Range("M10").Select 
Selection.PasteSpecial
Application.Run ("Second Macro") 
Loop Until IsEmpty(ActiveCell.Value) 

End Sub

您可以使用下面的小脚本在K列中循环:

Option Explicit
Sub LoopThroughColumnK()

Dim LastRowInColK As Long, Counter As Long
Dim SourceCell As Range, DestCell As Range
Dim MySheet As Worksheet

'set references up-front
Set MySheet = ThisWorkbook.Worksheets("Sheet1")
With MySheet
    LastRowInColK = .Range("K" & .Rows.Count).End(xlUp).Row
    Set DestCell = .Range("M10")
End With

'loop through column K, copying from cells(counter, 11) to M10
With MySheet
    For Counter = 1 To LastRowInColK
        Set SourceCell = .Range("K" & Counter)
        SourceCell.Copy Destination:=DestCell
        'call MyMacro below
        '... doing cool MyMacro stuff
    Next Counter
End With

End Sub
为了总结正在发生的事情,我们:

分配一个工作表变量以确保我们在正确的工作表上工作 为最后一行和单元格分配易于读取和引用的变量 在有问题的范围内循环,从Kn复制和粘贴到M10 此技术还避免使用.Select,这是运行时错误的常见来源。下面是一篇精彩的文章,概述了许多不用的方法。选择并激活:

编辑:我在上面的评论中描述的重构可以不费吹灰之力地实现。让我们把整个问题分成两部分:

获取K列中所有占用的单元格,并将它们保存为一个范围 为我们在上面步骤1中保存的范围内的每个单元格运行辅助宏(已从单元格M10中键入)。现在我们将调用次宏MyOtherMacro 我们去追吧。星期天,大家好!下面的代码被大量注释,以解释每个函数和步骤中发生了什么:

Option Explicit
Sub DoWork()

Dim MySheet As Worksheet
Dim ColKRange As Range

'set the worksheet we want to work on, in this case "Sheet1"
Set MySheet = ThisWorkbook.Worksheets("Sheet1")

'get the range of occupied cells in col K
Set ColKRange = OccupiedRangeInColK(MySheet)

'kick off the other macro using the range we got in the step above
Call MyOtherMacro(ColKRange)

End Sub
上面的DoWork是一个控制器类型脚本。它所做的只是启动我们在下面编写的另外两个函数,OccpiedrangeIncolk,然后,一步之后,MyOtherMacro

当涉及到脚本时,很酷的描述性名称是一件好事。上面的OccupiedRangeInColK获取一个工作表,然后从K列返回占用范围

'this function is a shell to be populated by @polymorphicicebeam
Public Function MyOtherMacro(TargetRange As Range)
    Dim Cell As Range

    'check for an empty range, exit the function if empty
    If TargetRange Is Nothing Then Exit Function

    'loop through all the cells in the passed-in range
    For Each Cell In TargetRange
        'Do cool stuff in here. For demo purposes, we'll just
        'print the address of the cell to the screen
        MsgBox (Cell.Address)
    Next Cell
End Function

最后,上面的MyOtherMacro是您添加自己的魔法的地方。我为您构建了一个shell函数,它只需使用MsgBox打印相关单元格的地址。您可以将自己的逻辑添加到TargetRange循环中每个单元格的指定位置。呜

虽然我理解你想要实现的目标,但我不知道为什么。单元格M10在工作表上是否有一些控制功能?这是为了运行另一个宏,即,一旦我将第一个值粘贴到M10中,我将应用程序运行另一个宏,然后使用循环粘贴第二个单元格并再次运行宏,等等。理解-感谢您的澄清。一个应该引起你注意的重构是这里的设计变更。假设您将运行的宏名为MyMacro-您可以修改它以获取范围变量,如Sub MyMacroTargetRange As Range,然后在传入的范围内的每个单元格上循环MyMacro OK,那么,您能告诉我如何编辑上面的代码以反映这些更改吗?我下面的笔记包括一个简单的脚本,用于将K列值发送到单元格M10,以及我在上一篇评论中描述的更深入的重构探索谢谢@Floris!有时我喜欢回答这样的问题,因为有很多机会将一次性脚本重构为可重用的函数。
'this function is a shell to be populated by @polymorphicicebeam
Public Function MyOtherMacro(TargetRange As Range)
    Dim Cell As Range

    'check for an empty range, exit the function if empty
    If TargetRange Is Nothing Then Exit Function

    'loop through all the cells in the passed-in range
    For Each Cell In TargetRange
        'Do cool stuff in here. For demo purposes, we'll just
        'print the address of the cell to the screen
        MsgBox (Cell.Address)
    Next Cell
End Function