VBA:根据匹配条件

时间:2017-03-20 22:26:15

标签: excel vba excel-vba

编辑:好的我觉得我太复杂了。下面的代码可以达到我想要的效果,但我会得到一种方法' Range'对象' _Global'失败的错误。关于如何解决它的任何想法?

Sub Test()

Dim var1 As String
Dim var3 As Variant
Dim var4 As Integer

var1 = Worksheets("wage run").Range("D1")
var3 = Worksheets("with Changes").Range("A1:A138")

var4 = Application.WorksheetFunction.Match(var1, var3, 0)



Select Case Range("b7").HasFormula
Case Is = False
    Sheets("wage run").Select
    Range("B7").Copy
    Sheets("With Changes").Select
    ***Range(Cells(var4, 5)).Select***
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
End Select

End Sub

1 个答案:

答案 0 :(得分:0)

我从另一篇文章得到了答案。谢谢大家的投入!

Sub tgr()

Dim rngFound As Range
Dim strFirst As String
Dim strID As String

strID = Worksheets("wage run").Range("D1")

Set rngFound = Worksheets("with changes").Columns("A").Find(strID, Worksheets("with changes").Cells(Rows.Count, "A"), xlValues, xlWhole)
If Not rngFound Is Nothing Then
    strFirst = rngFound.Address
        End If

Select Case Range("b7").HasFormula
Case Is = False
    Sheets("wage run").Select
    Range("B7").Copy
    Sheets("With Changes").Select
    Cells(rngFound.Row, 5).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Sheets("wage run").Select
End Select


Set rngFound = Columns("A").Find(strID, rngFound, xlValues, xlWhole)

Set rngFound = Nothing

End Sub