返回列表 上一主題 發帖

[發問] 執行階段錯誤13

[發問] 執行階段錯誤13

已經近一個星期了,我簡直要被這個 "執行階段錯誤13"搞得快瘋了,一直不斷地修改程式碼,但是錯誤一直不客氣地出現,請各位救救我吧!
以下是程式碼,紅字是出現偵錯的部分,附檔裡另有詳細說明

Sub C_MERGE()
If [T7] = "" Then Exit Sub
[X7:AG100].ClearContents
Dim cRow&, T As Range

Set d = CreateObject("Scripting.Dictionary") '中文品項合併
For Each T In Range([T7], [T300].End(xlUp))
   If d(T.Value) = "" Then '如果T欄資料只有一筆不重複
      d(T.Value) = T.Offset(, -6) & " " & T.Offset(, -4) & " " & T.Offset(, -3)
      Else
      d(T.Value) = d(T.Value) & ", " & T.Offset(, -6) & " " & T.Offset(, -4) & " " & T.Offset(, -3)

    End If
Next
[Y7].Resize(d.Count, 1) = Application.Transpose(d.keys)
[AF7].Resize(d.Count, 1) = Application.Transpose(d.items)
Set d = Nothing

'--------------------------
Set d1 = CreateObject("Scripting.Dictionary") '英文品項合併
For Each T In Range([T7], [T300].End(xlUp))
If T.Offset(, 2) = "" Then GoTo AA
   If d1(T.Value) = "" Then
      d1(T.Value) = T.Offset(, -5) & " " & T.Offset(, 2) & " " & T.Offset(, -3)
      Else
      d1(T.Value) = d1(T.Value) & ", " & T.Offset(, -5) & " " & T.Offset(, 2) & " " & T.Offset(, -3)
    End If
AA: Next

[AG7].Resize(d1.Count, 1) = Application.Transpose(d1.items)
Set d1 = Nothing

cRow = Range("Y100").End(xlUp).Row
     If cRow < 7 Then Exit Sub
     Range("X7:X" & cRow).Formula = "=VLOOKUP(Y7,T:U,2,FALSE)"
     Range("Z7:Z" & cRow).Formula = "=COUNTIF(T$7:T$490,Y7)"
     Range("AA7:AA" & cRow).Formula = "=SUMIF(T$7:T$490,Y7,S$7:S$490)"
     Range("AB7:AB" & cRow).Formula = "=ROUND(AA7/$AC$5*$AC$4,0)"
     Range("AC7:AC" & cRow).Formula = "=SUMIF(T$7:T$390,Y7,R$7:R$390)"
     Range("AD7:AD" & cRow).Formula = "=IF(AE7=""TOOLING"",AC7+Z7*10,AC7+Z7*2)"
     
End Sub
test.rar (24.77 KB)
Jess

參考:
http://forum.twbts.com/thread-22063-1-2.html

應該是 transpose 超限問題
EXCEL參考資料:
http://blog.xuite.net/smile1000mile/blog

TOP

回復 2# 准提部林

准大,您好,這個問題我也想過,但是同樣的語法,中文的資料長度更長,合併卻沒問題。
Jess

TOP

回復 3# jesscc


這文字長度如何讓transpose產生錯誤, 我也不清楚, 儘量不用~~

'英文品項合併
Dim Brr, N&, U&, TS$
Set d1 = CreateObject("Scripting.Dictionary")
ReDim Brr(1 To [T300].End(xlUp).Row, 0) '定義一個陣列[容納器]
For Each T In Range([T7], [T300].End(xlUp))
If T(1, 3) = "" Or T(1, -4) = "" Then GoTo AA 'T格或英文品項為空--略過
   TS = T(1, -4) & " " & T(1, 3) & " " & T(1, -2) '該行串接文字
   U = d1(T & "") '取得字典檔相應的[序號]
   If U > 0 Then Brr(U, 0) = Brr(U, 0) & "," & TS:   GoTo AA '如果序號大于0, 進行第2筆以後的串接
   N = N + 1:   d1(T & "") = N:   Brr(N, 0) = TS '如果序號為0, 序號遞增1存入字典檔, 填入第一個文字串
AA: Next
[AG7].Resize(N, 1) = Brr


==========================
EXCEL參考資料:
http://blog.xuite.net/smile1000mile/blog

TOP

回復 3# jesscc

2010版的Excel Application.Transpose 有兩個罩門
1.置換的元素中若有超出256字元會產生錯誤。
2.置換的列數若超出65536列會產生錯誤。

執行你的程式碼後所得字元數資料如下:
嘜頭號34【中文品項合併】為21
嘜頭號34【英文品項合併】為16

嘜頭號35【中文品項合併】為161
嘜頭號35【英文品項合併】為138

嘜頭號36【中文品項合併】為211
嘜頭號36【英文品項合併】為306

嘜頭號37【中文品項合併】為176
嘜頭號37【英文品項合併】為170

嘜頭號38【中文品項合併】為8
嘜頭號38【英文品項合併】為3

資料錯誤出現在嘜頭號36【英文品項合併】為306,已超出256的上限,而所有中文品項均合乎規則,因此不會出錯。

TOP

回復 5# Kubi
感謝K大,我爬了很多文,也確認了這一點。但是現在有個頭疼的地方,因為我的代碼媮晹釣銗L幾個地方都用了Transpose這個轉置函數,將來可能會瘋狂偵錯,該如何改寫呢?准大兩個陣列並用的寫法,幼兒園程度的我不太會用?附上更新的檔案,堶惘釦畯鴝l的寫法。
test1.rar (31.11 KB)
Jess

TOP

本帖最後由 准提部林 於 2019-10-3 09:51 編輯

回復 6# jesscc

'中文品項合併
Set d = CreateObject("Scripting.Dictionary")
For Each T In Range([T7], [T300].End(xlUp))
   If d(T.Value) = "" Then
      d(T.Value) = T.Offset(, -6) & " " & T.Offset(, -4) & " " & T.Offset(, -3)
      Else
      d(T.Value) = d(T.Value) & ", " & T.Offset(, -6) & " " & T.Offset(, -4) & " " & T.Offset(, -3)

    End If
Next
ReDim Arr(1 To d.Count, 0), Brr(1 To d.Count, 0)
For Each k In d.keys
    N = N + 1: Arr(N, 0) = k: Brr(N, 0) = d(k)
Next
[Y7].Resize(N, 1) = Arr
[AF7].Resize(N, 1) = Brr

'=========================================
'英文品項合併
Set d1 = CreateObject("Scripting.Dictionary")
For Each T In Range([T7], [T300].End(xlUp))
If T.Offset(, 2) = "" Then GoTo AA
   If d1(T.Value) = "" Then
      d1(T.Value) = T.Offset(, -5) & " " & T.Offset(, 2) & " " & T.Offset(, -3)
      Else
      d1(T.Value) = d1(T.Value) & ", " & T.Offset(, -5) & " " & T.Offset(, 2) & " " & T.Offset(, -3)
    End If
AA: Next
N = 0: ReDim Arr(1 To d1.Count, 0)
For Each k In d1.items
    N = N + 1: Arr(N, 0) = k
Next
[AG7].Resize(N, 1) = Arr



將字典檔再做一次後置處理, 納入陣列即可~~

===============================
EXCEL參考資料:
http://blog.xuite.net/smile1000mile/blog

TOP

回復 6# jesscc

兩種合併一次迴圈完成:
Set d = CreateObject("Scripting.Dictionary")
Set d1 = CreateObject("Scripting.Dictionary")
For Each T In Range([T7], [T300].End(xlUp))
   If d(T & "") = "" Then
      d(T & "") = T(, -5) & " " & T(, -3) & " " & T(, -2)
      d1(T & "") = T(, -4) & " " & T(, 3) & " " & T(, -2)
   Else
      d(T & "") = d(T & "") & ", " & T(, -5) & " " & T(, -3) & " " & T(, -2)
      d1(T & "") = d1(T & "") & ", " & T(, -4) & " " & T(, 3) & " " & T(, -2)
    End If
Next
ReDim Arr(1 To d.Count, 0), Brr(1 To d.Count, 1 To 2)
For Each k In d.keys
    N = N + 1
    Arr(N, 0) = k
    Brr(N, 1) = d(k)
    Brr(N, 2) = d1(k)
Next
[Y7].Resize(N, 1) = Arr
[AF7].Resize(N, 2) = Brr


=======================================
EXCEL參考資料:
http://blog.xuite.net/smile1000mile/blog

TOP

回復 6# jesscc

以變數代替文字串, 避免多次引用儲存格, 可提升效率,
同時, 若須更改, 只要修改一個地方即可~~
Set d = CreateObject("Scripting.Dictionary")
Set d1 = CreateObject("Scripting.Dictionary")
For Each T In Range([T7], [T300].End(xlUp))
    s1$ = T(, -5) & " " & T(, -3) & " " & T(, -2)
    s2$ = T(, -4) & " " & T(, 3) & " " & T(, -2)
    If d(T & "") = "" Then
       d(T & "") = s1
       d1(T & "") = s2
    Else
       d(T & "") = d(T & "") & ", " & s1
       d1(T & "") = d1(T & "") & ", " & s2
    End If
Next
ReDim Arr(1 To d.Count, 0), Brr(1 To d.Count, 1 To 2)
For Each k In d.keys
    N = N + 1
    Arr(N, 0) = k
    Brr(N, 1) = d(k)
    Brr(N, 2) = d1(k)
Next
[Y7].Resize(N, 1) = Arr
[AF7].Resize(N, 2) = Brr

=======================================
EXCEL參考資料:
http://blog.xuite.net/smile1000mile/blog

TOP

感謝准大的幫忙,用您中英合併的方式確實又快又正確,不過我有個問題
    s1$ = T(, -5) & " " & T(, -3) & " " & T(, -2)
    s2$ = T(, -4) & " " & T(, 3) & " " & T(, -2)
T括號裡的數值到底是以哪一欄做基準推算出來的?
Jess

TOP

        靜思自在 : 發脾氣是短暫的發瘋。
返回列表 上一主題