当必须使用同一列中的不同值填充单元格时,如何填充此列中的空白单元格?
How to fill in blank cells in this column, when the cells have to be populated with different values from this same column?
我一直在为我目前面临的问题绞尽脑汁。
I have a file that looks like this
我怎样才能做到这一点(使用 VBA)以便列 (B) 填充第一个值 (P),直到达到下一个值 (R),然后继续填充它 (R),直到达到下一个值,依此类推,直到 (C) 列中的值结束? (如自动填充)
(B)列中的值可以是(P)或(R),但每次都不相同。以及行数。
Should look like this (blank rows can be deleted)
我已经尝试了一些方法来让它发挥作用。
首先,我这样编码:
Dim nb As Workbook
nb.Sheets(2).Select
Range("C2").Select
i = 2
Do Until IsEmpty(Range("C" & i))
If InStr(1, Range("C" & i), "") Then
nb.Sheets(2).Range("B" & i) = nb.Sheets(2).Range("B2")
End If
i = i + 1
Loop
正如预期的那样,它只填充了该列的第一部分:
Like this:
然后我尝试在代码中添加更多内容:
nb.Sheets(2).Select
Range("C2").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
i = ActiveCell
Dim inD As String
ActiveCell.Offset(0, -1).Select
inD = ActiveCell
ActiveCell.Offset(0, 1).Select
Do Until IsEmpty(Range("C" & i))
If InStr(1, Range("C" & i), "") Then
nb.Sheets(2).Range("B" & i) = inD
End If
i = i + 1
Loop
但正如预期的那样,这不起作用。看来您无法使用 ActiveCell 将单元格位置分配给 i。现在我收到“运行时错误 1004:对象的方法范围 - 'Global' 失败”
我也试过使用录音自动填充,但也没用。
在此问题上,我将不胜感激!
您可以按照以下步骤操作:
- Select 整个列 B
- 按Ctrl+G启动“Goto”菜单,选择“Special”。
- 选择“空白”
- 在公式栏中单击鼠标并键入
=B2
- 按 Ctrl+ENTER
如果您想在宏中使用它,只需将其录制即可。
试试那个代码。您将获得与 Dominique 过程类似的输出,但它也会删除空白行。
Sub test()
Dim sht As Worksheet: Set sht = ActiveSheet
Dim LastRow As Long
Dim myRng As Range
'Find last row with data in sheet
LastRow = sht.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'Set Range to fill in with values
Set myRng = sht.Range("B2", "B" & LastRow - 1)
'Loop through every cell in range and paste value from previous cell
For Each cell In myRng
If Not cell = "" Then
If cell.Offset(1, 0) = "" And cell.Offset(1, 1) <> "" Then
cell.Offset(1, 0).Value = cell.Value
Else
GoTo nextIteration
End If
End If
nextIteration:
Next cell
'delete empty rows
For x = LastRow To 1 Step -1
If WorksheetFunction.CountA(Rows(x)) = 0 Then
sht.Rows(x).Delete
End If
Next
End Sub
我一直在为我目前面临的问题绞尽脑汁。
I have a file that looks like this
我怎样才能做到这一点(使用 VBA)以便列 (B) 填充第一个值 (P),直到达到下一个值 (R),然后继续填充它 (R),直到达到下一个值,依此类推,直到 (C) 列中的值结束? (如自动填充)
(B)列中的值可以是(P)或(R),但每次都不相同。以及行数。
Should look like this (blank rows can be deleted)
我已经尝试了一些方法来让它发挥作用。 首先,我这样编码:
Dim nb As Workbook
nb.Sheets(2).Select
Range("C2").Select
i = 2
Do Until IsEmpty(Range("C" & i))
If InStr(1, Range("C" & i), "") Then
nb.Sheets(2).Range("B" & i) = nb.Sheets(2).Range("B2")
End If
i = i + 1
Loop
正如预期的那样,它只填充了该列的第一部分:
Like this:
然后我尝试在代码中添加更多内容:
nb.Sheets(2).Select
Range("C2").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
i = ActiveCell
Dim inD As String
ActiveCell.Offset(0, -1).Select
inD = ActiveCell
ActiveCell.Offset(0, 1).Select
Do Until IsEmpty(Range("C" & i))
If InStr(1, Range("C" & i), "") Then
nb.Sheets(2).Range("B" & i) = inD
End If
i = i + 1
Loop
但正如预期的那样,这不起作用。看来您无法使用 ActiveCell 将单元格位置分配给 i。现在我收到“运行时错误 1004:对象的方法范围 - 'Global' 失败”
我也试过使用录音自动填充,但也没用。
在此问题上,我将不胜感激!
您可以按照以下步骤操作:
- Select 整个列 B
- 按Ctrl+G启动“Goto”菜单,选择“Special”。
- 选择“空白”
- 在公式栏中单击鼠标并键入
=B2
- 按 Ctrl+ENTER
如果您想在宏中使用它,只需将其录制即可。
试试那个代码。您将获得与 Dominique 过程类似的输出,但它也会删除空白行。
Sub test()
Dim sht As Worksheet: Set sht = ActiveSheet
Dim LastRow As Long
Dim myRng As Range
'Find last row with data in sheet
LastRow = sht.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'Set Range to fill in with values
Set myRng = sht.Range("B2", "B" & LastRow - 1)
'Loop through every cell in range and paste value from previous cell
For Each cell In myRng
If Not cell = "" Then
If cell.Offset(1, 0) = "" And cell.Offset(1, 1) <> "" Then
cell.Offset(1, 0).Value = cell.Value
Else
GoTo nextIteration
End If
End If
nextIteration:
Next cell
'delete empty rows
For x = LastRow To 1 Step -1
If WorksheetFunction.CountA(Rows(x)) = 0 Then
sht.Rows(x).Delete
End If
Next
End Sub