fork(1) download
  1. Option Explicit
  2.  
  3. Public Sub ユークリッド距離順()
  4. Const BA1 As Long = 1000 '表縦の倍率
  5. Const BA2 As Long = 100 '表横の倍率
  6. Const BA3 As Long = 10 '表高の倍率
  7. Const BA4 As Long = 1000 '裏縦の倍率
  8. Const BA5 As Long = 100 '裏横の倍率
  9. Const BA6 As Long = 10 '裏高の倍率
  10. Dim sh1 As Worksheet
  11. Dim sh2 As Worksheet
  12. Dim dicT As Object
  13. Dim maxrow1 As Long
  14. Dim namerow As Long
  15. Dim wrow As Long
  16. Dim row2 As Long
  17. Dim name As String
  18.  
  19. Dim d1 As Long, d2 As Long, d11 As Long, d12 As Long, d13 As Long, d21 As Long, d22 As Long, d23 As Long
  20. Set dicT = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  21. Set sh1 = Worksheets("Sheet1")
  22. Set sh2 = Worksheets("Sheet2")
  23. maxrow1 = sh1.Cells(Rows.Count, 1).End(xlUp).Row 'sheet1 最終行を求める
  24. If maxrow1 < 4 Then Exit Sub
  25. name = sh2.Range("B1").Value
  26. If name = "" Then Exit Sub
  27. sh2.Rows("5:" & Rows.Count).ClearContents '5行目以降クリア
  28. namerow = 0
  29. For wrow = 3 To maxrow1
  30. If name = sh1.Cells(wrow, 1).Value Then
  31. namerow = wrow
  32. End If
  33. Next
  34. If namerow = 0 Then
  35. MsgBox (name & "が存在しません")
  36. Exit Sub
  37. End If
  38. 'ユークリッド距離の計算
  39. row2 = 5
  40. For wrow = 3 To maxrow1
  41. If wrow <> namerow Then
  42. sh2.Cells(row2, 1).Value = sh1.Cells(wrow, 1).Value '表名前
  43. sh2.Cells(row2, 4).Value = sh1.Cells(wrow, 1).Value '裏名前
  44. d11 = (sh1.Cells(wrow, 2).Value - sh1.Cells(namerow, 2).Value)
  45. d12 = (sh1.Cells(wrow, 3).Value - sh1.Cells(namerow, 3).Value)
  46. d13 = (sh1.Cells(wrow, 4).Value - sh1.Cells(namerow, 4).Value)
  47. d21 = (sh1.Cells(wrow, 5).Value - sh1.Cells(namerow, 5).Value)
  48. d22 = (sh1.Cells(wrow, 6).Value - sh1.Cells(namerow, 6).Value)
  49. d23 = (sh1.Cells(wrow, 7).Value - sh1.Cells(namerow, 7).Value)
  50. d1 = d11 * d11 * BA1 + d12 * d12 * BA2 + d13 * d13 * BA3
  51. d2 = d21 * d21 * BA4 + d22 * d22 * BA5 + d23 * d23 * BA6
  52. sh2.Cells(row2, 2).Value = d1 '表距離
  53. sh2.Cells(row2, 5).Value = d2 '裏距離
  54. row2 = row2 + 1
  55. End If
  56. Next
  57. 'ソート
  58. sh2.Range("A5:B" & row2 - 1).Sort key1:=sh2.Range("B5"), Order1:=xlAscending, Header:=xlNo
  59. sh2.Range("D5:E" & row2 - 1).Sort key1:=sh2.Range("E5"), Order1:=xlAscending, Header:=xlNo
  60. sh2.Rows("10:" & Rows.Count).ClearContents '10行目以降クリア
  61. MsgBox ("完了")
  62. End Sub
  63.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty