【问题标题】:Inserting text to the same row but multiple columns Excel VBA将文本插入同一行但多列Excel VBA
【发布时间】:2016-05-16 14:03:26
【问题描述】:

我是 VBA 新手,我知道删除线是一种文本格式,我认为它不能用于空白单元格。

有没有更有效的方法将“-”插入同一行但从“D”到“G”的多列?

请看我的代码:

Set Insert = Sheets("Cash").Cells(CashRowName, "D")
Insert.Value = "-"

Set Insert = Sheets("Cash").Cells(CashRowName, "E")
Insert.Value = "-"

Set Insert = Sheets("Cash").Cells(CashRowName, "F")
Insert.Value = "-"

Set Insert = Sheets("Cash").Cells(CashRowName, "G")
Insert.Value = "-"

【问题讨论】:

    标签: vba excel multiple-columns


    【解决方案1】:

    您有几个选择。一种是将StrikeThrough 属性设置为True,然后用少量空格填充单元格。或者,如果以撇号开头,您可以只输入几个连字符。

    类似:

    Sheets("Cash").Cells(CashRowName, "D").Resize(, 4).Font.StrikeThrough = True
    Sheets("Cash").Cells(CashRowName, "D").Resize(, 4).Value = "          "
    

    或者:

    Sheets("Cash").Cells(CashRowName, "D").Resize(, 4).Value = "'-------"
    

    【讨论】:

      【解决方案2】:

      Excel 允许删除线文本,并将其设置为空白单元格上的格式。 参考 MS 文档:https://support.office.com/en-us/article/Format-text-as-strikethrough-163dd4e8-f413-4474-9c42-aaee8828d647

      对于“-”的插入部分,您可以使用循环方法而不是为列/行编写每一行。

      ' example
      For counter = 1 To 10
          Sheets(1).Range("A" & counter).Value = "-"
      Next
      

      但似乎 ms 文档会提供更多帮助。

      【讨论】:

      • 我知道 excel 允许删除文本,但我需要在空白单元格上添加删除线,除非我向其写入内容,否则这是不可能的。
      【解决方案3】:

      这是一种方法:

      Sub dural()
          Dim CashRowName As Long, Insert As Range
          CashRowName = 6
          Set Insert = Sheets("Cash").Range("D" & CashRowName & ":G" & CashRowName)
          Insert.Value = "-"
      End Sub
      

      (我个人会将Long 变量的名称更改为 CashRowNumber)

      编辑#1:

      如果范围内的某些单元格包含文本,而某些单元格为空,并且您想在填充的单元格上使用删除线字体并在空单元格中插入破折号,则在之前:

      新宏:

      Sub dural2()
          Dim CashRowName As Long, Insert As Range
          Dim cel As Range
      
          CashRowName = 6
          Set Insert = Sheets("Cash").Range("D" & CashRowName & ":G" & CashRowName)
      
          For Each cel In Insert
              cel.Font.Strikethrough = True
              If cel.Value = "" Then
                  cel.Value = "-"
              End If
          Next cel
      End Sub
      

      之后:

      【讨论】:

      • 谢谢!这对我来说真的是一个新事物!
      【解决方案4】:

      一种方法是通过根据列号引用它们来循环遍历列:

      Public Sub DashOutRow(CashRowName as String)
        Dim i as Long
        Dim Insert as Excel.Range
      
        For i = 4 to 7 'D - G
          Set Insert = Sheets("Cash").Cells(CashRowName, i)
          Insert.Value = "'-"
        Next
      End Sub
      

      您可以通过将工作表名称设置为变量或列范围变量(例如)来使其更加灵活。

      祝你好运!

      【讨论】:

      • 感谢您的帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 2010-12-16
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      相关资源
      最近更新 更多