我有2列,B和F.我试图将Range(" B:B")的语音值输入Range(" F:F" )。它显示测试B中的每个细胞,但结果没有显示在相应的细胞中。
Sub test()
Dim rng As Range
Dim i As Long
Set rng = Range("B:B")
i = 1
Do Until IsEmpty(Cells(i, 2))
If Cells(i, 2).Value <> "" Then
ActiveCell.Offset(0, 4).Value = rng.Phonetic.Text
End If
i = i + 1
Loop
End Sub
我还计划测试列F是否为空,如果不为空则再进行拼音。如下图所示。
Sub test()
Dim rng As Range
Dim i As Long
Set rng = Range("B:B")
i = 1
Do Until IsEmpty(Cells(i, 2))
If Cells(i, 2).Value <> "" Then
If Cells(i, 4).Value <> "" Then
ActiveCell.Offset(0, 4).Value = rng.Phonetic.Text
End If
End If
i = i + 1
Loop
End Sub
非常感谢你。
答案 0 :(得分:1)
ActiveCell.Offset(0, 4)
的目的之后已编辑
循环浏览范围的非空白单元格,您可以使用Range
对象的Specialcells()
方法
以下示例循环遍历列&#34; B&#34;具有常数值的单元格(即不能从公式中得出):
Dim cell As Range
For Each cell In Range("B:B").SpecialCells(xlCellTypeConstants)
If cell.Offset(0, 6).Text = "" Then cell.Offset(0, 6).Value = cell.Phonetic.Text '<--| get cells with 'constant' values
Next cell
如果您想将过滤范围缩小到常量文本值单元格,请添加xlTextvalues
参数:
Dim cell As Range
For Each cell In Range("B:B").SpecialCells(xlCellTypeConstants, xlTextValues) '<--| get cells with 'constant' text values
If cell.Offset(0, 6).Text = "" Then cell.Offset(0, 6).Value = cell.Phonetic.Text
Next cell
如果您的单元格中填充了公式,那么您希望使用xlCellTypeFormulas
作为第一个参数,始终使用xlTextValues
第二个参数
答案 1 :(得分:0)
好的,经过反复试验。我想通了(可能有更好的方法)。
Sub test()
'Dim rng As Range
Dim i As Long
'Set rng = Range("B:B")
i = 1
Do Until IsEmpty(Cells(i, 2))
If Cells(i, 2).Value <> "" Then
If Cells(i, 6).Value = "" Then
Cells(i, 2).Offset(0, 4).Value = Cells(i, 2).Phonetic.Text
End If
End If
i = i + 1
Loop
End Sub