Copy excel cell data till the nearest filled cell in column

会有一股神秘感。 提交于 2019-12-02 11:51:35

问题


I am a total newbie to Excel, and there's a bit of problem I am facing.

I have an Excel sheet that I have to import into another program of mine. Please consider this format:

Heading1 |  Sub-heading1  |   Sub-Sub-heading1 
         |                |   Sub-Sub-heading2 
         |                |   Sub-Sub-heading3 
         |                |   Sub-Sub-heading4 
         |  Sub-heading2  |   Sub-Sub-heading1 
         |                |   Sub-Sub-heading2 
         |                |   Sub-Sub-heading3 
         |                |   Sub-Sub-heading4 
Heading2 |  Sub-heading1  |   Sub-Sub-heading1
and so on..

The problem is that for my import purpose I cannot leave any cells blanks. So, I wanted some forumla through which Heading1 can be copied to all the cells above Heading2, and similar for column2 and all the data appearing afterwards in all the rows.

This would be great help, since it would remove the roadblock for me.


回答1:


This can be done very quick either manually (no code) or programmatically (simple code) using SpecialCells which has a xlBlanks collection

1. Manually

  • Select the cells in your first two columns
  • Press F5 ... Special ...Blanks
  • In the formula bar type =A1 where A1 is the first cell above your new selection of blanks
  • Hold the Ctrl key and press Enter to enter this formula in all the blank cells at once

This is written up well here including instructions as to how to convert these formulae to values

2. VBA

Three samples are provided here

This modified version of the second code would work on columns A:B (which appears to be your data layout)

Sub FillColBlanks_Offset()
'by Rick Rothstein  2009-10-24
'fill blank cells in column with value above
'http://www.contextures.com/xlDataEntry02.html
'modified by brettdj
Dim Area As Range, LastRow As Long
On Error Resume Next
With Columns("A:B")
    LastRow = .Find(What:="*", SearchOrder:=xlRows, _
                    SearchDirection:=xlPrevious, _
                    LookIn:=xlFormulas).Row
    For Each Area In .Resize(LastRow). _
        SpecialCells(xlCellTypeBlanks).Areas
        Area.Value = Area(1).Offset(-1).Value
    Next
End With
End Sub


来源:https://stackoverflow.com/questions/9450499/copy-excel-cell-data-till-the-nearest-filled-cell-in-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!