重复我的潜艇的一部分

时间:2016-07-25 19:49:10

标签: excel vba excel-vba ms-office

我想运行我的Sub的一部分,每次运行时都会使用不同的关键字。目前我写的是这样的,但是我可以采用更短的方式吗?

我在D栏中为直接借记,ATM现金提取和借记卡购买制作了一个类别 - 分别在B栏搜索D / D,C / L和POS。

我正在更改变量,但每次都要键入并运行相同的代码。我觉得应该有办法说“再次运行那一点,但随着Searchterm和Searchresult的改变!”

我确信有人比我能帮助的人更有知识。我已经使用了一个sub,所以不能插入另一个子在其中运行,还是一个函数?我已经循环了所以我不确定另一个循环是否适合?

你会做什么?你可能会猜到我对此很陌生,过去只有一点js知识。

Sub OrganiseDefaultCategories()
    '
    ' OrganiseDefaultCategories Macro
    ' Categorise the Bank Statement Entries with Default inputs. Run this first.
    '

    Dim FoundRange As Range, FirstAddress As String, Searchterm As Variant, Searchresult As Variant

    Searchterm = "D/D"
    Searchresult = "Direct Debit"

        With Range("b:b")
      Set FoundRange = .Find(What:=Searchterm, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
      FirstAddress = FoundRange.Address
      Do
        FoundRange.Offset(0, 2).Value2 = Searchresult

        Set FoundRange = .FindNext(FoundRange)
      Loop While Not FoundRange Is Nothing And FoundRange.Address <> FirstAddress

      Searchterm = "C/L"
    Searchresult = "ATM Cash Withdrawal"


      Set FoundRange = .Find(What:=Searchterm, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
      FirstAddress = FoundRange.Address
      Do
        FoundRange.Offset(0, 2).Value2 = Searchresult

        Set FoundRange = .FindNext(FoundRange)
      Loop While Not FoundRange Is Nothing And FoundRange.Address <> FirstAddress

      Searchterm = "POS"
    Searchresult = "Debit Card Purchase"


      Set FoundRange = .Find(What:=Searchterm, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
      FirstAddress = FoundRange.Address
      Do
        FoundRange.Offset(0, 2).Value2 = Searchresult

        Set FoundRange = .FindNext(FoundRange)
      Loop While Not FoundRange Is Nothing And FoundRange.Address <> FirstAddress

    End With


    End Sub

2 个答案:

答案 0 :(得分:2)

试一试:

Option Explicit

Sub OrganiseDefaultCategories()
  '
  ' OrganiseDefaultCategories Macro
  ' Categorise the Bank Statement Entries with Default inputs. Run this first.
  '

  Dim SearchTerm As String
  Dim SearchResult As String

  SearchTerm = "D/D"
  SearchResult = "Direct Debit"
  Finder SearchTerm, SearchResult
  SearchTerm = "C/L"
  SearchResult = "ATM Cash Withdrawal"
  Finder SearchTerm, SearchResult
  SearchTerm = "POS"
  SearchResult = "Debit Card Purchase"
  Finder SearchTerm, SearchResult

End Sub

Sub Finder(ByVal SearchTerm As String, ByVal SearchResult As String)

  Dim FoundRange As Range
  Dim FirstAddress As String

  With Range("b:b")
    Set FoundRange = .Find(What:=SearchTerm, LookIn:=xlValues, _
                           LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                           MatchCase:=False, SearchFormat:=False)
    If Not FoundRange Is Nothing Then
      FirstAddress = FoundRange.Address
      Do
        FoundRange.Offset(0, 2).Value2 = SearchResult
        Set FoundRange = .FindNext(FoundRange)
      Loop While Not FoundRange Is Nothing And FoundRange.Address <> FirstAddress
    End If
  End With

End Sub

我创建了一个小程序Finder,它带有2个参数,SearchTermSearchResult,然后设置变量并为每对调用Finder

我还将这两个声明为String而不是Variant。你真的只想在绝对必要时使用Variant(某些函数需要Variant),因为它可能导致很难找到错误,并且执行速度较慢。

当我将代码解压缩到Finder过程时,我也稍微修改了您的代码,以确保我们检查.Find的结果以确保我们有一些东西(即Not ... is Nothing之前试图以任何方式使用它。 VBA 执行短路IF语句,因此如果您没有找到.Find的任何内容,您仍然会收到错误,就像您最初使用它一样写入。

恭喜您使用.Find而不是循环遍历所有进行比较的行! .Find会明显加快。

答案 1 :(得分:1)

将公共代码解压缩为Sub,然后调用它:

Private Sub WhateverThisDoes(Searchterm As String, Searchresult As String)
    Set FoundRange = .Find(What:=Searchterm, LookIn:=xlValues, _
                           LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                           MatchCase:=False, SearchFormat:=False)
    FirstAddress = FoundRange.Address
    Do
        FoundRange.Offset(0, 2).Value2 = Searchresult

        Set FoundRange = .FindNext(FoundRange)
    Loop While Not FoundRange Is Nothing And FoundRange.Address <> FirstAddress
End Sub

来自调用程序:

WhateverThisDoes "C/L", "ATM Cash Withdrawal"
WhateverThisDoes "POS", "Debit Card Purchase"
'...