繁体   English   中英

在 col A 中找到非空单元格并将内容移动到 col B

[英]find non empty cell in col A and move contents to col B

我在excel中有两列文本,cols A和B。

我需要一个函数或 vb 代码来遍历列 A 中的每个单元格,找到一个不为空的单元格,例如 A21,然后将该单元格的内容移动到 B22。

然后从 A23 开始继续搜索。

列 A 中包含数据的行数可能长达数千。

我该如何解决这个问题?

谢谢你的帮助。

您可以通过编写 VBA 代码来解决此问题

Sub moveColAToColB()
  Dim currentSheet As Worksheet
  Set currentSheet = Sheets("Sheet1")
  Dim l As Long

  With currentSheet
    For l = 1 To.Rows.Count
      If Not IsEmpty(.Cells(l, 1).Value) Then
          .Cells(l, 2).Value = .Cells(l, 1).Value
      End If
    Next l
  End With
End Sub

编辑:我以为你在描述中犯了一个错误,但我不认为你不能对简单的代码进行简单的修改......

Sub moveColAToColB()
  Dim currentSheet As Worksheet
  Set currentSheet = Sheets("Sheet1")
  Dim l As Long

  With currentSheet
    For l = 1 To.Rows.Count
      If Not IsEmpty(.Cells(l, 1).Value) Then
          .Cells(l + 1, 2).Value = .Cells(l, 1).Value
      l = l + 1
      End If
    Next l
  End With
End Sub

见代码:

Sub MoveCellsFromColATOColB()

  For x = 21 To ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row ' here is the start range declared also used range
 
    Cells(x + 1, "B").Value = Cells(x, "A").Value
  
  Next x
  
End Sub

结果:
在此处输入图片说明

试试这个:

Sub test()

Dim lastrow As Integer

lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

Range("A1").Select

While ActiveCell.Row <= lastrow

If ActiveCell.Value = "" Then

ActiveCell.Offset(1, 0).Select

Else

ActiveCell.Offset(1, 1).Value = ActiveCell.Value

ActiveCell.Offset(2, 0).Select

End If

Wend

Range("A1").Select

End Sub

结果是这样的:

在此处输入图片说明

考虑:

Sub MoveData()
    Dim K As Long, nA As Long
    Dim i As Long

    nA = Cells(Rows.Count, "A").End(xlUp).Row
    K = 22

    For i = 1 To nA
        If Cells(i, "A").Value <> "" Then
            Cells(K, "B").Value = Cells(i, "A").Value
            K = K + 1
        End If
    Next i
End Sub

例如:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM