如何在相应的相邻单元格中打印循环结果?

我有2列,B和F.我试图将Range(“B:B”)的语音值设置为Range(“F:F”)。 它显示testingB中的每个单元格,但结果没有显示到相应的单元格中。

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 

我也打算testingF列是否为空,如果不是空的话就得到拼音。 如下所示。

 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 

非常感谢你提前。

在OP澄清ActiveCell.Offset(0, 4)意图之后进行编辑

要通过一个范围的非空白单元格循环,您可以使用Range对象的Specialcells()方法

以下示例循环遍历列“B”单元格与数值(即不是从公式派生):

 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第二个

好的,经过反复试验。 我想通了(可能有更好的方法)。

 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