首页 > 解决方案 > 从行上的单元格值添加前缀

问题描述

我正在创建一个宏循环,该循环从 A 列获取值并将其作为前缀添加到该行 D 列之后的其余单元格。当它到达一个空单元格时,它会转到下一行并重复该过程,直到 A 列单元格为空。我已经使用了适用于第一行的代码,但我似乎无法让它循环到其他行。

Sub FLOC

Dim I as Integer 
Dim j as Integer
I=4
j=1
 'Check that Column A is not empty to stop the Loop
 While Not IsEmpty(Cells(j, 1))

  If Not IsEmpty(Cells(j,i)) Then
  'Select Column D in that row
  Cells(j, i).Select
 'Add the prefix from Column A to the rest of the Cells on the row
 ActiveCell.Value = Cells(1, j).Value & ActiveCell.Value
 i = i + 1

  'When a empty cell is reached move the ActiveCell to next row, Column D.
  Else
  i = 4
  j = j + 1

   Endif

   Wend

   Sub End      

对正确道路的任何帮助将不胜感激。

标签: excelvbaloopsprefix

解决方案


感谢您帮助我解决了这个问题

Sub FLO2 ()        
Dim i As Double
Dim j As Double
Dim FLOC As String
j = 1
i = 4
FLOC = Cells(j, 1)

While Not IsEmpty(FLOC)

  If Not IsEmpty(Cells(j, i)) Then
  Cells(j, i).Select
  ActiveCell.Value = Cells(j, 1).Value & ActiveCell.Value
  i = i + 1
  Else
  i = 4
  j = j + 1
  End If

Wend
Sub End                                                                                           

推荐阅读