簡體   English   中英

運行時錯誤“ 13”:在我的VBA Excel代碼中

[英]Run-time error “13”: in my VBA excel code

我正在編寫一個腳本,該腳本將計算幾個單獨日期之間的天數。 我在單元格中有一個數據,例如:

1-進行中#02-分配給團隊#22/01/2013 14:54:23,4-擱置#02-分配中的團隊#18/01/2013 16:02:03,1-進行中#02 -分配給團隊#18/01/2013 16:02:03

那是關於我的交易狀態的信息。 我想計算此交易處於“ 4-保留”狀態的天數。 因此,在此示例中,它將在2013年1月18日至2013年1月22日之間。

我寫了這樣的東西(對不起我的母語文字)

Sub Aktywnywiersz()
    Dim wiersz, i, licz As Integer
    Dim tekstwsadowy As String
    Dim koniectekstu As String
    Dim pozostalytekst As String
    Dim dataztekstu As Date
    Dim status4jest As Boolean
    Dim status4byl As Boolean
    Dim datarozpoczecia4 As Date
    Dim datazakonczenia4 As Date
    Dim dniw4 As Long


    wiersz = 2 'I start my scrypt from second row of excel

    Do Until IsEmpty(Cells(wiersz, "A")) 'this should work until there is any text in a row

        status4jest = False 'is status 4-On Hold is now in a Loop
        status4byl = False 'is status 4-On Hold was in las loop
        dniw4 = 0 ' numbers od days in 4-On Hold status
        tekstwsadowy = Cells(wiersz, "H").Value2 'grabing text
        tekstwsadowy = dodanieprzecinka(tekstwsadowy) 'in some examples I had to add a coma at the end of text

        For i = 1 To Len(tekstwsadowy)
          If Right(Left(tekstwsadowy, i), 1) = "," Then licz = licz + 1  'count the number of comas in text that separates the changes in status
        Next

        For j = 1 To licz

            koniectekstu = funkcjaliczeniadni(tekstwsadowy) 'take last record after coma
            Cells(wiersz, "k") = koniectekstu

            dataztekstu = funkcjadataztekstu(koniectekstu) 'take the date from this record
            Cells(wiersz, "m") = dataztekstu

            status4jest = funkcjaokreslenia4(koniectekstu) 'check if there is 4-On Hold in record
            Cells(wiersz, "n") = status4jest


            If (status4byl = False And staus4jest = True) Then

                datarozpoczecia4 = dataztekstu
                status4byl = True

            ElseIf (status4byl = True And staus4jest = False) Then
                datazakonczenia4 = dataztekstu
                status4byl = False  'if elseif funkcion to check information about 4-On Hold
                dniw4 = funkcjaobliczeniadniw4(dniw4, datazakonczenia4, datarozpoczecia4) 'count days in 4-On Hold

            Else
                  'Else not needed...
            End If


            tekstwsadowy = resztatekstu(tekstwsadowy, koniectekstu) 'remove last record from main text

        Next

        Cells(wiersz, "L") = dniw4 ' show number of days in 4-On Hold status


        wiersz = wiersz + 1
    Loop
End Sub


Function funkcjaliczeniadni(tekstwsadowy As String)

    Dim a, dl As Integer
    dl = Len(tekstwsadowy)

    a = 0

On Error GoTo errhandler:

    Do Until a > dl
        a = Application.WorksheetFunction.Find(",", tekstwsadowy, a + 1)
    Loop

    funkcjaliczeniadni = tekstwsadowy
    Exit Function
errhandler:
    funkcjaliczeniadni = Right(tekstwsadowy, dl - a)

End Function


Function dodanieprzecinka(tekstwsadowy As String)

    If Right(tekstwsadowy, 1) = "," Then
        dodanieprzecinka = Left(tekstwsadowy, Len(tekstwsadowy) - 1)
    Else
        dodanieprzecinka = tekstwsadowy
    End If

End Function


Function resztatekstu(tekstwsadowy, koniectekstu As String)

    resztatekstu = Left(tekstwsadowy, Len(tekstwsadowy) - Len(koniectekstu))

End Function


Function funkcjadataztekstu(koniectekstu As String)

    funkcjadataztekstu = Right(koniectekstu, 19)
    funkcjadataztekstu = Left(funkcjadataztekstu, 10)

End Function


Function funkcjaobliczeniadniw4(dniw4 As Long, datazakonczenia4 As Date, datarozpoczecia4 As Date)

    Dim liczbadni As Integer

    liczbadni = DateDiff(d, datarozpoczecia4, datazakonczenia4)
    funkcjaobliczaniadniw4 = dniw4 + liczbadni

End Function

Function funkcjaokreslenia4(koniectekstu As String)

    Dim pierwszyznak As String

    pierwszyznak = "4"

    If pierszyznak Like Left(koniectekstu, 1) Then
        funkcjaokreslenia4 = True
    Else
        funkcjaokreslenia4 = False
    End If

End Function

現在我明白了

運行時錯誤“ 13”

dataztekstu = funkcjadataztekstu(koniectekstu) 'take the date from this record

我將非常感謝您的幫助。

由於類型不匹配,您遇到了該錯誤。 dataztekstu被聲明為日期,並且很可能由funkcjadataztekstu函數返回的表達式不是日期。 您將必須逐步解決它,以找到獲得的回報。

這是一個復制該問題的簡單示例

這會給你這個錯誤

Option Explicit

Sub Sample()
    Dim dt As String
    Dim D As Date

    dt = "Blah Blah"

    D = getdate(dt)

    Debug.Print D
End Sub

Function getdate(dd As String)
    getdate = dd
End Function

這不會

Option Explicit

Sub Sample()
    Dim dt As String
    Dim D As Date

    dt = "12/12/2014"

    D = getdate(dt)

    Debug.Print D
End Sub

Function getdate(dd As String)
    getdate = dd
End Function

如果您將功能更改為此

Function funkcjadataztekstu(koniectekstu As String)
    Dim temp As String

    temp = Right(koniectekstu, 19)
    temp = Left(temp, 10)

    MsgBox temp '<~~ This will tell you if you are getting a valid date in return

    funkcjadataztekstu = temp
End Function

然后,您可以看到該函數返回什么。

我嘗試運行您的代碼,但是要理解您要執行的操作有點困難。 它的一部分是您所用語言的代碼,但是由於缺少縮進等,因此代碼也很難閱讀:)

另外,我不了解工作表中的數據外觀。 我確實是通過猜測來運行的,但是當我得到相同的錯誤時,您正在For循環的第二次運行中描述該錯誤-這是因為koniectekstu字符串為空。 不知道這是否是您的問題,所以我的解決方案很籠統。

為了解決這類問題:

  1. 使用代碼模塊頂部的Option Explicit 這將使您必須聲明模塊中使用的所有變量,並且將消除運行代碼之前遇到的許多問題。 例如,您聲明一個變量status4jest但使用另一個名為staus4jest變量,除非您使用Option Explicit否則Excel不會抱怨。

  2. 聲明函數的返回類型。

  3. 格式化代碼,使其更易於閱讀。 在語句前后使用空格。 評論一切! 您已經做了一些,但是請確保初學者可以理解。 我將編輯您的代碼作為縮進示例。

  4. 調試 使用F8逐步檢查代碼,並確保所有變量都包含您認為的功能。 您很可能會通過這種方式調試代碼來解決您的問題。

  5. 在這里就遇到的特定問題或如何解決特定問題尋求幫助,不要發送所有代碼並詢問為什么它不起作用。 如果您將問題分解成幾個部分並單獨提問,您將更快地學習VBA。

  6. 有關您的代碼的一個特定技巧:查找Split函數。 它可以接受一個字符串並根據定界符創建一個數組-示例: Split(tekstwsadowy, ",")將為您提供一個字符串數組,文本之間用逗號分隔。

  7. 我是否提到Option Explicit ;)

無論如何,即使我沒有解決您遇到的確切錯誤,我也希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM