1

I have a Macro enabled Excel workbook, and when it launches, it edits some cells and I have a change event, now I want the change event to not run at start up but run when the user makes the changes because when it does, the target.value becomes a variant and hence crashes as am assigning the value to a string variable, here is the code:

If Not Intersect(Target, Range("H83:H102,K83:K102,N83:N102,Q83:Q102,T83:T102,W83:W102,Z83:Z102,AC83:AC102")) Is Nothing Then
    Dim rowNum As Integer
    rowNum = Target.row
    Dim family As String
    family = Target.Offset(0, -1).Value
    Dim selection As String
    selection = "G" & rowNum & ",J" & rowNum & ",M" & rowNum & ",P" & rowNum & ",S" & rowNum & _
        ",V" & rowNum & ",Y" & rowNum & ",AB" & rowNum
    Select Case family
        Case "FAMILY 1"
            Call validateFamMembers(Target, selection, family, "C64")
        Case "FAMILY 2"
            Call validateFamMembers(Target, selection, family, "I64")
        Case "FAMILY 3"
            Call validateFamMembers(Target, selection, family, "O64")
        Case "FAMILY 4"
            Call validateFamMembers(Target, selection, family, "U64")
        Case "FAMILY 5"
            Call validateFamMembers(Target, selection, family, "C76")
        Case "FAMILY 6"
            Call validateFamMembers(Target, selection, family, "I76")
        Case "FAMILY 7"
            Call validateFamMembers(Target, selection, family, "O76")
        Case "FAMILY 8"
            Call validateFamMembers(Target, selection, family, "U76")
    End Select
End If

The code crashes on this line on start up

family = Target.Offset(0, -1).Value

how do I handle that. Thanks in advance

4
  • 1
    Range.Value is supposed to be Variant...not String. If you need to force it to a string use CStr() Commented Dec 1, 2016 at 13:55
  • 1
    Please include all of your code Commented Dec 1, 2016 at 13:55
  • @CallumDA33 The code is very big but that is the chunk that executes on worksheet change, its that if among a bunch of other ifs but they are independent Commented Dec 1, 2016 at 14:16
  • @Rdster I tried using that but still type mismatch when i restart the workbook Commented Dec 1, 2016 at 14:17

1 Answer 1

2

I think your problem happens when you select the first column. It explodes, because of the offset, because it cannot reach -1 column after the first.

Edit:

Answer in the comments: You use Target. Thus, if you select more cells, more cells are in the target. And multiple cells cannot be converted to string, because they are a variant.

Sign up to request clarification or add additional context in comments.

12 Comments

Yep. Was just about to post that as a comment. The guard clause only makes sure the changed range contains cells the OP cares about.
Does that answer your question though?
@lulliezy, can you put the line debug.print Target.Offset(0, -1).Value before the line giving you the error and share the answer? There should be a value in the immediate window.
Type mismatch on Debug.Print Target.Offset(0, -1).Value @Vityata
@Vityata $E$83:$AB$102 this is the value that is printed
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.