[英]excel macro column auto-fill
I have a spreadsheet which I am cleaning and using macros to help. 我有一个电子表格,正在清理并使用宏来提供帮助。 My column 'C' has temperature data.
我的列“ C”具有温度数据。 Like with all data, there is some missing.
像所有数据一样,有一些缺失。 How would I write a macro that would auto-fill the missing spot with previous data?
我将如何编写一个宏,用以前的数据自动填充缺失的位置?
For example: 例如:
C C
1 37 1 37
2 35 2 35
3 --------> 3 35
4 37 4 37
5 36 5 36
The spot C3 has been filled with C2's data. 点C3已填充了C2的数据。
Thank you for your help. 谢谢您的帮助。
Do you really need VBA for this? 您真的需要VBA吗?
Do this 做这个
Special
Special
Blanks
Blanks
ScreenShot 截图
Give this a try: 试试看:
Sub FixC()
Dim N As Long, i As Long
N = Cells(Rows.Count, "C").End(xlUp).Row
For i = 2 To N
If Cells(i, "C") = "" Then
Cells(i, "C") = Cells(i - 1, "C")
End If
Next i
End Sub
How would I write that macro: 我将如何编写该宏:
This contains only snippets. 仅包含摘要。
loop over all cells in the column: 循环查看列中的所有单元格:
for each cell in ActiveSheet.Columns(1).Cells
if the cell value is not empty -> save the value to a variable 如果单元格值不为空->将值保存到变量
If cell.value <> Empty then lastCellValue = cell.value
if the cell value is empty -> write the saved cell value into the cell 如果单元格值为空->将保存的单元格值写入单元格
Else cell.value = lastCellValue
also: 也:
if more than x (eg 20) cells in a row were empty, break from loop 如果连续超过x个(例如20个)单元格为空,则从循环中中断
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.