Nested For Next Loops: Outer loop not iterating

2019-09-19 02:08发布

I have a range of data from A2:A34 with various names in it that I need to copy to the range E9:E14. I only need to copy and paste unique names (I don't need a double of the same name). I am pretty sure using a nested For Next loop is the way to go but I'm having trouble getting the outer loop to go to the next iteration. Right now this is only giving me the last name in the in range A2:A34 repeated in E9:14. I was looking into using Exit For but when I added that in the code, the outer loop iterated but then the inner loop started over at 2.

Any help with this would be greatly appreciated. Thanks! Below is my code:

Sub FillTable()
  Dim tableCount As Integer
  Dim rowCount As Integer

  For tableCount = 9 To 13
    If Range("E" & tableCount).Value = "" Then
      For rowCount = 2 To 34  
        If Range("E" & tableCount).Value = Range("A" & rowCount).Value Then

        ElseIf Range("E" & tableCount).Value <> Range("A" & rowCount).Value Then
          Range("E" & tableCount).Value = Range("A" & rowCount).Value
        End If
      Next rowCount
    End If
  Next tableCount
End Sub

2条回答
狗以群分
2楼-- · 2019-09-19 02:39

I am not sure if VBA is really needed for this exact issue but hopefully the below code will help. I switched the loops so that you only iterate through the large list of names once and then you iterate through the second list checking for duplicates. I also added a variable so it would allow for more than 5 unique names (unlike when tablecount was 9 to 13).

Fair warning - this is a quick and easy solution. It is neither elegant nor optimized.

Sub FillTable()

Dim tableCount As Integer
Dim rowCount As Integer
Dim n As Integer

n = 0
For rowCount = 2 To 34

  For tableCount = 9 To 9 + n
      If Range("E" & tableCount).Value = Range("A" & rowCount).Value Then
        ' name already found, break out of loop
        Exit For
      ElseIf Range("E" & tableCount).Value = "" Then
        Range("E" & tableCount).Value = Range("A" & rowCount).Value
        n = n + 1
      End If
  Next tableCount

Next rowCount

End Sub
查看更多
家丑人穷心不美
3楼-- · 2019-09-19 02:44

It seems that your description of your goal does not match the code.

You are copying from (to simplify a bit) col A to col E. Does col E already contain data? If not, why the first "if" stmt to see if some cell is empty? If yes, E already contains data, then you want to loop through E to see if E already contains the new name. I'll also point out that E has room (per your spec) for 6 names, while the source has 33 names.

Without knowing your goal, I won't suggest real code, but, perhaps a way of approaching the problem: Create functions that do only very simple little things. For example, perhaps simplest, a function to see if name already in list. Note that I assume a value for highest used row, be sure to define it, whether as 14, or as some counter.

Function Is_Name_Already_Present_in_E( Name-to-check as String ) As Bool
   Is_Name_Already_Present_in_E = False               ; Default we'll return if don't find name.
   for r = 9 to highest-so-far-used
      if Name-to-check = Range( "E" & r ).value then  ; If found name in list,
         Is_Name_Already_Present_in_E = true          ; then return true.
         exit function
      end if
   next r
end function                                          ; If scan whole list, and not found,false.

I'm sure there are a few syntax errors, but they should be easy to resolve.

Then, create a simple function to add your new name to E. Perhaps something like (Beware the assumptions!):

Function Add_New_Name_To_List( Name  as string ) as bool
   if highest_used_so_far >= 14 then
      Error "No room to insert name:" & Name & ". Rejected."
      Add_New_Name_To_List = false
      exit function
   end if
   highest_used_so_far = highest_used_so_far + 1
   range( A & h_u_s_r ).value = Name
   Add_New_Name_To_List = true
exit function

Then, your main becomes a very simple (fake code example because I don't know your intent):

for r = 2 to 34
    if not Is_Name_Already_Present_in_E( range( "A" & r ).value ) then
        if not Add_Name_to_E( range( "A" & r ).value ) then
           ... what to do if add fails. ...
        end if
    end if
next name

Break your problem into pieces and it should be clear how to write each piece. Good luck.

查看更多
登录 后发表回答