0

我正在尝试在第二张表中查找并匹配一个值,一旦找到,我想替换位于匹配值旁边的相邻值。

我希望只用一个 InputBox 来完成。我设法拼凑了以下代码,这对我有用,但它要求我自己在第二张纸上找到匹配的值。我试图操纵它来自动找到第二个值,但是我在这样做时遇到了麻烦。

以下是示例值

ReplaceRng (sheet1) : https://imgur.com/d59NDg5

Name = RandomName
Value1 = 27
Value2 = 29
Value3 = 31

InputRng (sheet2) : https://imgur.com/iiSTtrw

ReplaceRng Name = RandomName
ReplaceRng Value1 = 25
ReplaceRng Value2 = 22
ReplaceRng Value3 = 25

所以如果我使用这段代码,我可以找到并替换这些值,但它需要两个输入框:

Sub ReplaceRange 
Dim rng As Range  
Dim InputRng As Range, ReplaceRng As Range 
xTitleId = "ReplaceRange"   

'这部分是我输入的地方

Set InputRng = Application.Selection 
Set InputRng = Application.InputBox("Original Range ", xTitleId,    
InputRng.Address, Type:=8)
Set ReplaceRng = Application.InputBox("Replace Range :", xTitleId, Type:=8)
Application.ScreenUpdating = False

'这是 InputRng 为范围内的每个值分配一个变量名的地方

For Each rowInputRng In InputRng.Rows
Dim Name As String, Value1 As Integer, Value2 As Integer, Value3 As Integer
Name = InputRng.Cells(1).Value
Value1 = InputRng.Cells(2).Value
Value2 = InputRng.Cells(3).Value
Value3 = InputRng.Cells(4).Value

'如果替换范围内的第一个单元格与输入范围内的相同,则替换相邻的单元格

For Each Row In ReplaceRngRng.Rows
If Row.Cells(1).Value = Name Then
Row.Cells(2).Value = Value1
Row.Cells(3).Value = Value2
Row.Cells(4).Value = Value3

End If
Next Row  
Next rowInputRng  
End Sub

我当前的代码看起来像这样,或者至少是我从上面找到的代码中更改的内容,但是它似乎不起作用,我目前想不出改变的方法。我已经以几种不同的方式进行了尝试,但我似乎无法自动找到第二个范围:

'寻找价值

xTitleId = "RangeValueReplace"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Original Range ", xTitleId,       
InputRng.Address, Type:=8)
Set ReplaceRng =     
Application.WorksheetFunction.VLookup("InputRng.Cells(1).Value", 
"Sheet1!A1:A1000", 1, 0))

'替换相邻单元格

For Each Row In ReplaceRng.Rows
If ReplaceRng.Cell(1).Value = Name Then
ReplaceRng.Cells(1).Offset(, 1).Value = Value1
ReplaceRng.Cells(1).Offset(, 2).Value = Value2
ReplaceRng.Cells(1).Offset(, 3).Value = Value3
End If
Next Row

对于大多数错误,我得到“语法错误”或运行时错误“1004”无法获取 WorksheetFunction 类的 Vlookup 属性

4

2 回答 2

0

使用“ReplacingRng.Cells(1)”时如何使用这种格式...单元格(行,列)。例子...

For Each Row In ReplaceRng.Rows
    If ReplacingRng.Cell(row,1).Value = Name Then
        ReplacingRng.Cells(row,2).value = ValueWhatever
    End if
Next
于 2019-02-17T15:29:52.087 回答
0

所以我最终找到了解决我的问题的方法。请注意,您使用的 ActiveCell 很重要。

Sub Findreplace()

Dim InputRng As Range, ReplaceRng As Range

            xTitleId = "Findandreplace"

            Set InputRng = Application.Selection

            Set InputRng = Application.InputBox("Original Range ", xTitleId, 
            InputRng.Address, Type:=8)


            vFind = ActiveCell


            On Error Resume Next

                    With Sheet1

             Set rFound = .UsedRange.Find(What:=vFind, After:=.Cells(1, 1), 
            LookIn:=xlFormulas, LookAt:= _xlPart, SearchOrder:=xlByRows, 
                                 SearchDirection:=xlNext, MatchCase:=False)

                        If Not rFound Is Nothing Then

                    Application.Goto rFound, True

             End If

             End With


            Set ReplaceRng = rFound

            Application.ScreenUpdating = False


            Dim Name As String, Value1 As Integer, Value2 As Integer, Value3 As 
    Integer

                Name = InputRng.Cells(1).Value

                Value1 = InputRng.Cells(2).Value

                Value2 = InputRng.Cells(3).Value

                Value3 = InputRng.Cells(4).Value

                Value4 = InputRng.Cells(5).Value



            If ReplaceRng.Value = Name Then

                ReplaceRng.Offset(, 1).Value = Value1

                ReplaceRng.Offset(, 2).Value = Value2

                ReplaceRng.Offset(, 3).Value = Value3

                ReplaceRng.Offset(, 4).Value = Value4

            End If


End Sub
于 2019-02-21T16:19:34.490 回答