子例程向表中添加行,然后错误地转到同一工作簿中的随机函数

时间:2018-01-07 20:48:10

标签: excel vba excel-vba

我会尝试在这里获取所有信息...我在一张纸上有一个查询表(A1:E120 w / headers),还有一个格式很好的表(B1:F120 w / headers)on另外,我有一个宏,它通过这个子例程从查询表中更新格式化的表:

第1单元:

Sub UpdateLedger()
Dim Lgr1 As ListObject
Dim LgrSource As ListObject
Dim UniqueRowEntry As String
Dim n As Long
UniqueRowEntry = Cells(2, 6).Value
n = Sheets(5).UsedRange.Find(UniqueRowEntry, LookIn:=xlValues).Row - 2
Application.EnableEvents = False *I have a row highlight selection event

Set Lgr1 = Sheets(4).ListObjects(1)
Set LgrSource = Sheets(5).ListObjects(1)

For i = 1 To n
If Not Lgr1.ListRows(i).Range.Cells(1).Value = LgrSource.ListRows(i).Range.Cells(1).Value Then
    If Not Lgr1.ListRows(i).Range.Cells(5).Value = LgrSource.ListRows(i).Range.Cells(5).Value Then
       Lgr1.ListRows.Add (i)
       Lgr1.ListRows(i).Range.Value = LgrSource.ListRows(i).Range.Value

    End If
End If
Next i
Application.EnableEvents = True
End Sub

这个子太棒了!但是,当我进行调试时,它在添加行时不断跳到这些! :

第2单元:

Global CText As Range
Global SText As String
Global SWks As Integer

Private Function TextFind(wks As Integer, SearchText As String) As String
Dim SearchResult As Range
    Set SearchResult = Worksheets(wks).UsedRange.Find(SearchText)
    Set CText = SearchResult
        SText = SearchText
        SWks = wks
        TextFind = SearchResult.Address
    Debug.Print SearchResult.Address
End Function

Private Function NextText() As String
Dim SearchNext As Range
Dim ContinueBox As Variant
    Set SearchNext = Worksheets(SWks).UsedRange.Find(What:=SText, After:=CText, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
    If SearchNext Is Nothing Then
        ContinueBox = MsgBox("Clear Search Settings?", vbYesNo, "Next " & SText & " not found!")
            If ContinueBox = vbYes Then
                Set CText = Nothing: SText = "": SWks = Empty
            ElseIf ContinueBox = vbNo Then
                NextText = ""
            End If
    Else
        NextText = SearchNext.Address
        'Debug.Print SearchNext.Address
        Set CText = SearchNext
    End If
End Function

Private Function ReadCell(RType As String, RCell As Range, SheetNum As Long) As Variant
Dim addr As String
Select Case True
Case InStr(UCase(RType), UCase("row")) > 0
    ReadCell = Worksheets(SheetNum).Range(RCell.Value).Row
Case InStr(UCase(RType), UCase("col")) > 0
    ReadCell = Worksheets(SheetNum).Range(RCell.Text).Column
Case InStr(UCase(RType), UCase("val")) > 0
    ReadCell = Worksheets(SheetNum).Range(RCell.Text).Value
Case Else
    ReadCell = Error
End Select
End Function

Sub FindSomeText()
MsgBox InStr("Look in this string", "in")
End Sub

当我用' s禁用其中一个函数时,它只会跳转到另一个函数!所以我必须禁用所有这些子程序才能运行!它对我来说没有任何意义......函数名称不会意外地潜入表更新的代码中(我更愿意知道为什么会发生这种情况,而不仅仅是"嗯,那些是一次性练习功能,所以我想我会删除它们并继续生活"

我不知道它是否有帮助,但这里是格式化表格的代码中的高亮选择事件:

Sheet4

Sub worksheet_selectionchange(ByVal Target As Range)
Dim x, y, i, j, n As Long
Dim rng1, cell As Range
If Target.Column > 5 Or Target.Column < 2 Then Exit Sub
If tgb1.Value = False Then Exit Sub
x = UsedRange.Rows.Count
y = UsedRange.Find("Amount").Column - 1
Set cell = Range(Cells(2, 2), Cells(x, y))
Set rng1 = Application.Union(Target, cell)
    If Range(Cells(2, 2), Cells(x, y)).Cells.Count = Application.Union(Target, cell).Cells.Count Then
        Setformats
        If Cells(Target.Row, UsedRange.Find("amount").Column) < 0 Then
            With Range(Cells(Target.Row, 2), Cells(Target.Row, y))
                .Font.FontStyle = "Bold"
                With .Borders(xlEdgeBottom)
                    .LineStyle = xlContinuous
                    .ColorIndex = 3
                    .TintAndShade = 0
                    .Weight = xlThin
                End With
                With .Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .ColorIndex = 3
                    .TintAndShade = 0
                    .Weight = xlThin
                End With
            End With
            Cells(Target.Row, 1).Select
        ElseIf Cells(Target.Row, UsedRange.Find("amount").Column) > 0 Then
                With Range(Cells(Target.Row, 2), Cells(Target.Row, y))
                With .Borders(xlEdgeBottom)
                    .LineStyle = xlContinuous
                    .ColorIndex = 4
                    .TintAndShade = 0
                    .Weight = xlHairline
                End With
                With .Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .ColorIndex = 4
                    .TintAndShade = 0
                    .Weight = xlHairline
                End With
            End With
            Cells(Target.Row, 1).Select
        End If
End If
End Sub

Public Sub Setformats()
Dim x, y, i, j, n As Long

x = ActiveSheet.UsedRange.Rows.Count - 1
y = ActiveSheet.UsedRange.Columns.Count - 1
With Worksheets("USBank").Range(Cells(2, 2), Cells(x, y))
    .Borders(xlDiagonalDown).LineStyle = xlNone
    .Borders(xlDiagonalUp).LineStyle = xlNone
    .Borders(xlEdgeLeft).LineStyle = xlNone
    .Borders(xlEdgeTop).LineStyle = xlNone
    .Borders(xlEdgeBottom).LineStyle = xlNone
    .Borders(xlEdgeRight).LineStyle = xlNone
    .Borders(xlInsideVertical).LineStyle = xlNone
    .Borders(xlInsideHorizontal).LineStyle = xlNone
    .Font.FontStyle = "regular"
End With
End Sub

注意:它是银行对帐单(因此无法显示),5列:日期,操作(借记或贷记),交易(购买,存款,费用......),供应商(Joe&#39; s Coffee),金额(+/- $ 2.14)......这个项目的范围只是为了增加VBA的技能

0 个答案:

没有答案