简体   繁体   English

搜索具有特定文本的所有单元格的范围并将所有相邻单元格的值更改为 0

[英]Search range for all cells with specific text and change the value of all adjacent cell to 0

Looking for help to achieve searching a range of cells E9:E with All cells containing "Accommodation & Transportation" and changing the value of the cells adjacent to them with 0. , I was not able to get anything online with similar topic and I'm not too good with VBA coding, though i am able to understand what the code will provide in results.寻求帮助以实现搜索一系列单元格 E9:E 与所有包含“住宿和运输”的单元格并将与它们相邻的单元格的值更改为 0. ,我无法在网上获得任何类似主题的内容,我m VBA 编码不太好,尽管我能够理解代码将在结果中提供什么。

I Have a Commandbutton1 with the below code:我有一个带有以下代码的 Commandbutton1:

Sub CommandButton1_click()

Dim blanks As Excel.Range

Set blanks = Range("F9:F" & Cells(Rows.Count, 5).End(xlUp).Row).SpecialCells(xlCellTypeBlanks)

blanks.Value = blanks.Offset(0, -1).Value

End Sub

Further i have a command button that will select only cells that are not blank.此外,我有一个命令按钮,它将 select 仅非空白单元格。 I need the above result because if the below code selects Non Blank cells from Columns E:F it wont be selecting cells adjacent to those containing "Accommodation & Transportation" as they are blank cells and it will return the error "Runtime Error '1004' This action wont work on multiple selections".我需要上面的结果,因为如果下面的代码从 E:F 列中选择非空白单元格,它将不会选择与包含“住宿和运输”的单元格相邻的单元格,因为它们是空白单元格,它将返回错误“运行时错误'1004'此操作不适用于多项选择”。

The below code acts the same as [Go to Special => Constants]下面的代码与 [Go to Special => Constants] 的作用相同

Sub SelectNonBlankCells()

Dim rng As Range
Dim OutRng As Range
Dim InputRng As Range
Dim xTitle As String


On Error Resume Next

xTitle = Application.ActiveWindow.RangeSelection.Address

Set InputRng = Range("E8:F500")

ActiveWindow.ScrollRow = 1

For Each rng In InputRng

If Not rng.Value = "" Then

If OutRng Is Nothing Then

Set OutRng = rng

Else

Set OutRng = Application.Union(OutRng, rng)

End If

End If

Next

If Not (OutRng Is Nothing) Then

OutRng.Select

End If

End Sub

Maybe you can try another approach, if your goal is to edit cells adjacent to certain cells.如果您的目标是编辑与某些单元格相邻的单元格,也许您可以尝试另一种方法。 The code below is based on an example in the Help file of the Range.Find function:以下代码基于Range.Find function 帮助文件中的示例:

Sub DoSomething()

    Dim sh As Worksheet
    Set sh = ActiveSheet
    
    Dim checkRange As Range
    Set checkRange = sh.Range("E8:F500") ' your intended range to search
    
    Dim foundRange As Range
    Set foundRange = checkRange.Find("Accommodation & Transportation")
    
    Dim firstAddr As String
    
    If Not foundRange Is Nothing Then
    
        firstAddr = foundRange.Address
        Do
        
            ' use foundRange to access adjacent cells with foundRange.Offset(row, col)
            '
            '
            foundRange.Offset(0, 1) = "all good"
            
            Set foundRange = checkRange.FindNext(foundRange)
            
        Loop While Not foundRange Is Nothing And foundRange.Address <> firstAddr
    End If

End Sub

Or even better, you could add some parameters to make it more reusable:甚至更好的是,您可以添加一些参数以使其更可重用:

Sub Main()

    DoSomething "Accommodation & Transportation", ActiveSheet.Range("E8:F500")

End Sub


Sub DoSomething(ByVal findWhat As String, ByVal searchWhere As Range)

    Dim foundRange As Range
    Set foundRange = searchWhere.Find(findWhat)
    
    Dim firstAddr As String
    
    If Not foundRange Is Nothing Then
    
        firstAddr = foundRange.Address
        Do
        
            ' use foundRange to access adjacent cells with foundRange.Offset(row, col)
            '
            '
            foundRange.Offset(0, 1) = "all good"
            
            Set foundRange = searchWhere.FindNext(foundRange)
            
        Loop While Not foundRange Is Nothing And foundRange.Address <> firstAddr
    End If

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想列出相邻单元格值具有的范围内的所有值:1)时间差小于24小时,其他单元格2)等于特定文本 - I want to list all values in a range where adjacent cell value have:1) time difference is less than 24hour, and other cells 2) equal specific text 在工作表中搜索单元格值,然后将相邻单元格复制到变量范围中 - Search a worksheet for a cell value, then copy the adjacent cells into a variable range 如果在单元格上找到特定值,则删除范围内的所有单元格 - VBA - Delete all cells in a range if a specific value is found on a cell - VBA Excel VBA-将单元格值设置为与所有非空白单元格相邻的变量 - Excel VBA - Set Cell Value to Variable Adjacent to All Cells that Are Not Blank 搜索字符串范围,更改相邻单元格的值 - Search range for string, change values of adjacent cells 查找具有特定字符串值的范围内的所有单元格 - Find all cells in a range with a specific string value 当范围内的所有像元都等于相同值时更改像元的值 - Changing the value of a cell when all cells in a range equal the same value 查找特定的单元格,更改相邻单元格的值,该值取决于位置(Excel for Mac,15.6) - Find specific cells, change value of adjacent cell, value depends on location (Excel for Mac, 15.6) 在不同的单元格中搜索文本并使用 Excel 在一个单元格中添加所有结果 - Search text in different cells and add all results in a cell using Excel 根据相邻像元值锁定行中像元的范围 - Lock range of Cells in row based on adjacent cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM