插入文本到同一行,但多列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 = "-"
你有几个select。 一个是将StrikeThrough
属性设置为True,然后用一些空格填充单元格。 或者,如果带有撇号,则可以input一些连字符。
就像是:
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 = "'-------"
Excel允许使用删除线文本,并将其设置为空白单元格的格式。 请参阅MS文档: https : //support.office.com/zh-cn/article/Format-text-as-strikethrough-163dd4e8-f413-4474-9c42-aaee8828d647
对于插入部分的“ – ”,你可以使用循环方法,而不是每行写一个列/行。
' 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
variables的名称更改为CashRowNumber)
编辑#1:
如果范围内的某些单元格有文本,而且某些单元格是空的,并且想要在填充的单元格上使用删除线字体,并在空单元格中插入短划线,则在之前:
新的macros观:
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
您可以通过将工作表名称variables或列范围variables(例如)使其更灵活。
祝你好运!