VBA 使えるソースコード

つたないソースコードを載せます。これは、他人の書いたソースコードを読む練習に最適です。初心者の方は、どうしたらきれいになるかなど考えながら活用してください。

VBA 4種類のデータの比較を自動でしよう

Option Explicit

Sub fig_kinds_4()
'
' fig_kinds_4 Macro
' 4種類のデータの比較を行います。
'

'
Dim title As String
title = InputBox(" グラフタイトルを入力してください。")

Dim n As Integer: n = 0

Do Until Cells(n + 5, 1).Value = ""
n = n + 1
Loop

Cells(2, 7).Value = n


Range("B6:B" & n + 4).Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatterLines).Select
ActiveChart.SetSourceData Source:=Range("Sheet2!$B$6:$B$" & n + 4)
'Application.CutCopyMode = False
ActiveChart.FullSeriesCollection(1).Name = Cells(5, 2).Value
ActiveChart.FullSeriesCollection(1).XValues = "=Sheet2!$A$6:$A$" & n + 4
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = Cells(5, 3).Value
ActiveChart.FullSeriesCollection(2).XValues = "=Sheet2!$A$6:$A$" & n + 4
ActiveChart.FullSeriesCollection(2).Values = "=Sheet2!$C$6:$C$" & n + 4
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(3).Name = Cells(5, 4).Value
ActiveChart.FullSeriesCollection(3).XValues = "=Sheet2!$A$6:$A$" & n + 4
ActiveChart.FullSeriesCollection(3).Values = "=Sheet2!$D$6:$D$" & n + 4
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(4).Name = Cells(5, 5).Value
ActiveChart.FullSeriesCollection(4).XValues = "=Sheet2!$A$6:$A$" & n + 4
ActiveChart.FullSeriesCollection(4).Values = "=Sheet2!$E$6:$E$" & n + 4
ActiveChart.SetElement (msoElementLegendRight)

ActiveChart.Axes(xlCategory, xlPrimary).HasTitle = True
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = Cells(2, 2).Value
ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = Cells(2, 4).Value
ActiveSheet.ChartObjects(1).Chart.HasTitle = True
ActiveSheet.ChartObjects(1).Chart.ChartTitle.Text = title
End Sub

 

結果 ↓  グラフを作成しました

f:id:yoronx:20200307170216p:plain

 

VBA 折れ線グラフをVBAで作ってみよう

Sub Macro7()
'
' Macro7 Macro
'

'
Dim x_title As String, y_title As String, data_title As String, title As String


data_title = InputBox(" データの名称を入力してください。")
x_title = InputBox("横軸の名称を入力してください。")
y_title = InputBox("縦軸の名称を入力してください。")
title = InputBox(" グラフタイトルを入力してください。")

Dim n As Integer: n = 0

Do Until Cells(n + 5, 1).Value = ""
n = n + 1
Loop

Cells(2, 7).Value = n

Range("B5:B" & n + 4).Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmooth).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$B$5:$B$14")
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveChart.FullSeriesCollection(1).Name = data_title
ActiveChart.FullSeriesCollection(1).XValues = "=Sheet1!$A$5:$A$" & n + 4
ActiveChart.SetElement (msoElementLegendRight)
ActiveChart.Axes(xlCategory, xlPrimary).HasTitle = True
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = x_title
ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = y_title
ActiveSheet.ChartObjects(1).Chart.HasTitle = True
ActiveSheet.ChartObjects(1).Chart.ChartTitle.Text = title
End Sub

Python numpyの使い方のまとめ(例)

import numpy as np

def myPrint(ary):
print(ary)
print('length = ' + str(ary.size))

ary1 = np.array([3,5,7,1,4])
myPrint(ary1)

ary1[3] = 8
myPrint(ary1)

ary1 = np.insert(ary1, 2, 3)
myPrint(ary1)

ary1 = np.append(ary1,10)
myPrint(ary1)

ary1 = np.append(ary1,[9,12])
myPrint(ary1)

ary1 = np.delete(ary1,3)
myPrint(ary1)

myPrint(ary1+1)

ary2 = np.zeros(6)
myPrint(ary2)

Python UNIX時間の確認

import time

time1 = time.time()
#UNIX時間で表現した現在の時刻を返す
#UNIX時間は1970年1月1日午前0時からの秒数である
time.sleep(10)
#指定された値の秒数だけスレッドの実行を停止する
time2 = time.time()
print('time1 = ' + str(time1))
print('time2 = ' + str(time2))

Python プログラムの実行時間って、実際どのくらい??

import time

n = 10000

startTime = time.time()
for i in range(n):
pass #passはなにも処理しないことを意味する
endTime = time.time()
elapsedTime = endTime - startTime
print("elapsed time1 = " + str(elapsedTime))

startTime = time.time()
for i in range(n):
for j in range(n):
pass
endTime = time.time()
elapsedTime = endTime - startTime
print("elapsed time2 = " + str(elapsedTime))

 

結果 ↓

f:id:yoronx:20200303224817p:plain

 

Python クラスとメソッドの例

# -*- coding: utf-8 -*-

class MyStack:
def __init__(self):
self.ary = [1,2,3]

def push(self,element):
self.ary.append(element)

def pop(self):
self.ary.pop()

def printall(self):
for n in self.ary:
print(n)

def isEmpty(self):
if self.ary == []:
return True
else:
return False

def getSize(self):
return len(self.ary)

 


if __name__ == '__main__':
mystack = MyStack()

while True:
command = input('input command: ')
if command == 'push':
data = input('input data: ')
elif command == 'pop':
print(mystack.pop())
elif command == 'printall':
print(mystack.printall())
elif command == 'isEmpty':
print(mystack.isEmpty())
elif command == 'getSize':
print(mystack.getSize())
elif command == 'end':
break

Python 数列から数字を見つけよう2

import random
import sys

n = sys.argv[1]
target = sys.argv[2]
list_int = []#空のリストを生成
flag = False
intn = int(n)
inttarget = int(target)
while intn > 0:
list_int.append(random.randint(0,9))
intn = intn - 1
for val in list_int:
if val == inttarget:
flag = True
break
print(list_int)
if flag:
print(target + 'yes')
else:
print(target + 'no')