简体   繁体   中英

VBA Select Case insert

I am trying to write a select case function that determines the text in column D and the value in cell A2 and dependant upon the text and value respectively, inserts an appropriate value in the column adjacent to the active cell.

Here is what I have so far (obviously this will grow to take into account all the variables in Column D and A2)

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "D:D" Then
       Select Case ActiveCell.TextPart = ""
            Case ActiveCell.TextPart = "Cat I(a)" 
                 And Range("A2").Value = 1: 
                 ActiveCell.Offset (0,1).Insert.Value "1"

Can anyone tell me what I am doing wrong? I'm a VBA Newbie writing code for my Uni Dissertation.

Select Case ActiveCell.TextPart = ""

This selects on the boolean result of that comparison. As it will be true if TextPart is empty the subsequent Case ActiveCell.TextPart = "Cat I(a)" can never in fact be the "case".

If you want to select on TextPart :

Select Case ActiveCell.TextPart
    Case "Cat I(a)"
        If Range("A2").Value = 1 then ActiveCell.Offset (0,1).Insert.Value "1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM