我敢肯定,这很简单,只是在网上找不到任何东西。
我正在编写一个宏以格式化从第三方应用程序下载的XL电子表格。它们的格式都古怪,所以我正尝试使从它们中获取所需数据的过程变得更加容易。
这是一个简单的VBA Do Loop,它导致BL列中的单元更新。这些单元格中的数据包含换行符,除非您双击该单元格,否则这些换行符不会显示。下面的VBA导致对单元的更新,只需较少的工作即可达到相同的效果。但是,它当前使excel崩溃,我不知道为什么。它在单个实例中有效,但是当我循环时-BOOM!-冻结 任何帮助将不胜感激。
Sub updateCell()
Dim currentValue As String
ActiveSheet.Range("BL1").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
currentValue = ActiveCell().Value
ActiveCell().Value = currentValue & ""
Loop
End Sub
尝试一些更直接的方法:
With ActiveSheet
lrow = .Range("BL" & .Rows.Count).End(xlUp).Row '~~> find last row on BL
With .Range("BL1:BL" & lrow) '~~> work on the target range
.Value = .Value '~~> assign its current value to it
End With
End With
上面的代码就像手动按F2然后按Enter。
Edit1:关于获取最后一行的说明
ActiveSheet.Rows.Count '~~> Returns the number of rows in a sheet which is 1048576
MsgBox ActiveSheet.Rows.Count '~~> run this to confirm
因此,该行实际上将BL连接到1048576。
.Range("BL" & .Rows.Count) '~~> Count is a property of the Rows Collection
与...一样:
.Range("BL" & 1048576)
和:
.Range("BL1048576")
然后,使用最后一个范围对象结束方法到达最后一行。
.Range("BL" & .Rows.Count).End(xlUp)
因此,基本上,上述代码转到单元格BL1048576,然后像手动按Ctrl+一样Arrow Up。
要返回范围的实际行号,我们使用Range Object Row属性。
lrow = .Range("BL" & .Rows.Count).End(xlUp).Row
在这里查看有关With语句的更多信息。
它具有与循环相同的效果(与您的代码相同)。高温超导
但是,如果您要删除单元格上+产生的换行符,请尝试以下操作:AltEnter
Dim lrow As Long, c As Range
With ActiveSheet
lrow = .Range("BL" & .Rows.Count).End(xlUp).Row
For Each c In .Range("BL1:BL" & lrow)
c.Value = Replace(c.Value, Chr(10), "")
Next
End With
其中Chr(10)
的等效换行符是""
使用替换功能替换的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句