EXCEL メモ
Q1.日付データから年月の表示に変更するには
A1. =CONCATENATE(TEXT(F2,"YYYY"),"/",TEXT(F2,"MM"))
Q2. 文字形式で表されている日付をシリアル値に変更するには
A2. 日付関数のDATEVALUE(F4) を使用する。
Q3. 文字形式の列を一度にシリアルに変換するマクロ
A3. Sub MASDate2()
'
' MASDate2 Macro
' マクロ記録日 : 2002 9 22 ユーザー名 : Hiroshi Ohno
'
' Keyboard Shortcut: Ctrl+n
Dim i
'Cells(Rows.Count, 1)←A列の最後の行
i = Cells(Rows.Count, 1).End(xlUp).Row
Columns("E:E").Select
Selection.Insert Shift:=xlToRight
'Range("E2").Select
Cells(2, 5).Select
ActiveCell.FormulaR1C1 = "=DATEVALUE(RC[-1])"
Selection.Copy
Selection.CurrentRegion.Select
Selection.End(xlDown).Select
' Range("E3:E128").Select
Range(Cells(3, 5), Cells(i, 5)).Select
' Range("E128").Activate
Cells(i, 5).Activate
ActiveSheet.Paste
Selection.End(xlUp).Select
Columns("E:E").Select
' Range("E2").Activate
Cells(2, 5).Activate
Application.CutCopyMode = False
' Range("E2:E128").Select
Range(Cells(2, 5), Cells(i, 5)).Select
Selection.Copy
' Range("D2").Select
Cells(2, 4).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormatLocal = "dd-mmm-yy"
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Columns("F:F").Select
Selection.Insert Shift:=xlToRight
' Range("F2").Select
Cells(2, 6).Select
ActiveCell.FormulaR1C1 = "=DATEVALUE(RC[-1])"
Selection.Copy
Selection.CurrentRegion.Select
Selection.End(xlDown).Select
' Range("F3:F128").Select
Range(Cells(3, 6), Cells(i, 6)).Select
' Range("F128").Activate
Cells(i, 6).Activate
ActiveSheet.Paste
Selection.End(xlUp).Select
' Range("F2:F128").Select
Range(Cells(2, 6), Cells(i, 6)).Select
Application.CutCopyMode = False
Selection.Copy
' Range("E2").Select
Cells(2, 5).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormatLocal = "dd-mmm-yy"
Columns("F:F").Select
Selection.Delete Shift:=xlToLeft
Columns("G:G").Select
Selection.Insert Shift:=xlToRight
' Range("G2").Select
Cells(2, 7).Select
ActiveCell.FormulaR1C1 = "=DATEVALUE(RC[-1])"
Selection.Copy
Selection.CurrentRegion.Select
Selection.End(xlDown).Select
' Range("G3:G128").Select
Range(Cells(3, 7), Cells(i, 7)).Select
' Range("G128").Activate
Cells(i, 7).Activate
ActiveSheet.Paste
' Range("G127").Select
Cells(127, 7).Select
Selection.End(xlUp).Select
' Range("G2:G128").Select
Range(Cells(2, 7), Cells(i, 7)).Select
Application.CutCopyMode = False
Selection.Copy
' Range("F2").Select
Cells(2, 6).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormatLocal = "dd-mmm-yy"
Columns("G:G").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
End Sub
Q4. 1-4,7,9 で記述された内容から、3という数字が含まれているかどうかを
判定するマクロ。 Yesならば1を返し、Noならば0を返します。
Private Sub Command1_Click()
' Dim ModIn(0) As String
' Mod番号の長さの取得 結果をLen1とする。
Len1 = Len(Text2.Text)
Print Len1
' ReDimを使う理由は、配列数が変数のため、Dimは定数のみ
ReDim ModIn(Len1 + 1) As String
ReDim ModNo(Len1) As String
' 最終配列に@を入力し、終わりを示す
ModIn(Len1 + 1) = "@"
' Mod 番号を1文字ずつ分けてModInの配列に入れる
For i = 1 To Len1
ModIn(i) = Mid(Text2.Text, i, 1)
Next i
Print ModIn(Len1)
' Mod 番号の数字の切り出し
j = 1
For i = 1 To Len1
If CodeFlag(ModIn(i)) = 0 Then
ModNo(j) = ModIn(i)
While CodeFlag(ModIn(i + 1)) = 0
ModNo(j) = ModNo(j) + ModIn(i + 1)
i = i + 1
Wend
j = j + 1
ElseIf CodeFlag(ModIn(i)) = 1 Or CodeFlag(ModIn(i)) = 2 Then
ModNo(j) = ModIn(i)
j = j + 1
Else
End If
Next i
Print "j=", j
Print "ModNo(j)", ModNo(j)
'For i = 1 To j
' Text3.Text = Text3.Text & ModNo(i)
'Next i
' Step2
ReDim ModNo2(Val(ModNo(j - 1)))
k = 1
For i = 1 To j - 1
If ModNo(i) <> "," And ModNo(i) <> "-" Then
ModNo2(k) = ModNo(i)
k = k + 1
ElseIf ModNo(i) = "-" Then
n = 1
For m = Val(ModNo(i - 1)) + 1 To Val(ModNo(i + 1))
ModNo2(k) = Val(ModNo(i - 1)) + n
n = n + 1
k = k + 1
Next m
i = i + 2
End If
Next i
Text3.Text = ""
'For i = 1 To k - 1
' Text3.Text = Text3.Text & ModNo2(i)
'Next i
Text3.Text = 0
For i = 1 To k - 1
If ModNo2(i) = Text1.Text Then
Text3.Text = 1
i = k - 1
End If
Next i
End Sub
Function CodeFlag(code1 As String) As Integer
flag1 = -1
If Asc(code1) >= 48 And Asc(code1) <= 57 Then
' Print Val(code1)
flag1 = 0
' Print ModIn(i)
End If
If code1 = "," Then flag1 = 1
If code1 = "-" Then flag1 = 2
If flag1 = -1 Then
' Print "Error"
End If
CodeFlag = flag1
End Function
Q5. P1座標にある文字がN2座標に含まれている場合は、P1座標の文字を表示し、
そうでない場合は、アンダーバーを表示する関数。
A5. =IF(ISERROR(FIND(P$1,$N2,1)),"_",P$1)
BACKで元に戻って下さい。