ExcelVBAへの道 第8章 コード例2
|
罫線で格子を引く Range("A1:D9").Borders.Weight = xlThick Range("A1:D10").Borders.Weight = xlHairline Range("A1:D11").Borders.Weight = xlThin Range("A1:D12").Borders.Weight = xlMedium 罫線の種類を設定する Range("A1:D12").Borders.LineStyle = xlDash Range("A1:D13").Borders.LineStyle = xlContinuous Range("A1:D14").Borders.LineStyle = xlDashDot Range("A1:D15").Borders.LineStyle = xlDashDotDot Range("A1:D16").Borders.LineStyle = xlDot Range("A1:D17").Borders.LineStyle = xlDouble Range("A1:D18").Borders.LineStyle = xlSlantDashDot Range("A1:D19").Borders.LineStyle = xlLineStyleNone 書式クリア Range("A1:D19").ClearFormats アクティブシートを印刷する ActiveSheet.PrintOut 印刷プレビュー ActiveSheet.PtintPreview 用紙を横に設定する ActiveSheet.PageSetup.Orientation = xlLandscape 印刷範囲を設定 Sheet1.PageSetup.PrintArea = "A1:F7" フォームの表示位置 UserForm1.StartUpPosition = 0 UserForm1.StartUpPosition = 1 UserForm1.StartUpPosition = 2 UserForm1.StartUpPosition = 3 コンボボックスにリストを設定 ComboBox1.ListFillRange = "A1:A9" インプットボックスのデータを取得 Dim Mymsg As String Mymsg = InputBox("文字を入力") ダイアログボックスを表示 Dialogs(xlDialogOpen).Show 型変換 ActiveSheet.Cells(1,1).Value = Cbyte("25") ActiveSheet.Cells(1,1).Value = CInt(1234.567) ActiveSheet.Cells(1,1).Value = CLng("123.456789") ActiveSheet.Cells(1,1).Value = CSng("1.2345678") ActiveSheet.Cells(1,1).Value = CDbl("1.23456677") ActiveSheet.Cells(1,1).Value = CStr(12345) ActiveSheet.Cells(1,1).Value = Cdate("2004/2/2") ActiveSheet.Cells(1,1).Value = Ccur(1234) ActiveSheet.Cells(1,1).Value = Cvar(I & "50") 文字列を数値に変換 Range("A1").Value = Val("123456") 数値を文字列に変換 Range("A1").Value = Str(12345) エラー番号の意味を表示する ActiveCell.Value = Error(52) ファイル名または番号が不正です。 日付の年・月・日を表示する ActiveCell.Value = Year("平成13年2月2日") ActiveCell.Value = Month("平成13年2月3日") ActiveCell.Value = Day("平成13年2月4日") 時刻の時・分・秒を表示する ActiveCell.Value = Hour("午後1時45分32秒") ActiveCell.Value = Minute("午後1時45分33秒") ActiveCell.Value = Second("午後1時45分34秒") 日付の曜日番号を表示する ActiveCell.Value = WeekDay("平成15年4月16日") 1は日曜日 2は月曜日 3は火曜日 4は水曜日 5は木曜日 6は金曜日 7は土曜日 Dim Myday As Integer Myday = WeekDay("平成15年3月3日") Select Case Myday Case 1 ActiveCell.Value = "日曜日" Case 2 ActiveCell.Value = "火曜日" End Select 現在の日付・時刻・日時を表示する ActiveCell.Value = Date ActiveCell.Value = Time ActiveCell.Value = Now 文字列の長さを表示する Dim str As String str = "あいうえお" ActiveCell.Value = Len(str) ActiveCell.Value = LenB(str) 文字列を左から取得する Dim str As String str = "ABCDEFG" ActiveCell.Value = Left(str,3) ABC 文字列を右から取得する Dim str As String str = "ABCDEFG" ActiveCell.Value = Right(str,3) 文字列の指定位置から取得する Dim str As String str = "ABCDEFG" ActiveCell.Value = Mid(str,3,4) 文字列の形式を変換する Dim str As String str = "ABCDEFG") ActiveCell.Value = strConv(str,vbWide) vbUpperCase アルファベットを大文字に vbLowerCase アルファベットを小文字に vbProperCase 先頭の文字を大文字に vbWide 半角文字を全角文字に vbNarrow 全角文字を半角文字に vbKatakana ひらがなをカタカナに vbHiragana カタナカをひらがなに vbUnicode vbFromUnicode 文字コードで文字を指定する ActiveCell.Value = Chr(56) テキストを読み込む Workbooks.OpenText "C:\abc.txt") セル内容を検索する Dim x With Range("A1:K40") Set x = .Find(検索文字,LookIn:=xlValues) If Not x Is Nothing Then ActiveCell.Value = x End If End With 違うシートに移動して自動的にスクロールする Applocation.Goto Worksheets("Sheet3").Rnage("A29"),True 10から100までの間で乱数を取得する Dim n As Integer ActiveCell.Value = Int(100-10+1)*Rand+10) イベントの発生を止める Application.EnableEvents = False ビープ音を鳴らす。 Beep |