使用VBA将单个单元格从工作表复制到另一个工作表

时间:2015-03-16 11:48:09

标签: excel vba excel-vba

这是在VBA中搜索,将选定的单元格从一个工作表复制到另一个工作表。

Sub uredi()
    Application.ScreenUpdating = False

    Dim ime As String
    Dim prezime As String
    Dim red As Integer
    Dim k As String

    ime = Sheets("Evidencija").Range("C7").Value
    prezime = Sheets("Evidencija").Range("C8").Value
    red = Sheets("Baza podataka").Range("F10000").End(xlUp).Row

    Sheets("Baza podataka").Select
    For i = 3 To red
        If Cells(i, 6) = ime And Cells(i, 7) = prezime Then

        ' this is where i get the error
        Sheets("Baza podataka").Range(Cells(i, 2)).Copy 

        Sheets("Evidencija").Range("C2").PasteSpecial (xlPasteAll)
      End If
    Next i

    Sheets("Evidencija").Select
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub

2 个答案:

答案 0 :(得分:1)

首先替换:

Sheets("Baza podataka").Range(Cells(i, 2)).Copy

使用:

Sheets("Baza podataka").Cells(i, 2).Copy

然后继续调试。

答案 1 :(得分:0)

这样做,

    Sub Button1_Click()
    Dim ws As Worksheet, sh As Worksheet
    Dim Rws As Long, Rng As Range, c As Range
    Dim ime As String
    Dim prezime As String
    Dim red As Integer

    Set ws = Sheets("Evidencija")
    Set sh = Sheets("Baza podataka")

    With sh
        Rws = .Cells(Rows.Count, "F").End(xlUp).Row
        Set Rng = .Range(.Cells(3, "F"), .Cells(Rws, "F"))
    End With

    ime = ws.Range("C7").Value
    prezime = ws.Range("C8").Value

    For Each c In Rng
        If c = ime And c.Offset(0, 1) = prezime Then
            c.Offset(0, -3).Copy ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        End If
    Next c

End Sub

这将复制并过去到A列中的下一个空单元格,您可以根据需要将粘贴代码更改为实际范围。