Excelの数式エラーを隠すユーザー定義関数 iferror関数

Excel2007で追加される関数のひとつ「iferror関数」
数式の結果がエラーだった場合に返す値を指定できる関数で、もちろん空白も返すことができます。

かなり、待ち望んだ関数ですが、なんとExcel2003以下でもユーザー定義関数を用いれば使用可能。
Function IfError(formula As Variant, show As String)

On Error GoTo ErrorHandler

If IsError(formula) Then
IfError = show
Else
IfError = formula
End If

Exit Function

ErrorHandler:
Resume Next

End Function

引用:Excel での数式エラーを隠すサンプル ユーザー定義関数

posted by うずまき at 21:59 | Comment(0) | TrackBack(0) | VBA

プロットエリアに縦線を引く:Excel VBA

散布図に対角線を引くの派生版です。

グラフのプロットエリアに縦線を引くマクロです。

通常は、散布図での使用となりますが、他のグラフでも値を正しく指定できれば、思ったとおりの動作が可能です。

プロットエリアに縦線を引くマクロ


Sub Line_PA2()
'
' プロットエリアに縦線 Macro
' マクロ記録日 : 2005/04/05 ユーザー名 : uzumaki7
'
Dim PIH As Single, PIW As Single, PIT As Single, PIL As Single
Dim SetScale As Single, MaxnScale As Single, MinScale As Single

On Error GoTo ErrorHandler

If ActiveChart Is Nothing Then

MsgBox "アクティブなグラフがありません!"

Exit Sub
Else
SetScale = InputBox("値を入力してください")
If StrPtr(SetScale) = 0 Then
Exit Sub
End If
End If

With ActiveChart
With .Axes(xlCategory)
MinScale = .MinimumScale
MaxScale = .MaximumScale
End With
With .PlotArea
PIH = .InsideHeight
PIW = .InsideWidth
'プロットエリアの線の太さ分
PIT = .InsideTop - 0.25
PIL = .InsideLeft - 0.25
End With
End With

ActiveChart.Shapes.AddLine((SetScale - MinScale) / (MaxScale - MinScale) * PIW + PIL, _
PIT + PIH, _
(SetScale - MinScale) / (MaxScale - MinScale) * PIW + PIL, _
PIT).Select

ErrorHandler:

Exit Sub

End Sub

posted by うずまき at 21:54 | Comment(0) | TrackBack(0) | VBA

グラフのマーカーを間引く:Excel VBA

折れ線グラフなどで、マーカーが必要なんだけど、データ数が多すぎて困るということはないでしょうか?
スムージングでは表現できない曲線を表示しつつ、マーカーも部分的に表示したい!そんなことはないでしょうか?

そんな時、このマクロなら、任意のステップ数でマーカーを残すように間引くことが出来ます。

グラフのマーカーを間引くマクロ


Sub ThinMarker()

' マーカーを間引く Macro
' マクロ記録日 : 2005/4/7 ユーザー名 : uzumaki7

Dim objChart As Object
Dim objChartSeriesCollection As Object
Dim lngPointIndex As Long 'Pointカウント用
Dim MarkerCount As Long 'Point総数

On Error Resume Next

Application.ScreenUpdating = False

'ステップ数の入力
MarkerStep = InputBox("マーカーのステップ数を入力して下さい")

If StrPtr(MarkerStep) = 0 Then
Exit Sub
End If

'すべてのチャートに適用
For Each objChart In ActiveSheet.ChartObjects

'すべての系列に適用
For Each objChartSeriesCollection In objChart.Chart.SeriesCollection

lngPointIndex = 1

'マーカーの総数をカウント
MarkerCount = objChartSeriesCollection.Points.Count

For lngPointIndex = 2 To MarkerCount

'マーカーを間引く
If ((lngPointIndex - 1) Mod MarkerStep) <> 0 Then
objChartSeriesCollection.Points(lngPointIndex).MarkerStyle = xlNone
End If

Next lngPointIndex

Next

Next

Application.ScreenUpdating = True

End Sub

posted by うずまき at 21:51 | Comment(0) | TrackBack(0) | VBA

グラフの凡例を同じ位置にする:Excel VBA

似たようなグラフなのに、判例の位置がずれていて気持ち悪い!そんなことはないでしょうか?

今回は、シート中にあるすべてのグラフの判例を同じ位置に合わせるマクロです。
使い方は、基準となるグラフをアクティブにして、マクロを実行するだけ。すべてのグラフの判例位置が、基準としたグラフの判例位置と同じになります。

グラフの判例を同じ位置にする


Sub LegendMove()
'
' 判例は、ここよ Macro
' マクロ記録日 : 2005/04/11 ユーザー名 : uzumaki7
'

'
Dim objChart As Object
Dim LegendLeft As Long
Dim LegendTop As Long
Dim LegendHeight As Long
Dim LegendWidth As Long

On Error GoTo ErrorHandler

If ActiveChart Is Nothing Then

MsgBox "アクティブなグラフがありません!"
Exit Sub

End If

'アクティブチャートの判例位置とサイズを取得
With ActiveChart.Legend
LegendLeft = .Left
LegendTop = .Top
LegendHeight = .Height
LegendWidth = .Width
End With

'すべてのチャートに適用
For Each objChart In ActiveSheet.ChartObjects

With objChart.Chart.Legend
.Left = LegendLeft
.Top = LegendTop
.Height = LegendHeight
.Width = LegendWidth
End With

Next

ErrorHandler:

Exit Sub
End Sub

posted by うずまき at 21:49 | Comment(0) | TrackBack(0) | VBA

散布図に対角線を引く:Excel VBA

散布図グラフを正方形化する:Excel VBAと関連です。

 散布図では傾き:1の対角線を引きたい場合があります。しかし、Excelにはそんな機能はありません。そこで、またまたVBAの登場です。

 使い方は、グラフを選択してマクロを実行するだけです。

散布図に対角線を引くマクロ


Sub Line_PA()
'
' プロットエリアに対角線 Macro
' マクロ記録日 : 2001/8/29 ユーザー名 : uzumaki7
'
Dim PIH As Single, PIW As Single, PIT As Single, PIL As Single

On Error GoTo ErrorHandler

If ActiveChart Is Nothing Then
MsgBox "アクティブなグラフがありません!"
Exit Sub
End If

With ActiveChart.PlotArea
PIH = .InsideHeight
PIW = .InsideWidth
'プロットエリアの線の太さ分
PIT = .InsideTop - 0.25
PIL = .InsideLeft - 0.25
End With

ActiveChart.Shapes.AddLine(PIL, PIT + PIH, PIL + PIW, PIT).Select

ErrorHandler:
Exit Sub
End Sub

posted by うずまき at 21:46 | Comment(1) | TrackBack(0) | VBA

散布図グラフを正方形化する:Excel VBA

 Excelのグラフは機能が貧弱で、なんでこの機能がないの?なんて事がよくあります。私は、機能を補うために、VBAを利用しています。
 散布図では横軸と縦軸が同じ場合、ぴったりサイズを合わせたくないですか?

 そんなあなたのためのVBAを紹介します。
 使い方は、グラフを選択して、マクロを実行するだけ。ツールバーにアイコンを登録しておくと便利です。

 散布図に対角線を引くマクロとあわせるとさらに便利です。

散布図を正方形化するマクロ


Sub PAtoSQR()
'
' 散布図用 散布図は正方形Macro
' マクロ記録日 : 2001/8/29 ユーザー名 : uzumaki7
On Error GoTo ErrorHandler

If ActiveChart Is Nothing Then
MsgBox "アクティブなグラフがありません!"
Exit Sub
End If

With ActiveChart
'フォントの自動サイズ変更の停止
.ChartArea.AutoScaleFont = False

'最大スケール合わせ(最大値に合わせる)
If .Axes(xlCategory).MaximumScale >= .Axes(xlValue).MaximumScale Then
.Axes(xlValue).MaximumScale = .Axes(xlCategory).MaximumScale
.Axes(xlCategory).MaximumScaleIsAuto = False
Else
.Axes(xlCategory).MaximumScale = .Axes(xlValue).MaximumScale
.Axes(xlValue).MaximumScaleIsAuto = False
End If

'最小スケール合わせ(最小値に合わせる)
If .Axes(xlCategory).MinimumScale <= .Axes(xlValue).MinimumScale Then
.Axes(xlValue).MinimumScale = .Axes(xlCategory).MinimumScale
.Axes(xlCategory).MinimumScaleIsAuto = False
Else
.Axes(xlCategory).MinimumScale = .Axes(xlValue).MinimumScale
.Axes(xlValue).MinimumScaleIsAuto = False
End If

'間隔合わせ
If .Axes(xlCategory).MajorUnit >= .Axes(xlValue).MajorUnit Then
.Axes(xlValue).MajorUnit = .Axes(xlCategory).MajorUnit
.Axes(xlCategory).MajorUnitIsAuto = False
Else
.Axes(xlCategory).MajorUnit = .Axes(xlValue).MajorUnit
.Axes(xlValue).MajorUnitIsAuto = False
End If

'正方形化
With .PlotArea
.Width = .Width - .InsideWidth + .InsideHeight
End With
End With

ErrorHandler:
Exit Sub
End Sub

posted by うずまき at 21:41 | Comment(0) | TrackBack(0) | VBA

Outlookでメールの添付ファイルを削除する:Outlook VBA

Outlookで特定フォルダおよびそのサブフォルダ内の特定キーワードを含む添付ファイルを削除するVBA


概要

 Outlookには、添付ファイルを管理する機能がありません。jpg,pdf,doc,excelなど日々いろいろなファイルが添付され送られてくることでしょう。この添付ファイル、ハードディスクに保存することは出来ても、なんとOutlook上から消すことは出来ません。また、気づくとOutlookのデータファイルであるpstファイルがとんでもなく肥大化していることもザラです。
 そこで、いろいろと添付ファイルを削除するファイルを検索してみたのですが、そういった日本製のツールも見つかりませんでした。そこで、Outlook VBAで自作することにしました。

VBAコード

Option Compare Text

Sub SetFolder()
Dim olApp As Outlook.Application
Dim nspNameSpace As NameSpace
Dim fldFolder As MAPIFolder
Dim strAttachFileName As String

Dim intDelCount As Integer
Dim lngDelFileSize As Long

Set olApp = New Outlook.Application ' Application オブジェクト
Set nspNameSpace = olApp.GetNamespace("MAPI") ' Namespace オブジェクト
Set fldFolder = nspNameSpace.PickFolder

Dim blDelSubFolder As Boolean

If fldFolder Is Nothing Then
Exit Sub
End If

strAttachFileName = InputBox("検索する添付ファイル名の名前の一部を入れてください")

If StrPtr(strAttachFileName) = 0 Then
Exit Sub
End If

If MsgBox("サブフォルダの削除も行いますか?", vbYesNo) = vbYes Then
blDelSubFolder = True
Else
blDelSubFolder = False
End If

Call DelAttachments(fldFolder, blDelSubFolder, strAttachFileName, _
intDelCount, lngDelFileSize)

MsgBox (intDelCount & "件," & Format(lngDelFileSize / 1024, 0) & _
"KBの添付ファイルを削除しました")

End Sub

Sub DelAttachments(fldFolder As Outlook.MAPIFolder, _
ByVal blDelSubFolder As Boolean, _
ByVal strAttachFileName As String, _
ByRef intDelCount As Integer, _
ByRef lngDelFileSize As Long)

Dim objItem As Object
Dim dteCreateDate As Date
Dim strSubject As String
Dim strItemType As String
Dim intCounter As Integer
Dim objAttachment As Attachments

Dim strOutputMessage As String

On Error Resume Next

'フォルダ内のすべてのアイテムに再帰関数
If blDelSubFolder And fldFolder.Folders.Count > 0 Then
For Each objItem In fldFolder.Folders
Call DelAttachments(objItem, blDelSubFolder, strAttachFileName, intDelCount, lngDelFileSize)
Next objItem
End If

'フォルダにアイテムがあり、それがメールであるか確認
If fldFolder.Items.Count > 0 And fldFolder.Items(1).Class = olMail Then
'フォルダ内のアイテムを削除するか確認する
' If MsgBox("Folder '" & fldFolder.Name & "' (Contains " & fldFolder.Items.Count & _
" items) の添付ファイルを削除しますか?", vbYesNo, 添付ファイルの削除) = vbYes Then

For Each objItem In fldFolder.Items
With objItem
intCounter = .Attachments.Count
Do While intCounter > 0
If .Attachments(intCounter).FileName Like ("*" & strAttachFileName & "*") Then
lngDelFileSize = lngDelFileSize + .Size
.Attachments.Remove intCounter
.Save
lngDelFileSize = lngDelFileSize - .Size
intDelCount = intDelCount + 1
End If
intCounter = intCounter - 1
Loop
End With
Next objItem
' End If
End If

End Sub


導入方法

 まず、[ツール]-[マクロ]-[Visual Basic Editor]から、Visual Basic Editorを起動します。上のVBAコードをコピーして、適当なモジュールファイルか、ThisOutlookSessionに貼り付けてください。
 さらに、[ツール]-[マクロ]-[セキュリティ]から、マクロのセキュリティレベルを『中』にする必要があります(要再起動)。ただ、マクロの実行を許可することで、マクロウィルスへの感染の危険性も高まりますので、気をつけてください。

使い方

 [ツール]-[マクロ]-[マクロ]から、SetFolderを実行します。対象フォルダを選択し、添付ファイル名の一部を入力することで、対象ファイルを削除します。
 おすすめは、拡張子での指定です。部分一致ですので、ワイルドカードは必要ありません。また、大文字・小文字の区別もしません。『.pdf』などを指定するとよいでしょう。
 サブフォルダも対象にするか聞いてきますので、必要に応じて選んでください。

補足事項

 VBA実行後は、Outlookデータファイルの圧縮(最適化)を忘れずに!!データファイルが小さくなります

posted by うずまき at 21:24 | Comment(6) | TrackBack(0) | VBA

広告


この広告は60日以上更新がないブログに表示がされております。

以下のいずれかの方法で非表示にすることが可能です。

・記事の投稿、編集をおこなう
・マイブログの【設定】 > 【広告設定】 より、「60日間更新が無い場合」 の 「広告を表示しない」にチェックを入れて保存する。


×

この広告は90日以上新しい記事の投稿がないブログに表示されております。