VBA复制粘贴字符串搜索
我似乎不知道如何编写一个vba代码,搜索单元格C10:G10以找到等于单元格A10的匹配项,一旦找到,将A14:A18范围复制到匹配的单元格,但在下面,例如F14:F18(见图) 下面的宏VBA复制粘贴字符串搜索,vba,excel,Vba,Excel,我似乎不知道如何编写一个vba代码,搜索单元格C10:G10以找到等于单元格A10的匹配项,一旦找到,将A14:A18范围复制到匹配的单元格,但在下面,例如F14:F18(见图) 下面的宏 'Copy Range("A14:A18").Select Selection.Copy 'Paste Range("F14:F18").Select ActiveSheet.Paste! 试试这个: With Sheets("SheetName") ' Change to your actual shee
'Copy
Range("A14:A18").Select
Selection.Copy
'Paste
Range("F14:F18").Select
ActiveSheet.Paste!
试试这个:
With Sheets("SheetName") ' Change to your actual sheet name
Dim r As Range: Set r = .Range("C10:G10").Find(.Range("A10").Value2, , , xlWhole)
If Not r Is Nothing Then r.Offset(4, 0).Resize(5).Value2 = .Range("A14:A18").Value2
End With
范围对象必须帮助您查找范围内的值。然后返回与搜索条件匹配的范围对象。
要将值设置到正确的位置,只需使用
偏移和调整大小方法
Edit1:回答OP的评论
要查找范围内的公式,需要将LookIn
参数设置为xlFormulas
Set r = .Range("C10:G10").Find(What:=.Range("A10").Formula, _
LookIn:=xlFormulas, _
LookAt:=xlWhole)
上面的代码使用与单元格A10完全相同的公式查找范围。尝试以下操作:
With Sheets("SheetName") ' Change to your actual sheet name
Dim r As Range: Set r = .Range("C10:G10").Find(.Range("A10").Value2, , , xlWhole)
If Not r Is Nothing Then r.Offset(4, 0).Resize(5).Value2 = .Range("A14:A18").Value2
End With
范围对象必须帮助您查找范围内的值。然后返回与搜索条件匹配的范围对象。
要将值设置到正确的位置,只需使用
偏移和调整大小方法
Edit1:回答OP的评论
要查找范围内的公式,需要将LookIn
参数设置为xlFormulas
Set r = .Range("C10:G10").Find(What:=.Range("A10").Formula, _
LookIn:=xlFormulas, _
LookAt:=xlWhole)
上面的代码使用与单元格A10完全相同的公式查找范围。尝试以下操作:
With Sheets("SheetName") ' Change to your actual sheet name
Dim r As Range: Set r = .Range("C10:G10").Find(.Range("A10").Value2, , , xlWhole)
If Not r Is Nothing Then r.Offset(4, 0).Resize(5).Value2 = .Range("A14:A18").Value2
End With
范围对象必须帮助您查找范围内的值。然后返回与搜索条件匹配的范围对象。
要将值设置到正确的位置,只需使用
偏移和调整大小方法
Edit1:回答OP的评论
要查找范围内的公式,需要将LookIn
参数设置为xlFormulas
Set r = .Range("C10:G10").Find(What:=.Range("A10").Formula, _
LookIn:=xlFormulas, _
LookAt:=xlWhole)
上面的代码使用与单元格A10完全相同的公式查找范围。尝试以下操作:
With Sheets("SheetName") ' Change to your actual sheet name
Dim r As Range: Set r = .Range("C10:G10").Find(.Range("A10").Value2, , , xlWhole)
If Not r Is Nothing Then r.Offset(4, 0).Resize(5).Value2 = .Range("A14:A18").Value2
End With
范围对象必须帮助您查找范围内的值。然后返回与搜索条件匹配的范围对象。
要将值设置到正确的位置,只需使用
偏移和调整大小方法
Edit1:回答OP的评论
要查找范围内的公式,需要将LookIn
参数设置为xlFormulas
Set r = .Range("C10:G10").Find(What:=.Range("A10").Formula, _
LookIn:=xlFormulas, _
LookAt:=xlWhole)
上述代码查找的范围与单元格A10的公式完全相同。其他附加变量 1.对每个循环使用
Sub test()
Dim Cl As Range, x&
For Each Cl In [C10:G10]
If Cl.Value = [A10].Value Then
x = Cl.Column: Exit For
End If
Next Cl
If x = 0 Then
MsgBox "'" & [A10].Value & "' has not been found in range 'C10:G10'!"
Exit Sub
End If
Range(Cells(14, x), Cells(18, x)).Value = [A14:A18].Value
End Sub
2.使用Find
方法(L42已发布,但有点不同)
3.使用WorksheetFunction.Match
Sub test2()
Dim Cl As Range, x&
On Error Resume Next
x = WorksheetFunction.Match([A10], [C10:G10], 0) + 2
If Err.Number > 0 Then
MsgBox "'" & [A10].Value2 & "' has not been found in range 'C10:G10'!"
Exit Sub
End If
[A14:A18].Copy Range(Cells(14, x), Cells(18, x))
End Sub
另一个附加变体
1.对每个循环使用
Sub test()
Dim Cl As Range, x&
For Each Cl In [C10:G10]
If Cl.Value = [A10].Value Then
x = Cl.Column: Exit For
End If
Next Cl
If x = 0 Then
MsgBox "'" & [A10].Value & "' has not been found in range 'C10:G10'!"
Exit Sub
End If
Range(Cells(14, x), Cells(18, x)).Value = [A14:A18].Value
End Sub
2.使用Find
方法(L42已发布,但有点不同)
3.使用WorksheetFunction.Match
Sub test2()
Dim Cl As Range, x&
On Error Resume Next
x = WorksheetFunction.Match([A10], [C10:G10], 0) + 2
If Err.Number > 0 Then
MsgBox "'" & [A10].Value2 & "' has not been found in range 'C10:G10'!"
Exit Sub
End If
[A14:A18].Copy Range(Cells(14, x), Cells(18, x))
End Sub
另一个附加变体
1.对每个循环使用
Sub test()
Dim Cl As Range, x&
For Each Cl In [C10:G10]
If Cl.Value = [A10].Value Then
x = Cl.Column: Exit For
End If
Next Cl
If x = 0 Then
MsgBox "'" & [A10].Value & "' has not been found in range 'C10:G10'!"
Exit Sub
End If
Range(Cells(14, x), Cells(18, x)).Value = [A14:A18].Value
End Sub
2.使用Find
方法(L42已发布,但有点不同)
3.使用WorksheetFunction.Match
Sub test2()
Dim Cl As Range, x&
On Error Resume Next
x = WorksheetFunction.Match([A10], [C10:G10], 0) + 2
If Err.Number > 0 Then
MsgBox "'" & [A10].Value2 & "' has not been found in range 'C10:G10'!"
Exit Sub
End If
[A14:A18].Copy Range(Cells(14, x), Cells(18, x))
End Sub
另一个附加变体
1.对每个循环使用
Sub test()
Dim Cl As Range, x&
For Each Cl In [C10:G10]
If Cl.Value = [A10].Value Then
x = Cl.Column: Exit For
End If
Next Cl
If x = 0 Then
MsgBox "'" & [A10].Value & "' has not been found in range 'C10:G10'!"
Exit Sub
End If
Range(Cells(14, x), Cells(18, x)).Value = [A14:A18].Value
End Sub
2.使用Find
方法(L42已发布,但有点不同)
3.使用WorksheetFunction.Match
Sub test2()
Dim Cl As Range, x&
On Error Resume Next
x = WorksheetFunction.Match([A10], [C10:G10], 0) + 2
If Err.Number > 0 Then
MsgBox "'" & [A10].Value2 & "' has not been found in range 'C10:G10'!"
Exit Sub
End If
[A14:A18].Copy Range(Cells(14, x), Cells(18, x))
End Sub
给你
Sub DoIt()
Dim rng As Range, f As Range
Dim Fr As Range, Crng As Range
Set Fr = Range("A10")
Set Crng = Range("A14:A18")
Set rng = Range("C10:G19")
Set f = rng.Find(what:=Fr, lookat:=xlWhole)
If Not f Is Nothing Then
Crng.Copy Cells(14, f.Column)
Else: MsgBox "Not Found"
Exit Sub
End If
End Sub
给你
Sub DoIt()
Dim rng As Range, f As Range
Dim Fr As Range, Crng As Range
Set Fr = Range("A10")
Set Crng = Range("A14:A18")
Set rng = Range("C10:G19")
Set f = rng.Find(what:=Fr, lookat:=xlWhole)
If Not f Is Nothing Then
Crng.Copy Cells(14, f.Column)
Else: MsgBox "Not Found"
Exit Sub
End If
End Sub
给你
Sub DoIt()
Dim rng As Range, f As Range
Dim Fr As Range, Crng As Range
Set Fr = Range("A10")
Set Crng = Range("A14:A18")
Set rng = Range("C10:G19")
Set f = rng.Find(what:=Fr, lookat:=xlWhole)
If Not f Is Nothing Then
Crng.Copy Cells(14, f.Column)
Else: MsgBox "Not Found"
Exit Sub
End If
End Sub
给你
Sub DoIt()
Dim rng As Range, f As Range
Dim Fr As Range, Crng As Range
Set Fr = Range("A10")
Set Crng = Range("A14:A18")
Set rng = Range("C10:G19")
Set f = rng.Find(what:=Fr, lookat:=xlWhole)
If Not f Is Nothing Then
Crng.Copy Cells(14, f.Column)
Else: MsgBox "Not Found"
Exit Sub
End If
End Sub
避免使用select
方法,这是错误的做法无效使用select
方法,这是错误的做法无效使用select
方法,这是错误的做法无效使用select
方法,这是错误的做法42-如果查找和搜索值是公式,我如何使其工作。。我已尝试更改.Value2,但似乎不起作用。L42-如果查找和搜索值是公式,如何使其起作用。。我已尝试更改.Value2,但似乎不起作用。L42-如果查找和搜索值是公式,如何使其起作用。。我已尝试更改.Value2,但似乎不起作用。L42-如果查找和搜索值是公式,如何使其起作用。。我已尝试更改.Value2,但似乎不起作用。