Excel 中的宏:如何使用两个相邻单元格(水平)的内容、自动填充和在电子表格中的某个单词处停止
Posted
技术标签:
【中文标题】Excel 中的宏:如何使用两个相邻单元格(水平)的内容、自动填充和在电子表格中的某个单词处停止【英文标题】:Macro in Excel: How to use contents of two adjacent cells (horizontal), autofill, and stop at certain word in spreadsheet 【发布时间】:2014-06-19 13:47:59 【问题描述】:我想在 Excel 中创建一个宏:
1st:查找包含某个单词的单元格。
第二个:向下一个单元格并离开两个单元格(箭头键)(从 C 列到 A 列)
3rd:选择A列中的单元格,以及B列中的相邻单元格
4th:自动填充A和B列各自的内容,直到步骤1中相同单词的下一个实例。(重复过程)....可以循环完成吗?
提前谢谢你!!!
【问题讨论】:
这不是代码编写服务。您能否展示您为解决此问题所做的工作? 【参考方案1】:这最终奏效了。从 ozgrid 的一个好人那里得到它:
Sub FindAndFill() Dim firstAdd As String,findWord As String 将 fCell 调暗为范围 暗淡 firstRow As Long、nextRow As Long、lastRow As Long
findWord = InputBox("What are we looking for?", "Search word")
If findWord = "" Then Exit Sub
Application.ScreenUpdating = False
With ActiveSheet.Range("C:C")
Set fCell = .Find(findWord)
If fCell Is Nothing Then
MsgBox findWord & " was not found.", vbOKOnly, "Not found"
Exit Sub
End If
'Define our limits
firstAdd = fCell.Address
lastRow = ActiveSheet.UsedRange.Rows.Count
Do
firstRow = fCell.Row
Set fCell = .FindNext(fCell)
If fCell.Address = firstAdd Then
nextRow = lastRow
Else
nextRow = fCell.Row - 1
End If
Range(Cells(firstRow + 1, "A"), Cells(nextRow, "B")).FillDown
Loop Until nextRow = lastRow
End With
Application.ScreenUpdating = True
结束子
【讨论】:
【参考方案2】:我认为这是你想要的:
Sub stack()
Dim certain_word As String 'this will be your word
Dim count1, count2, count3, count4 As Long 'counts to move through loop
certain_word = "a"
'if you wanted to find the word "a". you will have to change this
count1 = Application.CountA(Range("A:A"))
'this will find the number of rows, assuming row "A" will provide an accurate count
count2 = 1
count3 = count2 + 1
While count4 < count1
If Range("C" & CStr(count2)).Value = certain_word Then
If Range("C" & CStr(count3)) <> certain_word Then
If Range("B" & CStr(count3)) <> "" Then
Range("A" & CStr(count3)) = Range("A" & CStr(count2))
Range("B" & CStr(count3)) = Range("B" & CStr(count2))
End If
count3 = count3 + 1
count4 = count4 + 1
Else
count2 = count2 + 1
count3 = count2 + 1
End If
Else
count2 = count2 + 1
count3 = count2 + 1
count4 = count4 + 1
End If
Wend
End Sub
输入在右边,输出在左边。让我知道这是否正确。
1 10 a 1 10 a
1 10 b 23 1 b
1 10 c 34 1 c
25 13 a 25 13 a
8 14 a 8 14 a
8 14 c 7 15 c
【讨论】:
以上是关于Excel 中的宏:如何使用两个相邻单元格(水平)的内容、自动填充和在电子表格中的某个单词处停止的主要内容,如果未能解决你的问题,请参考以下文章
用于合并 Excel 中与其他列中的信息匹配的行的单元格的宏
使用 CSS Grid 和 Flexbox 水平对齐位于不同网格单元格中的元素