【问题标题】:Changing the value in a range of cells更改单元格区域中的值
【发布时间】:2018-08-23 12:47:21
【问题描述】:

我有一张工作表,我想在与我的选择不同的列中更改一系列单元格中的值。我使用 VBA 是因为它是向特定打印机发送信息的代码的一部分。我想让宏也显示选择已打印。

澄清一下:
- 当我选择 F4:F10 时,我希望将单元格 G4:G10 更改为文本值“是”。
- 当我选择 F4:F15 时,我希望将单元格 G4:G15 更改为文本值“是”。

我搜索了论坛,发现/制作了以下内容:

Sheets("INPUT").Select
ActiveCell.Offset(0, 1).FormulaR1C1 = "Yes"

效果很好,但是它只改变一个单元格的值,而不是一系列单元格。

有没有办法将一系列单元格更改为“是”而不是一个单元格?

【问题讨论】:

  • 添加到@QHarr 的答案,您需要了解ActiveCellSelection 之间的区别。在您的代码中,您使用ActiveCell,它总是一个单元格,而Selection 可以是多个单元格——甚至是不相邻的单元格(在这种情况下,您需要使用Areas 属性)。事实上,Selection 可以是任何东西 - 单元格、形状、图表(嗯,图表 形状 :) )等等。

标签: excel vba range


【解决方案1】:

试试

Selection.Offset(0,1).Value = "Yes"

这是您选择的范围Selection。这偏移了 1 列,并且一次性分配了 "Yes" 的值。

【讨论】:

  • 我有点想念整个活动的部分。
  • Selection.Offset(,1).Value = "Yes" 更短:)
  • 确实如此:-)
  • @JohnyL 这是一个滑坡……很快我们就会像code golf 一样计算字节数 :-)
  • 如果我们能参加代码高尔夫比赛,但我无法想象 vba 会赢
【解决方案2】:

一般来说,尽量避免使用选择。这是提示用户选择范围,然后更新右侧列中的相应单元格的替代方法:

Sub tgr()

    Const sUpdatedValue As String = "Yes"

    Dim rSelected As Range
    Dim rArea As Range

    On Error Resume Next
    Set rSelected = Application.InputBox(Prompt:="Select Range of Cells." & Chr(10) & _
                                                 "Note that the cells in the column to the RIGHT of the selection will be updated.", _
                                         Title:="Range Selection", _
                                         Default:=Selection.Address, _
                                         Type:=8)
    On Error GoTo 0
    If rSelected Is Nothing Then Exit Sub   'Pressed cancel

    For Each rArea In rSelected.Areas
        rArea.Offset(, 1).Value = sUpdatedValue
    Next rArea

End Sub

【讨论】:

    【解决方案3】:

    好的,这是我解决这个问题的方法。我不太确定是什么触发了您当前的代码,所以我认为通过Worksheet.SelectionChange Event 捕获它是有意义的。

    如果您选择了所有F4:F10,这将更新所有G4:G10的值,同样,如果您选择所有F4:F15,则只会更改G4:G15。 p>

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Dim intersectedAll As Boolean
        Dim cell As Range
    
    
        Dim rng1 As Range
        Set rng1 = Range("F4:F10")
    
        If Target.count = rng1.count Then
            intersectedAll = True
    
            For Each cell In rng1
                If Intersect(Target, rng1) Is Nothing Then
                    intersectedAll = False
                End If
            Next cell
    
            If intersectedAll = True Then
                Target.offset(0, 1).value = "Yes"
                intersectedAll = False
            End If
        End If
    
    
        Dim rng2 As Range
        Set rng2 = Range("F4:F15")
    
        If Target.count = rng2.count Then
            intersectedAll = True
    
            For Each cell In rng2
                If Intersect(Target, rng2) Is Nothing Then
                    intersectedAll = False
                End If
            Next cell
    
            If intersectedAll = True Then
                Target.offset(0, 1).value = "Yes"
                intersectedAll = False
            End If
        End If
    
    End Sub
    

    【讨论】:

      【解决方案4】:

      感谢以上回复,我让它完美运行。非常感谢!

      我不知道如何关闭这个帖子或者是否有必要,但“案例已关闭”

      【讨论】:

        猜你喜欢
        • 2016-10-06
        • 1970-01-01
        • 2014-04-28
        • 1970-01-01
        • 2020-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多