我需要替换在一定数量的行之后重复的某个特定行中的值。我需要多个Excel。
当前数据:
7th row: IN
16th row: IN ( comes after 9 rows)
25th row: IN ( comes after 9 rows)
我需要将这些值替换为OUT
。
我做了一些研究,似乎可以使用宏,但是我对宏并不熟悉。
任何人都可以循环使用宏或提出其他建议吗?
答案 0 :(得分:2)
每个表格都可以使用一个简单的excel公式,当然,宏也可以使用
从B2开始的excel解决方案
=IF(AND($A1 = "IN",$A1 = $A2),"Out",$A2)
此公式将复制带有修复的原始列。那么只需将B列的值复制并粘贴到A即可
VBA解决方案,您需要为其选择相关列:
Sub fixOut()
Dim cell As Object
For Each cell In Selection
If cell = "IN" AND cell = cell.OffSet(-1, 0) Then cell = "Out"
Next cell
End Sub
答案 1 :(得分:1)
对于Excel Office 365,在“视图”->“宏”->“视图宏”->下创建一个宏,您输入一个宏名称,然后按“创建”按钮。
应出现一个文本编辑器屏幕,宏应为以下内容:
Sub test_macro()
Dim searching_string As String
searching_string = "IN"
replacing_string = "OUT"
searching_column = "A"
minimum_distance_to_be_modified = 3
previous_found_row = -1
row_number = 10000
For i = 1 To row_number
If Range(searching_column + CStr(i)).Value = searching_string Then
If i - previous_found_row <= minimum_distance_to_be_modified And previous_found_row <> -1 Then
Range(searching_column + CStr(i)).Value = replacing_string
End If
previous_found_row = i
End If
Next
End Sub
设置您的searching_string
,searching_column
,minimum_distance_to_be_modified
,replacing_string
,就可以了!
我用您在代码段中找到的设置进行了测试,结果是:
希望这会对您有所帮助。
答案 2 :(得分:0)
我通过宏和VB代码解决了我的问题。
Sub Macro1()
'
' Macro1 Macro
'
Dim i As Integer
i = 7
Do While i <= Cells(Rows.Count, 1).End(xlUp).Row
Cells(i, 1).Value = "Out"
i = i + 9
Loop
End Sub
将此代码添加到新的宏中,如果有人遇到类似的问题,请运行这些宏。
答案 3 :(得分:0)
您可以尝试:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long
Dim arr As Variant
'Change target worksheet if needed
With ThisWorkbook.Worksheets("Sheet1")
'Find the last row of column A
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Set in arr the range with the data starting from row 1 to last row
arr = .Range("A1:A" & LastRow)
For i = LBound(arr) To UBound(arr) - 1
'Check if the current and the next Arr(i) are both "IN"
If arr(i, 1) = "IN" And arr(i + 1, 1) = "IN" Then
'Set the next arr(i) to OUT
arr(i + 1, 1) = "OUT"
End If
Next i
'Print the values
.Range("A1:A" & UBound(arr)).Value = arr
End With
End Sub