将文本插入同一行但多列 Excel VBA

Inserting text to the same row but multiple columns Excel VBA

我是 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 = "-"

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

对于“-”的插入部分,您可以使用循环方法而不是为 column/row.

编写每一行
' example
For counter = 1 To 10
    Sheets(1).Range("A" & counter).Value = "-"
Next

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

这是一种方法:

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

及之后:

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

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

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

祝你好运!

你有几个选择。一种是将 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 = "'-------"