質問編集履歴
4
二次元配列の構造の図を追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -163,3 +163,11 @@
|
|
163
163
|
コード
|
164
164
|
|
165
165
|
```
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
2次元配列の図
|
172
|
+
|
173
|
+
![![イメージ説明](2bca722ba1d551a86e22322b15ab09a6.jpeg)](14b0beda04b34bc04dc8d9e1b7084417.jpeg)
|
3
間違って削除したのを戻し増しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,11 +6,119 @@
|
|
6
6
|
|
7
7
|
1.VBAで構造体内の一項目を使用して構造体全体をソートさせるのはどうすればいいでしょうか?
|
8
8
|
|
9
|
+
構造体のコード
|
10
|
+
|
11
|
+
```VBA
|
12
|
+
|
13
|
+
Public Const ranking_table_y_max = 4000
|
14
|
+
|
15
|
+
Public Const ranking_table_x_max = 10
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
Type ranking_table
|
20
|
+
|
21
|
+
officename As String '事業所名
|
22
|
+
|
23
|
+
sei_corporationname As String '法人正式名
|
24
|
+
|
25
|
+
corporationname As String '法人名
|
26
|
+
|
27
|
+
corporationno As Long '法人番号
|
28
|
+
|
29
|
+
zen_salesamount As Currency '前年売上高
|
30
|
+
|
31
|
+
kon_salesamount As Currency '当年売上高
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
End Type
|
36
|
+
|
37
|
+
Public ranking(0 To ranking_table_x_max, 0 To ranking_table_y_max) As ranking_table '地区と法人
|
38
|
+
|
39
|
+
'rankingの部分が配列名
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
```
|
44
|
+
|
45
|
+
|
46
|
+
|
9
47
|
|
10
48
|
|
11
49
|
2.下記のようにソートのプログラムを書いてみたのですが、値は動きましたが、ソートされませんでした。
|
12
50
|
|
13
51
|
※https://excel-ubara.com/excelvba5/EXCELVBA229.html こちらのサイトを参考しました。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
```VBA
|
56
|
+
|
57
|
+
Public Function zyun_csvimport_Sort() As Boolean
|
58
|
+
|
59
|
+
On Error GoTo Sort_ERROR
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
Dim vSwap As ranking_table
|
66
|
+
|
67
|
+
Dim i As Long
|
68
|
+
|
69
|
+
Dim j As Long
|
70
|
+
|
71
|
+
Dim k As Integer
|
72
|
+
|
73
|
+
Dim keyPos As Long: keyPos = 1
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
For i = LBound(ranking, 1) To UBound(ranking, 1)
|
82
|
+
|
83
|
+
For j = LBound(ranking) To UBound(ranking) - 1
|
84
|
+
|
85
|
+
If ranking(j, keyPos).kon_salesamount < ranking(j + 1, keyPos).kon_salesamount Then
|
86
|
+
|
87
|
+
For k = LBound(ranking, 2) To UBound(ranking, 2)
|
88
|
+
|
89
|
+
vSwap = ranking(j, k)
|
90
|
+
|
91
|
+
ranking(j, k) = ranking(j + 1, k)
|
92
|
+
|
93
|
+
ranking(j, k) = vSwap
|
94
|
+
|
95
|
+
Next
|
96
|
+
|
97
|
+
End If
|
98
|
+
|
99
|
+
Next j
|
100
|
+
|
101
|
+
Next i
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
zyun_csvimport_Sort = True
|
106
|
+
|
107
|
+
Exit Function
|
108
|
+
|
109
|
+
Sort_ERROR:
|
110
|
+
|
111
|
+
zyun_csvimport_Sort = False
|
112
|
+
|
113
|
+
MsgBox "zyun_csvimport_Sort(ソート部分)でエラー"
|
114
|
+
|
115
|
+
End Function
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
```
|
120
|
+
|
121
|
+
|
14
122
|
|
15
123
|
|
16
124
|
|
2
ソートに入る前の部分のコードを記載しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,112 +14,44 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
+
ソートに入る前の部分のコード
|
18
|
+
|
17
19
|
```VBA
|
18
20
|
|
19
|
-
|
21
|
+
Close #filenumber
|
20
22
|
|
23
|
+
'ソート部分-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
|
24
|
+
|
25
|
+
'ここまでは動きます。 *********************************↓
|
26
|
+
|
21
|
-
|
27
|
+
If zyun_csvimport_Sort() = False Then
|
28
|
+
|
29
|
+
Else
|
30
|
+
|
31
|
+
End If
|
22
32
|
|
23
33
|
|
24
34
|
|
25
|
-
|
26
|
-
|
27
|
-
Dim vSwap
|
28
|
-
|
29
|
-
Dim i As Long
|
30
|
-
|
31
|
-
Dim j As Long
|
32
|
-
|
33
|
-
Di
|
35
|
+
Debug.Print "zyun_csvimport_Sort"
|
34
36
|
|
35
37
|
|
36
38
|
|
37
|
-
|
39
|
+
'出力部分-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
|
38
40
|
|
41
|
+
zyun_csvimport = True
|
39
42
|
|
40
|
-
|
41
|
-
'バブルソート start
|
42
|
-
|
43
|
-
For i = LBound(ranking, 1) To UBound(ranking, 1)
|
44
|
-
|
45
|
-
For j = LBound(ranking) To UBound(ranking) - 1
|
46
|
-
|
47
|
-
If ranking(j, i).kon_salesamount < ranking(j, i + 1).kon_salesamount Then
|
48
|
-
|
49
|
-
For k = LBound(ranking, 2) To UBound(ranking, 2)
|
50
|
-
|
51
|
-
vSwap = ranking(j, k).kon_salesamount
|
52
|
-
|
53
|
-
|
43
|
+
MsgBox "正常終了しました"
|
54
|
-
|
55
|
-
ranking(j + 1, k).kon_salesamount = vSwap
|
56
|
-
|
57
|
-
Next
|
58
|
-
|
59
|
-
End If
|
60
|
-
|
61
|
-
Next j
|
62
|
-
|
63
|
-
Next i
|
64
|
-
|
65
|
-
'バブルソート end
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
'ここは関係ない
|
70
|
-
|
71
|
-
zyun_csvimport_Sort = True '正常終了の場合は Trueを返す
|
72
44
|
|
73
45
|
Exit Function
|
74
46
|
|
75
|
-
|
47
|
+
csvimport_ERROR:
|
76
48
|
|
77
|
-
zyun_csvimport
|
49
|
+
zyun_csvimport = False
|
78
50
|
|
79
|
-
MsgBox "zyun_csvimport
|
51
|
+
MsgBox "zyun_csvimportでエラー"
|
80
52
|
|
81
|
-
|
53
|
+
End Function
|
54
|
+
|
55
|
+
コード
|
82
56
|
|
83
57
|
```
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
3.構造体はこちらのものです。
|
88
|
-
|
89
|
-
```VBA
|
90
|
-
|
91
|
-
Type ranking_table
|
92
|
-
|
93
|
-
officename As String '事業所名
|
94
|
-
|
95
|
-
sei_corporationname As String '法人正式名
|
96
|
-
|
97
|
-
corporationname As String '法人名
|
98
|
-
|
99
|
-
corporationno As Long '法人番号
|
100
|
-
|
101
|
-
zen_salesamount As Currency '前年売上高
|
102
|
-
|
103
|
-
kon_salesamount As Currency '当年売上高
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
End Type
|
108
|
-
|
109
|
-
Public ranking(0 To ranking_table_x_max, 0 To ranking_table_y_max) As ranking_table '地区と法人
|
110
|
-
|
111
|
-
'rankingの部分が配列名
|
112
|
-
|
113
|
-
```
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
環境
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
win10
|
124
|
-
|
125
|
-
Excel 2016
|
1
使用環境を追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -111,3 +111,15 @@
|
|
111
111
|
'rankingの部分が配列名
|
112
112
|
|
113
113
|
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
環境
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
win10
|
124
|
+
|
125
|
+
Excel 2016
|