Excel Application.WorksheetFunction.Match未按预期工作
我使用的EXCEL文件包含11个工作表,配置如下: VOL_CODE=我要查找的代码。如果存在于工作表中,则应删除包含卷代码的整行 工作表:Excel Application.WorksheetFunction.Match未按预期工作,excel,vba,loops,Excel,Vba,Loops,我使用的EXCEL文件包含11个工作表,配置如下: VOL_CODE=我要查找的代码。如果存在于工作表中,则应删除包含卷代码的整行 工作表: “新VOL数据””–这是在整个过程中“保持打开”(“活动”或“选定”)的工作表。 此外,在K1:K10范围内,如果选择或未选择目标工作表,则显示(“X”) “目的地”1“,“目的地2”,“目的地3”。。。“目标10” “目标”工作表,如果出现卷代码且工作表指示为“选定”(在“新卷数据”工作表中),则应删除其行 代码: Private子命令按钮3\u单击
Private子命令按钮3\u单击()
'---从所有工作表中删除卷代码行-----
将ws1、ws2标注为工作表
Dim VOL_代码作为字符串
Dim PLA,林作为整数
设置ws1=工作表(“新卷数据”)---这是工作表-----
VOL_CODE=“RS_123456”-这是要搜索的代码-----
对于PLA=1到10
如果UCase(射程(“K”和PLA))为“X”,则转到跳伞:
设置ws2=工作表(“目的地”和修剪(Str(PLA)))
做正确的事
转到跳转计划时出错:
LIN=Application.WorksheetFunction.Match(卷代码,ws2.Range(“B:B”),0)
ws2.Cells(LIN,1).EntireRow.Delete
环
中国人民解放军:
下一个解放军
端接头
问题是,当我执行代码时,它在DESTINATION1工作表中运行良好,包含或不包含VOL_代码(如果包含,则循环删除VOL_代码的行,直到没有更多的行),然后,当找不到VOL_代码的更多条目时,它转到“JUMP_PLA:”和“Next PLA”。。。从这里开始,现在转到下一个“DESTINATIONx”工作表(下一个选定的工作表)。。。执行Application.WorksheetFunction.Match命令时出现错误(查找或不是有效条目):
执行错误:1004
应用程序定义或对象定义错误
我知道这一定是一个愚蠢的错误,但由于我是一个新手,我无法想象它。这让我发疯。。。
有人能给我点灯吗?非常感谢,我提前向您表示感谢。这里有一个稍微修改过的版本 有关更改的注释,请参见注释
Private子命令按钮3\u单击()
将ws1设置为工作表,将ws2设置为工作表'这是一个稍微重新处理过的版本
有关更改的注释,请参见注释
Private子命令按钮3\u单击()
将ws1调整为工作表,将ws2调整为工作表'Application.WorksheetFunction.Match'以删除行
- 最好将过程
doDest
复制到标准模块(例如Module1
)中,然后调用按钮代码:
- 我把你的代码放在里面了,所以你可以看到错误和一些选项
- 代码的其余部分只是一些用来玩的玩具,因为我从未见过用于删除行的
Match
- 如果您想玩,请将完整的代码复制到新建工作簿中的标准模块中,并将工作表重命名为
新建VOL DATA
。在其范围K1:K10
中,在几个单元格中输入一个x
,就可以开始了
代码
Option Explicit
Sub doDest()
'--- Remove VOL_CODE line from all worksheets -----
' Speed up the code (you won't see what the macro is doing).
Application.ScreenUpdating = False
Dim ws1 As Worksheet, ws2 As Worksheet 'Dim ws1, ws2 As Worksheet
Dim VOL_CODE As String
Dim PLA As Long, LIN As Long ' Dim PLA, LIN As Integer
Dim wb As Workbook: Set wb = ThisWorkbook ' The workbook with this code.
Set ws1 = wb.Worksheets("NEW VOL DATA") 'Set ws1 = Worksheets("NEW VOL DATA") '--- This is the Working Worksheet -----
VOL_CODE = "RS_123456" '--- This is the code to search for -----
For PLA = 1 To 10
'If StrComp(ws1.Range("K" & PLA).Value, "X", vbTextCompare) <> 0 _
Then GoTo JUMP_PLA
If UCase(ws1.Range("K" & PLA).Value) <> "X" Then GoTo JUMP_PLA ' If UCase(Range("K" & PLA)) <> "X" Then GoTo JUMP_PLA:
Set ws2 = wb.Worksheets("DESTINATION" & PLA) ' Set ws2 = Worksheets("DESTINATION" & Trim(Str(PLA)))
Do ' Do While True
' On Error GoTo JUMP_PLA:
' LIN = Application.WorksheetFunction.Match(VOL_CODE, ws2.Range("B:B"), 0)
' ws2.Cells(LIN, 1).EntireRow.Delete
On Error Resume Next ' Turn ON error trapping.
' "ws2.Columns("B")" is just an option, you can stick with
' "ws2.Range("B:B")".
LIN = Application.WorksheetFunction _
.Match(VOL_CODE, ws2.Columns("B"), 0)
If Err.Number <> 0 Then
On Error GoTo 0 ' Turn OFF error trapping.
'Debug.Print "Done with worksheet '" & ws2.Name & "'."
Exit Do ' or: GoTo JUMP_PLA
Else
On Error GoTo 0 ' Turn OFF error trapping.
ws2.Cells(LIN, 1).EntireRow.Delete
End If
Loop
JUMP_PLA:
Next PLA
Application.ScreenUpdating = True
MsgBox "Deleted rows containing '" & VOL_CODE & "'.", _
vbInformation, "Success"
End Sub
' Deletes all sheets named "DESTINATIONx", where x is from 1 to 10.
Sub deleteDest()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim i As Long
For i = 1 To 10
Application.DisplayAlerts = False ' To prevent Excel from 'complaining'.
On Error Resume Next ' If a sheet does not exist.
wb.Sheets("DESTINATION" & i).Delete
On Error GoTo 0
Application.DisplayAlerts = True
Next i
End Sub
' Adds worksheets named "DESTINATIONx", where x is from 1 to 10.
' In each of those worksheets, adds "RS_123456" to up to 100 cells
' in 'random' rows from 1 to 1000 in column 'B'.
Sub createDest()
' Speed up the code (you won't see what the macro is doing).
Application.ScreenUpdating = False
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet, i As Long, j As Long, CurrName As String
For i = 1 To 10
CurrName = "DESTINATION" & i
On Error Resume Next ' Turn ON error trapping.
Set ws = wb.Worksheets(CurrName)
If Err.Number <> 0 Then
' Sheet with current name does not exist.
Set ws = wb.Worksheets _
.Add(After:=wb.Worksheets(wb.Worksheets.Count))
ws.Name = CurrName
'Else ' Sheet with current name exists.
End If
On Error GoTo 0 ' Turn OFF error trapping.
ws.Columns("B").Clear ' Ensures new data if sheets already exist.
For j = 1 To 100
ws.Cells(Application.WorksheetFunction.RandBetween(1, 1000), "B") _
.Value = "RS_123456"
Next j
Next i
wb.Sheets(1).Select
Application.ScreenUpdating = True
End Sub
' Counts the number of cells in column 'B' containing a value.
Sub countDest()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim i As Long
For i = 1 To 10
On Error Resume Next
Debug.Print "DESTINATION" & i, wb.Worksheets("DESTINATION" & i) _
.Columns("B") _
.SpecialCells(xlCellTypeConstants) _
.Cells.Count
If Err.Number <> 0 Then
Debug.Print "DESTINATION" & i, "No cells found."
End If
Next i
End Sub
' Ultimate test
Sub testDest()
deleteDest ' Deletes the sheets.
createDest ' Creates the worksheets with random data.
countDest ' Counts the cells containing "RS_123456" (Debug.Print).
doDest ' Deletes the rows containing "RS_123456" in column 'B'.
countDest ' Counts the cells containing "RS_123456" (Debug.Print).
MsgBox "Ultimate: deleted, created, counted, done and counted again."
End Sub
' Initialize
Sub initCreateAndCount()
deleteDest ' Deletes the sheets.
createDest ' Creates the worksheets with random data.
countDest ' Counts the cells containing "RS_123456" (Debug.Print).
MsgBox "Initialized: Sheets deleted and created, and cells counted."
End Sub
' Shows how even when the 'dest' sheets exist, new values are generated.
Sub testCreateCount()
createDest ' Creates the worksheets with random data.
countDest ' Counts the cells containing "RS_123456" (Debug.Print).
MsgBox "Sheets created and cells counted."
End Sub
选项显式
亚多德斯特()
'---从所有工作表中删除卷代码行-----
'加快代码速度(您将看不到宏正在做什么)。
Application.ScreenUpdating=False
将ws1调整为工作表,将ws2调整为工作表“将ws1调整为工作表,将ws2调整为工作表”
Dim VOL_代码作为字符串
模糊PLA为长,林为长“模糊PLA,林为整数
将wb设置为工作簿:将wb=ThisWorkbook设置为具有此代码的工作簿。
Set ws1=wb.Worksheets(“新卷数据”)“Set ws1=Worksheets(“新卷数据”)”---这是工作表-----
VOL_CODE=“RS_123456”-这是要搜索的代码-----
对于PLA=1到10
'如果StrComp(ws1.Range(“K”和PLA).Value,“X”,vbtextcomper)0_
然后去跳伞
如果UCase(ws1.Range(“K”)和PLA.Value)“X”则跳转PLA'如果UCase(Range(“K”)和PLA))“X”则跳转PLA:
设置ws2=wb.工作表(“目的地”和PLA)设置ws2=工作表(“目的地”和Trim(Str(PLA)))
做真实的事
'错误转到跳转\u PLA:
'LIN=Application.WorksheetFunction.Match(卷代码,ws2.Range(“B:B”),0)
'ws2.Cells(LIN,1).EntireRow.Delete
“错误恢复下一步”打开错误捕获。
“ws2.Columns(“B”)只是一个选项,您可以坚持使用
“ws2.Range(“B:B”)”。
LIN=Application.WorksheetFunction_
.Match(卷代码,ws2.Columns(“B”),0)
如果错误号为0,则
错误转到0'时关闭错误捕获。
'Debug.Print“使用工作表完成”'&ws2.Name&''
退出待办事项:转到跳转计划
其他的
错误转到0'时关闭错误捕获。
ws2.Cells(LIN,1).EntireRow.Delete
如果结束
环
中国人民解放军:
下一个解放军
Application.ScreenUpdating=True
MsgBox“已删除包含“&”卷代码“&”的行_
VBA信息,“成功”
端接头
'删除名为“DESTINATIONx”的所有工作表,其中x为1到10。
子删除dest()
将wb设置为工作簿:设置wb=ThisWorkbook
我想我会坚持多久
对于i=1到10
Application.DisplayAlerts=False“以防止Excel“投诉”。
错误时,如果工作表不存在,则“继续下一步”。
工作分解表(“目的地”&i)。删除
错误转到0
Application.DisplayAlerts=True
接下来我
端接头
'添加名为“DESTINATIONx”的工作表,其中x是fro
Private Sub CommandButton3_Click()
doDest
End Sub
Option Explicit
Sub doDest()
'--- Remove VOL_CODE line from all worksheets -----
' Speed up the code (you won't see what the macro is doing).
Application.ScreenUpdating = False
Dim ws1 As Worksheet, ws2 As Worksheet 'Dim ws1, ws2 As Worksheet
Dim VOL_CODE As String
Dim PLA As Long, LIN As Long ' Dim PLA, LIN As Integer
Dim wb As Workbook: Set wb = ThisWorkbook ' The workbook with this code.
Set ws1 = wb.Worksheets("NEW VOL DATA") 'Set ws1 = Worksheets("NEW VOL DATA") '--- This is the Working Worksheet -----
VOL_CODE = "RS_123456" '--- This is the code to search for -----
For PLA = 1 To 10
'If StrComp(ws1.Range("K" & PLA).Value, "X", vbTextCompare) <> 0 _
Then GoTo JUMP_PLA
If UCase(ws1.Range("K" & PLA).Value) <> "X" Then GoTo JUMP_PLA ' If UCase(Range("K" & PLA)) <> "X" Then GoTo JUMP_PLA:
Set ws2 = wb.Worksheets("DESTINATION" & PLA) ' Set ws2 = Worksheets("DESTINATION" & Trim(Str(PLA)))
Do ' Do While True
' On Error GoTo JUMP_PLA:
' LIN = Application.WorksheetFunction.Match(VOL_CODE, ws2.Range("B:B"), 0)
' ws2.Cells(LIN, 1).EntireRow.Delete
On Error Resume Next ' Turn ON error trapping.
' "ws2.Columns("B")" is just an option, you can stick with
' "ws2.Range("B:B")".
LIN = Application.WorksheetFunction _
.Match(VOL_CODE, ws2.Columns("B"), 0)
If Err.Number <> 0 Then
On Error GoTo 0 ' Turn OFF error trapping.
'Debug.Print "Done with worksheet '" & ws2.Name & "'."
Exit Do ' or: GoTo JUMP_PLA
Else
On Error GoTo 0 ' Turn OFF error trapping.
ws2.Cells(LIN, 1).EntireRow.Delete
End If
Loop
JUMP_PLA:
Next PLA
Application.ScreenUpdating = True
MsgBox "Deleted rows containing '" & VOL_CODE & "'.", _
vbInformation, "Success"
End Sub
' Deletes all sheets named "DESTINATIONx", where x is from 1 to 10.
Sub deleteDest()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim i As Long
For i = 1 To 10
Application.DisplayAlerts = False ' To prevent Excel from 'complaining'.
On Error Resume Next ' If a sheet does not exist.
wb.Sheets("DESTINATION" & i).Delete
On Error GoTo 0
Application.DisplayAlerts = True
Next i
End Sub
' Adds worksheets named "DESTINATIONx", where x is from 1 to 10.
' In each of those worksheets, adds "RS_123456" to up to 100 cells
' in 'random' rows from 1 to 1000 in column 'B'.
Sub createDest()
' Speed up the code (you won't see what the macro is doing).
Application.ScreenUpdating = False
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet, i As Long, j As Long, CurrName As String
For i = 1 To 10
CurrName = "DESTINATION" & i
On Error Resume Next ' Turn ON error trapping.
Set ws = wb.Worksheets(CurrName)
If Err.Number <> 0 Then
' Sheet with current name does not exist.
Set ws = wb.Worksheets _
.Add(After:=wb.Worksheets(wb.Worksheets.Count))
ws.Name = CurrName
'Else ' Sheet with current name exists.
End If
On Error GoTo 0 ' Turn OFF error trapping.
ws.Columns("B").Clear ' Ensures new data if sheets already exist.
For j = 1 To 100
ws.Cells(Application.WorksheetFunction.RandBetween(1, 1000), "B") _
.Value = "RS_123456"
Next j
Next i
wb.Sheets(1).Select
Application.ScreenUpdating = True
End Sub
' Counts the number of cells in column 'B' containing a value.
Sub countDest()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim i As Long
For i = 1 To 10
On Error Resume Next
Debug.Print "DESTINATION" & i, wb.Worksheets("DESTINATION" & i) _
.Columns("B") _
.SpecialCells(xlCellTypeConstants) _
.Cells.Count
If Err.Number <> 0 Then
Debug.Print "DESTINATION" & i, "No cells found."
End If
Next i
End Sub
' Ultimate test
Sub testDest()
deleteDest ' Deletes the sheets.
createDest ' Creates the worksheets with random data.
countDest ' Counts the cells containing "RS_123456" (Debug.Print).
doDest ' Deletes the rows containing "RS_123456" in column 'B'.
countDest ' Counts the cells containing "RS_123456" (Debug.Print).
MsgBox "Ultimate: deleted, created, counted, done and counted again."
End Sub
' Initialize
Sub initCreateAndCount()
deleteDest ' Deletes the sheets.
createDest ' Creates the worksheets with random data.
countDest ' Counts the cells containing "RS_123456" (Debug.Print).
MsgBox "Initialized: Sheets deleted and created, and cells counted."
End Sub
' Shows how even when the 'dest' sheets exist, new values are generated.
Sub testCreateCount()
createDest ' Creates the worksheets with random data.
countDest ' Counts the cells containing "RS_123456" (Debug.Print).
MsgBox "Sheets created and cells counted."
End Sub