0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VBA お役立ち

Posted at

テキストファイル操作

検索したい文字列まで行を移動する

'テキストファイルを開く
Open "ファイルパスの入ったセル" For Input As #1

Do
  Line Input #1, "行を格納する変数"
            
  '見つかった場合はループを抜ける
  If InStr("行を格納する変数", "検索したい文字列") <> 0 Then
    Exit Do
  End If
  
  Loop

Excelファイル操作

連続する同一文字を一文字にまとめる

Function DeleteMultiSpace(str As String) As String
  DeleteMultiSpace = str
    
  While InStr(DeleteMultiSpace, "  ") > 0
    DeleteMultiSpace = Replace(DeleteMultiSpace, "  ", " ")
  Wend
    
End Function

最終行の行数を取得する

"行数を格納する変数" = "起点となるセル".End(xlDown).Row

指定の範囲をSUM

'計算範囲の指定
Set "範囲を格納する変数" = Range(開始セル, 終了セル)
        
'指定した範囲をSUM
"計算結果を格納する変数" = Application.WorksheetFunction.Sum("範囲を格納する変数")

アクティブシートを切り替える

Worksheets("シート番号").Activate

※シート番号は一番左から数えるため、シート順を変えるとバグる

まとまったセルを削除

  Set "起点となるセルを格納する変数" = Range("B10")
    
  'セル初期化(Ctrl+Shift+Endと同じ挙動)
  Range("起点となるセルを格納する変数", ActivateCell.SpecialCells(xlLastCell)).ClearContents

ファイルを開く

Sub FileOpen()

  Dim OpenFileName As String  'ファイル名
    
  'ダイアログボックスを開く
  OpenFileName = Application.GetOpenFilename(FileFilter:="Cファイル,*.c")

  'ファイル名の取得判定
  If OpenFileName <> "False" Then        
    '取得したファイル名をセルへ格納
    "ファイル名を格納する変数" = OpenFileName        
  Else
    MsgBox "キャンセルされました"
  End If

End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?