简体   繁体   中英

Compare two cells from different worksheets using for loop and array

I have a list on cell A1 to lastrow on the worksheet "Input Here" and I have a complete list of string values on the "String List" worksheet on cell A1 to A692 which I have stored on the array svr . What I want to happen is the macro to check all values on column A on worksheet "Input Here" and compare it to the values inside array svr , one by one until it finds it match, and when it does, it will copy a range of cells from worksheet "String List" to worksheet "Input Here". I have tried the code below, and I think it needs more work.

Sub Main_SvrLst()

Dim inp As Worksheet
Dim lst As Worksheet
Dim svr(691) As String

Set inp = ThisWorkbook.Sheets("Input Here")
Set lst = ThisWorkbook.Sheets("String List")

lr = inp.Cells(Rows.Count, 1).End(xlUp).Row

For svrctr = 0 To 691
    svr(svrctr) = lst.Range("A2").Offset(svrctr, 0).Value
Next svrctr

For a = 2 To lr
If inp.Cells(a, 1) = svr(a) Then

    Worksheets("String List").Activate
    lst.Range(Cells(a, 2), Cells(a, 8)).Copy
    inp.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial
    Worksheets("Input Here").Activate

End If
Next a



End Sub

Instead of using VBA code, you could do this with formulas in your "Input Here" sheet:

In B2 :

=""&IFERROR(INDEX('String List'!$A:$H, MATCH($A2, 'String List'!$A:$A, 0), COLUMN()), "")

Copy this formula to the right up to H2 , and copy all that down as far as your input goes.

I have changed some of your naming for clearer identification.

also off set your Array s to get the number referencing correct

 Sub Main_SvrLst()

      Dim inp As Worksheet
      Dim lst As Worksheet
      Dim svr(691) As String

      Set inp = ThisWorkbook.Sheets("Input Here")
      Set lst = ThisWorkbook.Sheets("String List")

      Dim LastRowOfInputHere As Long
      LastRowOfInputHere = inp.Cells(Rows.Count, 1).End(xlUp).Row

      Dim svrctr As Long
      'Made it 2 to 691 to off set your Header Row
      'This way the Array position and the row number are the same
      For svrctr = 2 To 691
           svr(svrctr) = lst.Cells(svrctr, "A").Value
      Next svrctr

      Dim InputHereRowReference As Long
      Dim StringListArrayReference As Long

      'With your original text it was comparing a two row offset between the "Input" and "String" Sheets
      For InputHereRowReference = 2 To LastRowOfInputHere

           For StringListArrayReference = 2 To 691

                If inp.Cells(InputHereRowReference, 1) = svr(StringListArrayReference) Then

                     lst.Activate
                     lst.Range(Cells(StringListArrayReference, 2), Cells(StringListArrayReference, 8)).Copy
                     inp.Activate
                     inp.Cells(InputHereRowReference, 2).PasteSpecial

                End If

           Next StringListArrayReference

      Next InputHereRowReference

 End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM