简体   繁体   English

Excel宏列自动填充

[英]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 做这个

  1. Select Col C 选择列C
  2. Press Ctrl + G Ctrl + G
  3. Click on Special 点击Special
  4. Next Click on Blanks 下一步单击Blanks
  5. Click Ok 点击确定
  6. All Empty cells are now selected. 现在选择了所有空单元格。 Press the = key and then the Up arrow key =键,然后按向上箭头键
  7. Lastly press Ctrl + Tab + Enter and you are done. 最后按Ctrl + Tab + Enter ,您就完成了。

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.

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