使用VBA比较两列中的值

时间:2016-12-07 18:02:52

标签: excel vba excel-vba

我正在处理这个比较A列(代码源)和B列(代码roc)的代码,对于A列中的每个代码源,它的代码在C列中有规定,在D列中有地址,所以如果A = B将它们复制回E和F,其代码在G列中注册,其地址在H列中。 这个我使用它的代码阻塞,直到我关闭excel并且它没有给我确切的结果如果有人可以帮助我谢谢你 这是我需要A和B,C和D

的结果的图片

enter image description here

Sub copy_lignes()

  Dim DerLigA, DerLigB  As Long, i, j As Long

    DerLigA = Sheets("sheet3").Range("A" & Rows.Count).End(xlUp).Row
    DerLigB = Sheets("sheet3").Range("B" & Rows.Count).End(xlUp).Row
    For i = 2 To DerLigA
      For j = 2 To DerLigB
        If Sheets("sheet3").Range("A" & i) = Sheets("sheet3").Range("B" & j) Then

          Sheets("sheet3").Range("A" & i).Copy Destination:=Sheets("sheet3").Range("E" & i)
          Sheets("sheet3").Range("B" & i).Copy Destination:=Sheets("sheet3").Range("F" & i)
          Sheets("sheet3").Range("C" & i).Copy Destination:=Sheets("sheet3").Range("G" & i)
          Sheets("sheet3").Range("D" & i).Copy Destination:=Sheets("sheet3").Range("H" & i)

        End If
      Next j
    Next i

End Sub

2 个答案:

答案 0 :(得分:0)

尝试下面的代码,也许这就是你在帖子中的意思:

Sub copy_lignes()

Dim DerLigA, DerLigB  As Long, i, j As Long
Dim PasteRow As Long

' optimize speed performance
Application.ScreenUpdating = False

With Sheets("Sheet3")

    DerLigA = .Cells(.Rows.Count, "A").End(xlUp).Row
    DerLigB = .Cells(.Rows.Count, "B").End(xlUp).Row
    PasteRow = 2

    For i = 2 To DerLigA
        For j = 2 To DerLigB
            If .Range("A" & i) = .Range("B" & j) Then
                .Range("A" & i).Copy Destination:=.Range("E" & PasteRow)
                .Range("B" & j & ":D" & j).Copy Destination:=.Range("F" & PasteRow)
                PasteRow = PasteRow + 1
            End If
        Next j
    Next i
End With

' restore settings 
Application.ScreenUpdating = True

End Sub

答案 1 :(得分:-1)

可能你需要在几行中标注,所以它应该如下所示:

--Putting data from only 1 station from our big datatable into the new testtable called '_testdatatable'

SELECT        station, sample_date, sample_time, wtemp, streamcode, stationdesc, TIMEVALUE
INTO              _testdatatable
FROM            MyBigDataTable
WHERE        (stationdesc = 'Back River')
order by [sample_date],[sample_time]

--Next, make a new table [_testdatatableGap] with all time values in 15min increments from a datetime table we made
SELECT [wtemp]=null
      ,[streamcode]='ABC1234'
      ,[stationdesc]= 'Back River'
      ,[TIMEVALUE]
      into [tide].[dbo].[_testdatatableGap]
  FROM DateTimeTable
  WHERE  (TIMEVALUE BETWEEN '4/19/2014' AND getdate())

--Then, get the missing dates from the Gap table and put into the testdatatable
INSERT into [_testdatatable]
      (  [wtemp]
        ,[streamcode]
        ,[stationdesc]
        ,[TIMEVALUE] 
)
    (SELECT 
       [wtemp]=null -- needs this for except to work
      ,
      [streamcode]
      ,[stationdesc]
      ,
      [TIMEVALUE] 
  FROM [_testdatatableGap]   
EXCEPT   
SELECT 
       [wtemp]=null -- needs this for except to work
      ,
    [streamcode]
      ,[stationdesc]
      ,
      [TIMEVALUE] 
  FROM [_testdatatable])