在单元格中添加前后值asterix。

我在网上find了这个代码,以帮助我在列的值前后打印asterix。 这个问题是,它打印4 asterix如果我有一个select的4行列等等我不能硬编码的范围,因为它不同的时间。 有时候,我在列a中有2行,其中有5或7等等。

你有任何提示,我可以如何解决这个或任何帮助? 我不知道如何声明我的范围,例如,所以它一次检查一个单元格,而不是一个单元格范围。

Sub Add_Asterisk() Dim r As Range With Selection For Each r In Selection r.Value = "*" & r.Value & "*" Next End With End Sub 

先谢谢你/ D

您可以声明一个variables来保存所需列中的数据的最后一行,然后遍历设置范围中的所有单元格。

 Sub Add_Asterisk() Dim LastRow As Long Dim Rng As Range, Cell As Range 'Finding the last row with data in column A LastRow = Cells(Rows.Count, "A").End(xlUp).Row 'Setting the range in column A Set Rng = Range("A2:A" & LastRow) For Each Cell In Rng Cell.Value = "*" & Cell.Value & "*" Next Cell End Sub