简体   繁体   English

复制单元格与工作表名称匹配的行会使下标超出范围(错误9)

[英]Copy row where cell matches worksheet name throws Subscript out of range (Error 9)

I was searching around this forum for quite a long time and learned quite a bit. 我在这个论坛上搜索了很长时间,并且学到了很多东西。 However, I have a problem now which is easy to fix, I guess, but I am too blind to see the right solution. 但是,我想我现在有一个容易解决的问题,但是我太盲目了,看不到正确的解决方案。

I have a sheet with over 50k rows which also contain a number for suppliers, so these numbers happen to be duplicates. 我有一个超过5万行的工作表,其中还包含一个供供应商使用的数字,因此这些数字恰好是重复的。 I got a vba macro which creates a new sheet for every supplier number without duplicates, so thats not the problem. 我有一个vba宏,它为每个供应商编号创建了一个新表,没有重复,所以那不是问题。 However, I want to copy the data of the row to the worksheet which is equal to the supplier number appearing in that row. 但是,我想将该行的数据复制到工作表中,该工作表等于该行中出现的供应商编号。

The supplier numbers are in column A. So, if Row 2 has supplier number 10 then copy the row to sheet "10", Row 3 has number 14 to sheet "14", Row 4 has number 10 to sheet "10" again and so on. 供应商编号在A列中。因此,如果第2行的供应商编号为10,则将该行复制到工作表“ 10”,第3行的编号为14到工作表“ 14”,第4行的编号为10再次工作表“ 10”,以此类推。

I used the following code I found here and remodeld it a bit. 我使用了在这里找到的以下代码,并对它进行了一些重塑。

Sub CopyRows()

Dim DataSht As Worksheet, DestSht As Worksheet

Set DataSht = Sheets("All Data")

RowCount = DataSht.Cells(Cells.Rows.Count, "A").End(xlUp).Row

For i = 2 To RowCount

DataSht.Range("A" & i).EntireRow.Copy
Set DestSht = Sheets(DataSht.Range("A" & i).Value)
DestLast = DestSht.Cells(Cells.Rows.Count, "A").End(xlUp).Row
DestSht.Range("A" & DestLast + 1).Paste

Next i

End Sub

However it get an subscript out of range error on this line: Set DestSht = Sheets(DataSht.Range("A" & i).Value) 但是,它在此行上得到下标超出范围的错误:Set DestSht = Sheets(DataSht.Range(“ A”&i).Value)

Try this: 尝试这个:

For i = 2 To RowCount      
    Set DestSht = Sheets(CStr(DataSht.Range("A" & i)))
    DestLast = DestSht.Cells(Cells.Rows.Count, "A").End(xlUp).row
    DataSht.Range("A" & i).EntireRow.Copy Destination:=DestSht.Range("A" & DestLast + 1)
Next I

Since: 以来:

  • with CStr function it points to Sheets("12") 使用CStr函数,它指向Sheets("12")

  • while Cstr it'd point to Sheets(12) , ie the twelfth sheet in the workbook, which could not be the one you'd want or neither be there. Cstr则指向Sheets(12) ,即工作簿中的第十二个工作表,它可能不是您想要的工作表,也可能不存在。

This error is caused because Excel can't identify a sheet with the same name as your column A value. 导致此错误的原因是Excel无法识别与列A值同名的工作表。 You might want to run this small sub to see if it gives you a clue as to why... 您可能想运行这个小子,看看它是否为您提供了一个有关为什么的线索...

Sub SheetNamesAndIndexes()

DIm ws as Worksheet
For Each ws in ThisWorkbook.Sheets
    Debug.print ws.Name & ";" & ws.Index
Next
End Sub

This will show you the names and the indexes of all your sheets. 这将显示所有工作表的名称和索引。 If that doesn't reveal the problem, you can take this and incorporate it into your code to help you debug, like so... 如果仍然不能解决问题,您可以将其合并到代码中以帮助您调试,就像这样...

Dim ws as Worksheet
For i = 2 To RowCount
    For Each ws in ThisWorkbook.Sheets
        Debug.Print ws.Name * ";""" & DataSht.Range("A" & i).Value & """;" & ws.Name = DataSht.Range("A" & i).Value
    Next
...

This will put the value of each cell in Col A next to each sheet name, along with whether or not Excel thought the two matched. 这会将Col A中每个单元格的值放在每个工作表名称的旁边,以及Excel是否认为这两个匹配。 If you see one that says "False" that you expect to be "True", investigate that next. 如果您看到一个期望为“ True”的错误消息,请接下来进行调查。 I've put quotes around the DataSht.Range.Value to make it more obvious if you've got extra spaces, etc. If that doesn't yield answers, another answer suggested making sure that you're not comparing strings to integers. 我在DataSht.Range.Value周围加上了引号,以使其在出现多余空格等情况时更加明显。如果未产生答案,则建议使用另一个答案来确保您未将字符串与整数进行比较。 If that's the case, then wrap your Range.Value in a Cstr() and run it again. 如果是这种情况,则将Range.Value包装在Cstr() ,然后再次运行。 Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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