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 1 year has passed since last update.

[VBA] 選択したセルに反応して値を得る方法

Posted at

はじめに

仕事でVBAを勉強する必要があったので、勉強のメモ

目的

選択したセルの値によって、特定のセルに条件を満たすような値を表すようにしたい。

Code

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
    ' 選択セルが 2個以上、空白、数字ではないなら終わらせる
    If Target.Cells.Count > 1 Or IsEmpty(Target) Or Not IsNumeric(Target.Value) Then
        ActiveSheet.Cells(8, 6).Value = ""
        Exit Sub
    End If
    
    If Target.Value <> 0 Then '0で割らないようにする
    
        ActiveSheet.Cells(8, 6).Value = ActiveSheet.Cells(8, 2).Value * Target.Value
    
    End If
    
End Sub

結果

image.png

image.png

image.png

終わりに

選択したセルの値に反応し、上の式のような結果を表示するようにできた。
数字でないセル、空白のセルを選択した場合、空白の結果を返すようにした。

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?