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.

【ExcelVBA基礎】3章:セルのデータ操作

Last updated at Posted at 2021-05-28

はじめに
セルの操作は書式の設定や、ソート、範囲データを取得等マクロでできることは多岐にわたりますが、ここでは単一のセルからデータを取得する方法と、単一のセルにデータを格納する方法についてのみ説明します。

Sub set_demon_slayer()
    
    Dim pillar() As String
    Dim table_string As String

    ReDim pillar(0 To 7, 0 To 1)
    
    Cells(1, 1).Value = "冨岡義勇"
    Cells(2, 1).Value = "煉獄杏寿郎"
    Cells(3, 1).Value = "胡蝶しのぶ"
    Cells(4, 1).Value = "宇随天元"
    Cells(5, 1).Value = "甘露寺蜜璃"
    Cells(6, 1).Value = "時透無一郎"
    Cells(7, 1).Value = "悲鳴嶼行冥"
    Cells(8, 1).Value = "不死川実弥"

    Range("B1").Value = "トミオカギユウ"
    Range("B2").Value = "レンゴクキョウジュウロウ"
    Range("B3").Value = "コチョシノブ"
    Range("B4").Value = "ウズイテンゲン"
    Range("B5").Value = "カンロジミツリ"
    Range("B6").Value = "トキトウムイチロウ"
    Range("B7").Value = "ヒメジマギョウメイ"
    Range("B8").Value = "シナズガワサネミ"
    
    pillar(0, 0) = Range("A1").Value
    pillar(1, 0) = Range("A2").Value
    pillar(2, 0) = Range("A3").Value
    pillar(3, 0) = Range("A4").Value
    pillar(4, 0) = Range("A5").Value
    pillar(5, 0) = Range("A6").Value
    pillar(6, 0) = Range("A7").Value
    pillar(7, 0) = Range("A8").Value
    
    pillar(0, 1) = Cells(1, 2).Value
    pillar(1, 1) = Cells(2, 2).Value
    pillar(2, 1) = Cells(3, 2).Value
    pillar(3, 1) = Cells(4, 2).Value
    pillar(4, 1) = Cells(5, 2).Value
    pillar(5, 1) = Cells(6, 2).Value
    pillar(6, 1) = Cells(7, 2).Value
    pillar(7, 1) = Cells(8, 2).Value
    
    table_string = table_string & pillar(0, 0) & "," & pillar(0, 1) & vbCrLf
    table_string = table_string & pillar(1, 0) & "," & pillar(1, 1) & vbCrLf
    table_string = table_string & pillar(2, 0) & "," & pillar(2, 1) & vbCrLf
    table_string = table_string & pillar(3, 0) & "," & pillar(3, 1) & vbCrLf
    table_string = table_string & pillar(4, 0) & "," & pillar(4, 1) & vbCrLf
    table_string = table_string & pillar(5, 0) & "," & pillar(5, 1) & vbCrLf
    table_string = table_string & pillar(6, 0) & "," & pillar(6, 1) & vbCrLf
    table_string = table_string & pillar(7, 0) & "," & pillar(7, 1) & vbCrLf
    
    MsgBox table_string
    
    '出力結果:冨岡義勇,トミオカギユウ
    '          煉獄杏寿郎,レンゴクキョウジュウロウ
    '          胡蝶しのぶ,コチョシノブ
    '          宇随天元,ウズイテンゲン
    '          甘露寺蜜璃,カンロジミツリ
    '          時透無一郎,トキトウムイチロウ
    '          悲鳴嶼行冥,ヒメジマギョウメイ
    '          不死川実弥,シナズガワサネミ
    
End Sub

ExcelVBAでセルを指定する方法は「Cells」か「Range」を使う方法の2つです。

i)Cells(行,列)
行・列それぞれ数値で指定します。また変数に格納された数値で指定することもできます。配列とは違い行・列の最初は1から始まります。

ii)Range("セルの名前")
セルに名前を付けていなければ、Excelで馴染みのある"A1"や"D5"、"AA12"のように指定します。Excelの "名前の定義" 機能を使って自身で設定した名前を指定することもできます。

iii)「.Value」
データの取得・格納を問わず、原則指定することを推奨します。Excelで言う値貼付けのようなものです。

4章:繰り返し処理(For~Next文)
https://qiita.com/daichi05w/items/3c5b0362798feb59aa50

序章:まえがきと目次
https://qiita.com/daichi05w/items/002f311490dabaaf14d0

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?