![](/img/trans.png)
[英]How to fill empty cells in Col B based on relationship between filled cells in Col A and 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 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
结果是这样的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.