为什么我的excel宏在另一台电脑上的效果不一样?

问题描述 投票:0回答:1

我想出了一个VBA来帮助我的朋友,但是宏在他的电脑上做的事情不一样。我们尝试了网络上所有常见的解决方案。我们有相同的工具参考和excel版本,所以我们真的不知道为什么它的工作方式不同。以下是问题的总结和我设计的代码。

问题:excel图片显示的是一个仓库的简单蓝图。在单元格A20:C30中,我们希望Macro能够将托盘ID插入到相应的托盘位置。例如:托盘ID 656816将被插入到B16,而656822将被插入到C16。请忽略excel表格中的其他内容。Excel图片

代码:`

Sub PalletIn()

    Dim myLastRow As Long
    Dim myRow As Long
    Dim myFind As String
    Dim myReplace As String

    myLastRow = ThisWorkbook.Worksheets("VNA").Cells(Rows.Count, "C").End(xlUp).Row
    Application.ScreenUpdating = False

    'start loop
    For myRow = 21 To myLastRow
        'Find and replace values
        myFind = ThisWorkbook.Worksheets("VNA").Cells(myRow, "C")
        myReplace = ThisWorkbook.Worksheets("VNA").Cells(myRow, "B")
        'Fix the search range
        Range("B4:P17").Select
        'Ignore errors that result from finding no matches
        On Error Resume Next
        'Do all replacements on sheet        
        Cells.Find(What:=myFind, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Offset(-1, 0).Select
        If Not IsEmpty(ActiveCell.Value) Then MsgBox "There is another pallet in this location!"
        If Not IsEmpty(ActiveCell.Value) Then Exit Sub
        Cells(myRow, "B").Copy _
        Destination:=ActiveCell
        'Reset error checking
        On Error GoTo 0
    Next myRow

    Application.ScreenUpdating = True

    MsgBox "Pallet in!"

End Sub

它在我的机器上完全按照我的要求工作,但当我把文件发给我的朋友时,它的工作方式却不同。例如,Macro会提示错误信息 "There is another pallet"(即使单元格是空白的),并将托盘ID 656816粘贴到单元格B4。

我们尝试调整了这里和那里的代码,但也没有用。如果是因为初学者的错误,我们提前道歉!

谢谢您的时间和帮助

excel-vba
1个回答
1
投票

由于查找函数的设置,我得到了和你描述的相同的错误。

所以,我改变了什么,在 Cells.Find() 功能是。

  • =xlPart -> =xlWhole 'To look at the whole cell
  • =xlFormulas2 -> xlFormulas 'It's the standard, would think the previous could be a defined variable...

你也可以详细说明来改变

  • MatchCase:=False -> MatchCase:=True "如果搜索是大小写敏感的

我还增加了 ThisWorkbook.Worksheets("VNA"). 以确保公式检查正确的表有正确的参考。

修改后的代码为。

Sub PalletIn()

    Dim myLastRow As Long
    Dim myRow As Long
    Dim myFind As String
    Dim myReplace As String

    myLastRow = ThisWorkbook.Worksheets("VNA").Cells(Rows.Count, "C").End(xlUp).Row
    Application.ScreenUpdating= False

    'start loop
    For myRow = 21 To myLastRow
        'Find and replace values
        myFind = ThisWorkbook.Worksheets("VNA").Cells(myRow, "C")
        myReplace = ThisWorkbook.Worksheets("VNA").Cells(myRow, "B")
        'Fix the search range
        Range("B4:P17").Select
        'Ignore errors that result from finding no matches
        On Error Resume Next
        'Do all replacements on sheet
        ThisWorkbook.Worksheets("VNA").Cells.Find(What:=myFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Offset(-1, 0).Select

        If Not IsEmpty(ActiveCell.Value) Then 'Instead of having two "IF" function you could have one, which is faster and easier to read.
            MsgBox "There is another pallet in this location!" & vbCrLf & "(Pallet Location: " & ActiveCell.Offset(-1, 0).Value & ", Pallet ID: " & ActiveCell.Value & ")"
            Exit Sub
        End If

        ActiveCell = ThisWorkbook.Worksheets("VNA").Cells(myRow, "B") 'This is faster, but the way your wrote is more preferable if you want to keep cell formatting.
        'Reset error checking
        On Error GoTo 0
    Next myRow

    Application.ScreenUpdating = True

    MsgBox "Pallet in!"

End Sub

结果:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.