teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

VBAコードの追記

2018/09/30 08:29

投稿

退会済みユーザー
answer CHANGED
@@ -2,7 +2,96 @@
2
2
  > 50サイトづつに分けると問題なくプログラムが最後まで動きます。
3
3
 
4
4
  コードは読んでいませんが、単純に考えられるのはメモリリークとかでしょうか。
5
- 50回毎にieオブジェクトを破棄して、改めてCreateObjectするようにしたら改善するかもしれません。
5
+ 例えば30回毎にieオブジェクトを破棄して、改めてCreateObjectするようにしたら改善するかもしれません。
6
+ ```VBA
7
+ Private Function urlscr(btAry As String)
6
8
 
9
+ Application.ScreenUpdating = False
10
+
11
+ Dim i As Long
12
+ Dim urlLen As Long
13
+ Dim urlStr() As String
14
+ Dim tAry() As String
15
+
16
+ urlStr() = Split(btAry, ",")
17
+ urlLen = UBound(urlStr)
18
+
19
+ ReDim tAry(urlLen, 1) As String
20
+
21
+ For i = 0 To urlLen - 1
22
+ tAry(i, 0) = urlStr(i)
23
+ Sleep (1)
24
+ Next
25
+
26
+ Dim ie As InternetExplorer
27
+ Dim htdoc As HTMLDocument
28
+ Dim tagName As HTMLElementCollection
29
+
30
+ '変更箇所 ここから
31
+ Const ResetPages = 30
32
+
33
+ For i = 0 To urlLen - 1
34
+ If i Mod ResetPages = 0 Then
35
+ If Not ie Is Nothing Then
36
+ ie.Quit
37
+ End If
38
+
39
+ Set ie = CreateObject("InternetExplorer.Application")
40
+
41
+ ie.Visible = True
42
+ ie.Top = True
43
+ ie.Left = True
44
+ ie.Width = 500
45
+ ie.Height = 500
46
+ End If
47
+ '変更箇所 ここまで
48
+
49
+ Debug.Print i & " / " & urlLen - 1
50
+ ie.Navigate tAry(i, 0)
51
+
52
+ Do While ie.Busy Or ie.ReadyState < READYSTATE_COMPLETE
53
+ DoEvents
54
+ Loop
55
+
56
+ Set htdoc = ie.document
57
+
58
+ If InStr(htdoc.Url, "") > 0 Then
59
+ For Each tagName In htdoc.getElementsByClassName("")
60
+ If InStr(tagName.outerText, "") > 0 Then
61
+ tAry(i, 1) = "×"
62
+ Exit For
63
+ End If
64
+ Next
65
+ End If
66
+
67
+
68
+ Set htdoc = Nothing
69
+ Set tagName = Nothing
70
+ Sleep 1
71
+ Next
72
+
73
+ ie.Quit
74
+
75
+ Set ie = Nothing
76
+ Set htdoc = Nothing
77
+
78
+ Dim num As Long
79
+ Dim ws As Worksheet
80
+ Set ws = ActiveSheet
81
+ num = ws.Cells(1, 1).CurrentRegion.Rows.Count - 1
82
+
83
+ For i = 0 To num - 2
84
+ If ws.Cells(i + 2, 1).Value = tAry(i, 0) Then
85
+ ws.Cells(i + 2, 3).Value = tAry(i, 1)
86
+ End If
87
+ Sleep 1
88
+ Next
89
+
90
+ Set ws = Nothing
91
+
92
+ End Function
93
+ ```
94
+
95
+
7
96
  それと、100サイト回した時に、何回目くらいでエラーが出るのか、エラーが出たサイトを序盤に実行した場合に同じサイトでエラーにならないか。
8
97
  といった法則性について調査されていないので調べてみたほうが良いかもしれません。