我的代码完全按照我想要的方式工作,但是我不希望它跳到另一列。我只想让我的宏在 C 列内运行然后退出。我是 excel 中的 VBA 新手,所以请原谅我的错误。任何帮助将非常感激。提前致谢。
Sub CopyValuetoRange()
'
' CopyValuetoRange Macro
Dim search_range As Range, Block As Range, last_cell As Range
Dim first_address$
Set search_range = ActiveSheet.UsedRange
Set Block = search_range.Find(what:="*", _
after:=search_range.SpecialCells(xlCellTypeLastCell), _
LookIn:=xlValues, searchorder:=xlColumns, searchdirection:=xlDown)
If Block Is Nothing Then Exit Sub
Set Block = Block.CurrentRegion
first_address$ = Block.Address
Do
Block.Select
Selection.End(xlDown).Select
ActiveCell.CurrentRegion.Rows(2).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormulaR1C1 = "=R[-1]C"
'MsgBox "Next Block Range"
Set last_cell = Block.Cells(Block.Rows.Count)
Set Block = search_range.FindNext(after:=last_cell).CurrentRegion
Loop Until Block.Address = first_address$ 'ActiveSheet.Range("C26").End(xlDown).Row
End Sub
这是我从我发现基本上会做同样事情的东西中修改的东西,但是它将第一个单元格值放入范围内的所有单元格中。而这个宏实际上保留在 C 列中,因为我最近发现它不是一个区域,而是一个范围。
有没有办法更改以下内容以将公式添加到指向范围中第一个单元格的范围内的所有单元格?
Sub Macro5()
Dim Rng As Range
Dim RngEnd As Range
Dim rngArea As Range
Set Rng = Range("C1")
Set RngEnd = Cells(Rows.Count, Rng.Column).End(xlDown)
If RngEnd.Row < Rng.Row Then Exit Sub
Set Rng = Range(Rng, RngEnd)
On Error GoTo ExitSub
Set Rng = Rng.SpecialCells(xlCellTypeConstants)
For Each rngArea In Rng.Areas
rngArea.Value = rngArea.Cells(Rng.Rows.Count, 1).Value
Next rngArea
ExitSub:
' Macro will exit here if the range is empty.
End Sub