Excel VBA - 查找和填充值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21533462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA - Find and Fill in Values
提问by ZubaZ
Sorry for the noobish question, but I'm new and I don't have much of a programming background. I tried this on my own for a few hours, but I just don't know enough. I checked thisand this, but I couldn't figure out how to modify it enough.
很抱歉问这个菜鸟问题,但我是新手,而且我没有太多的编程背景。我自己尝试了几个小时,但我知道的还不够多。我检查了this和this,但我无法弄清楚如何对其进行足够的修改。
Example data:
示例数据:
The first part is how I get the file, the 2nd part is how I want it to look.
第一部分是我如何获取文件,第二部分是我想要它的外观。
The first 3 columns have values somewhere in the column. I need to find those values, copy them, and paste them down to the next value, then repeat all the way to the bottom of the range. Sometimes there are many values per column, sometimes only 1. The last row of the data could be determined by column 4. Basically I just need to fill in all the blank cells. Note: Row 2 does not always contain the first value.
前 3 列在列中的某处具有值。我需要找到这些值,复制它们,然后将它们粘贴到下一个值,然后一直重复到范围的底部。有时每列有很多值,有时只有 1。数据的最后一行可以由第 4 列确定。基本上我只需要填写所有空白单元格。注意:第 2 行并不总是包含第一个值。
Here's what I have so far (update):
这是我到目前为止所拥有的(更新):
Sub FindFillIn()
Dim columnValues As Range, i As Long
Dim cellstart As Integer
Dim cellend As Integer
cellstart = 2
cellend = ActiveSheet.Range("E" & ActiveSheet.Rows.Count).End(xlUp).Row
i = 1
For i = cellstart To cellend
If Cells(i, 1).Value = "" Then
Cells(i, 1).Value = Cells(i - 1, 1).Value
End If
Next i
End Sub
Update:
更新:
It appears to run correctly on the first column, but it only does one column. How do I get it to run on columns 1, 2, and 3?
它似乎在第一列上正确运行,但它只执行一列。如何让它在第 1、2 和 3 列上运行?
回答by Tim Williams
Sub FillInBlanks()
Dim rng As Range
On Error Resume Next
With ActiveSheet
Set rng = .Range(.Range("A2"), _
.Cells(Rows.Count, 4).End(xlUp)).SpecialCells(xlCellTypeBlanks)
End With
On Error GoTo 0
If Not rng Is Nothing Then
With rng
.FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
End If
End Sub
回答by Angel Diaz
This is easy without using VB. Select the data of column 1 until the last cell where you want to fill with the data. Press CTRL+G and click on Special. Select Blank and press OK. Now, on the formula bar, type "=" and select (pressing Ctrl+Click) the first Cell where the data start and then Control + Enter.
这很容易,无需使用 VB。选择第 1 列的数据,直到要填充数据的最后一个单元格。按 CTRL+G 并单击特殊。选择空白,然后按 OK。现在,在公式栏上,键入“=”并选择(按 Ctrl+单击)数据开始的第一个单元格,然后按 Control + Enter。
You can record this process to a macro and then view the code.
您可以将此过程记录到宏中,然后查看代码。