Setting the string value of 'myTerm' based on various cell content combinations
up vote
0
down vote
favorite
Is it possible to set the value of myTerm using a series of If statements?
I have 3 columns in a worksheet which will have data added to them over the course of the year.
I have the following scenarios:
K has data and M & O = "Autumn"
K and M have data and O is empty = "Spring"
K, M and O all have data = Summer
I have written the following (a snippet of the full code):
Sub FilterProgressData()
    Dim myTerm As String
    If SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value = "" And _
        SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Autumn"
    ElseIf SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Spring"
    ElseIf SrcWs.Cells(i, "O").Value <> "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Summer"
but get a 'Run-time error '1004' error - Application-defined or object-defined error'
Any advice will be gratefully received!
Thanks.
excel vba if-statement
add a comment |
up vote
0
down vote
favorite
Is it possible to set the value of myTerm using a series of If statements?
I have 3 columns in a worksheet which will have data added to them over the course of the year.
I have the following scenarios:
K has data and M & O = "Autumn"
K and M have data and O is empty = "Spring"
K, M and O all have data = Summer
I have written the following (a snippet of the full code):
Sub FilterProgressData()
    Dim myTerm As String
    If SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value = "" And _
        SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Autumn"
    ElseIf SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Spring"
    ElseIf SrcWs.Cells(i, "O").Value <> "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Summer"
but get a 'Run-time error '1004' error - Application-defined or object-defined error'
Any advice will be gratefully received!
Thanks.
excel vba if-statement
Your error has little to nothing to do with the fact that you are usingIfstatements. Without a Minimal, Complete, and Verifiable example it is impossible to tell what is causing the error. The problem almost certainly lies in code that you have decided to not show.
– John Coleman
Nov 22 at 13:12
What is Variable SrcWs? It needs to be defined. If it is a worksheet then you need. Dim SrcWs As Worksheet.
– Anthony
Nov 22 at 13:15
Thank you for these comments - the other variables are defined and set appropriately, I was trying to give you the minimal code that i have added which caused the error when testing. I'll try the change as outlined below and, if I still have issues, will post a fuller version of the code.
– gbra
Nov 22 at 14:05
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Is it possible to set the value of myTerm using a series of If statements?
I have 3 columns in a worksheet which will have data added to them over the course of the year.
I have the following scenarios:
K has data and M & O = "Autumn"
K and M have data and O is empty = "Spring"
K, M and O all have data = Summer
I have written the following (a snippet of the full code):
Sub FilterProgressData()
    Dim myTerm As String
    If SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value = "" And _
        SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Autumn"
    ElseIf SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Spring"
    ElseIf SrcWs.Cells(i, "O").Value <> "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Summer"
but get a 'Run-time error '1004' error - Application-defined or object-defined error'
Any advice will be gratefully received!
Thanks.
excel vba if-statement
Is it possible to set the value of myTerm using a series of If statements?
I have 3 columns in a worksheet which will have data added to them over the course of the year.
I have the following scenarios:
K has data and M & O = "Autumn"
K and M have data and O is empty = "Spring"
K, M and O all have data = Summer
I have written the following (a snippet of the full code):
Sub FilterProgressData()
    Dim myTerm As String
    If SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value = "" And _
        SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Autumn"
    ElseIf SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Spring"
    ElseIf SrcWs.Cells(i, "O").Value <> "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Summer"
but get a 'Run-time error '1004' error - Application-defined or object-defined error'
Any advice will be gratefully received!
Thanks.
excel vba if-statement
excel vba if-statement
asked Nov 22 at 13:04
gbra
337
337
Your error has little to nothing to do with the fact that you are usingIfstatements. Without a Minimal, Complete, and Verifiable example it is impossible to tell what is causing the error. The problem almost certainly lies in code that you have decided to not show.
– John Coleman
Nov 22 at 13:12
What is Variable SrcWs? It needs to be defined. If it is a worksheet then you need. Dim SrcWs As Worksheet.
– Anthony
Nov 22 at 13:15
Thank you for these comments - the other variables are defined and set appropriately, I was trying to give you the minimal code that i have added which caused the error when testing. I'll try the change as outlined below and, if I still have issues, will post a fuller version of the code.
– gbra
Nov 22 at 14:05
add a comment |
Your error has little to nothing to do with the fact that you are usingIfstatements. Without a Minimal, Complete, and Verifiable example it is impossible to tell what is causing the error. The problem almost certainly lies in code that you have decided to not show.
– John Coleman
Nov 22 at 13:12
What is Variable SrcWs? It needs to be defined. If it is a worksheet then you need. Dim SrcWs As Worksheet.
– Anthony
Nov 22 at 13:15
Thank you for these comments - the other variables are defined and set appropriately, I was trying to give you the minimal code that i have added which caused the error when testing. I'll try the change as outlined below and, if I still have issues, will post a fuller version of the code.
– gbra
Nov 22 at 14:05
Your error has little to nothing to do with the fact that you are using
If statements. Without a Minimal, Complete, and Verifiable example it is impossible to tell what is causing the error. The problem almost certainly lies in code that you have decided to not show.– John Coleman
Nov 22 at 13:12
Your error has little to nothing to do with the fact that you are using
If statements. Without a Minimal, Complete, and Verifiable example it is impossible to tell what is causing the error. The problem almost certainly lies in code that you have decided to not show.– John Coleman
Nov 22 at 13:12
What is Variable SrcWs? It needs to be defined. If it is a worksheet then you need. Dim SrcWs As Worksheet.
– Anthony
Nov 22 at 13:15
What is Variable SrcWs? It needs to be defined. If it is a worksheet then you need. Dim SrcWs As Worksheet.
– Anthony
Nov 22 at 13:15
Thank you for these comments - the other variables are defined and set appropriately, I was trying to give you the minimal code that i have added which caused the error when testing. I'll try the change as outlined below and, if I still have issues, will post a fuller version of the code.
– gbra
Nov 22 at 14:05
Thank you for these comments - the other variables are defined and set appropriately, I was trying to give you the minimal code that i have added which caused the error when testing. I'll try the change as outlined below and, if I still have issues, will post a fuller version of the code.
– gbra
Nov 22 at 14:05
add a comment |
                                2 Answers
                                2
                        
active
oldest
votes
up vote
0
down vote
This works fine. I do not see a problem with your if statement.
Option Explicit
Sub FilterProgressData()
Dim myTerm As String
Dim i As Integer
Dim SrcWs As Worksheet
Set SrcWs = Worksheets("Sheet1")
i = 1
    If SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value = "" And _
        SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Autumn"
    ElseIf SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Spring"
    ElseIf SrcWs.Cells(i, "O").Value <> "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Summer"
    End If
    End Sub
add a comment |
up vote
0
down vote
Couple of issues that I could see in your snippet:
SrcWsis not declared/instantiated anywhere in your snippet.
iis not assigned. If it's being implicitly converted to0(when usingCells()), then you're trying to access the zeroth row of the worksheet (which doesn't exist and will give you an error).
Say you want to assign a value to myTerm based on the values in columns K, M, O and rows 1 to 100 (for example's sake). You could then try something like the below:
Option Explicit
Sub ConditionallyAssignMyTerm()
    Dim SrcWs As Worksheet
    Set SrcWs = ThisWorkbook.Worksheets("Sheet1")
    Dim i As Long ' Might be better to rename this to rowIndex or something more descriptive than just i
    With SrcWs
        Dim myTerm As String
        Dim columnKisBlank As Boolean
        Dim columnMisBlank As Boolean
        Dim columnOisBlank As Boolean
        For i = 1 To 100 ' Arbitrary loop values
            columnKisBlank = CellIsBlank(.Cells(i, "K"))
            columnMisBlank = CellIsBlank(.Cells(i, "M"))
            columnOisBlank = CellIsBlank(.Cells(i, "O"))
            If (Not columnKisBlank) And columnMisBlank And columnOisBlank Then
                myTerm = "Autumn"
            ElseIf (Not columnKisBlank) And (Not columnMisBlank) And columnOisBlank Then
                myTerm = "Spring"
            ElseIf (Not columnKisBlank) And (Not columnMisBlank) And (Not columnOisBlank) Then
                myTerm = "Summer"
            End If
        Next i
    End With
End Sub
Function CellIsBlank(ByRef cellToCheck As Range) As Boolean
    CellIsBlank = VBA.IsEmpty(cellToCheck.Value2) Or (Len(cellToCheck.Value2) = 0)
End Function
add a comment |
                                2 Answers
                                2
                        
active
oldest
votes
                                2 Answers
                                2
                        
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
This works fine. I do not see a problem with your if statement.
Option Explicit
Sub FilterProgressData()
Dim myTerm As String
Dim i As Integer
Dim SrcWs As Worksheet
Set SrcWs = Worksheets("Sheet1")
i = 1
    If SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value = "" And _
        SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Autumn"
    ElseIf SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Spring"
    ElseIf SrcWs.Cells(i, "O").Value <> "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Summer"
    End If
    End Sub
add a comment |
up vote
0
down vote
This works fine. I do not see a problem with your if statement.
Option Explicit
Sub FilterProgressData()
Dim myTerm As String
Dim i As Integer
Dim SrcWs As Worksheet
Set SrcWs = Worksheets("Sheet1")
i = 1
    If SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value = "" And _
        SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Autumn"
    ElseIf SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Spring"
    ElseIf SrcWs.Cells(i, "O").Value <> "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Summer"
    End If
    End Sub
add a comment |
up vote
0
down vote
up vote
0
down vote
This works fine. I do not see a problem with your if statement.
Option Explicit
Sub FilterProgressData()
Dim myTerm As String
Dim i As Integer
Dim SrcWs As Worksheet
Set SrcWs = Worksheets("Sheet1")
i = 1
    If SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value = "" And _
        SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Autumn"
    ElseIf SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Spring"
    ElseIf SrcWs.Cells(i, "O").Value <> "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Summer"
    End If
    End Sub
This works fine. I do not see a problem with your if statement.
Option Explicit
Sub FilterProgressData()
Dim myTerm As String
Dim i As Integer
Dim SrcWs As Worksheet
Set SrcWs = Worksheets("Sheet1")
i = 1
    If SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value = "" And _
        SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Autumn"
    ElseIf SrcWs.Cells(i, "O").Value = "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Spring"
    ElseIf SrcWs.Cells(i, "O").Value <> "" And SrcWs.Cells(i, "M").Value <> "" _
        And SrcWs.Cells(i, "K").Value <> "" Then
            myTerm = "Summer"
    End If
    End Sub
answered Nov 22 at 13:31
Anthony
177110
177110
add a comment |
add a comment |
up vote
0
down vote
Couple of issues that I could see in your snippet:
SrcWsis not declared/instantiated anywhere in your snippet.
iis not assigned. If it's being implicitly converted to0(when usingCells()), then you're trying to access the zeroth row of the worksheet (which doesn't exist and will give you an error).
Say you want to assign a value to myTerm based on the values in columns K, M, O and rows 1 to 100 (for example's sake). You could then try something like the below:
Option Explicit
Sub ConditionallyAssignMyTerm()
    Dim SrcWs As Worksheet
    Set SrcWs = ThisWorkbook.Worksheets("Sheet1")
    Dim i As Long ' Might be better to rename this to rowIndex or something more descriptive than just i
    With SrcWs
        Dim myTerm As String
        Dim columnKisBlank As Boolean
        Dim columnMisBlank As Boolean
        Dim columnOisBlank As Boolean
        For i = 1 To 100 ' Arbitrary loop values
            columnKisBlank = CellIsBlank(.Cells(i, "K"))
            columnMisBlank = CellIsBlank(.Cells(i, "M"))
            columnOisBlank = CellIsBlank(.Cells(i, "O"))
            If (Not columnKisBlank) And columnMisBlank And columnOisBlank Then
                myTerm = "Autumn"
            ElseIf (Not columnKisBlank) And (Not columnMisBlank) And columnOisBlank Then
                myTerm = "Spring"
            ElseIf (Not columnKisBlank) And (Not columnMisBlank) And (Not columnOisBlank) Then
                myTerm = "Summer"
            End If
        Next i
    End With
End Sub
Function CellIsBlank(ByRef cellToCheck As Range) As Boolean
    CellIsBlank = VBA.IsEmpty(cellToCheck.Value2) Or (Len(cellToCheck.Value2) = 0)
End Function
add a comment |
up vote
0
down vote
Couple of issues that I could see in your snippet:
SrcWsis not declared/instantiated anywhere in your snippet.
iis not assigned. If it's being implicitly converted to0(when usingCells()), then you're trying to access the zeroth row of the worksheet (which doesn't exist and will give you an error).
Say you want to assign a value to myTerm based on the values in columns K, M, O and rows 1 to 100 (for example's sake). You could then try something like the below:
Option Explicit
Sub ConditionallyAssignMyTerm()
    Dim SrcWs As Worksheet
    Set SrcWs = ThisWorkbook.Worksheets("Sheet1")
    Dim i As Long ' Might be better to rename this to rowIndex or something more descriptive than just i
    With SrcWs
        Dim myTerm As String
        Dim columnKisBlank As Boolean
        Dim columnMisBlank As Boolean
        Dim columnOisBlank As Boolean
        For i = 1 To 100 ' Arbitrary loop values
            columnKisBlank = CellIsBlank(.Cells(i, "K"))
            columnMisBlank = CellIsBlank(.Cells(i, "M"))
            columnOisBlank = CellIsBlank(.Cells(i, "O"))
            If (Not columnKisBlank) And columnMisBlank And columnOisBlank Then
                myTerm = "Autumn"
            ElseIf (Not columnKisBlank) And (Not columnMisBlank) And columnOisBlank Then
                myTerm = "Spring"
            ElseIf (Not columnKisBlank) And (Not columnMisBlank) And (Not columnOisBlank) Then
                myTerm = "Summer"
            End If
        Next i
    End With
End Sub
Function CellIsBlank(ByRef cellToCheck As Range) As Boolean
    CellIsBlank = VBA.IsEmpty(cellToCheck.Value2) Or (Len(cellToCheck.Value2) = 0)
End Function
add a comment |
up vote
0
down vote
up vote
0
down vote
Couple of issues that I could see in your snippet:
SrcWsis not declared/instantiated anywhere in your snippet.
iis not assigned. If it's being implicitly converted to0(when usingCells()), then you're trying to access the zeroth row of the worksheet (which doesn't exist and will give you an error).
Say you want to assign a value to myTerm based on the values in columns K, M, O and rows 1 to 100 (for example's sake). You could then try something like the below:
Option Explicit
Sub ConditionallyAssignMyTerm()
    Dim SrcWs As Worksheet
    Set SrcWs = ThisWorkbook.Worksheets("Sheet1")
    Dim i As Long ' Might be better to rename this to rowIndex or something more descriptive than just i
    With SrcWs
        Dim myTerm As String
        Dim columnKisBlank As Boolean
        Dim columnMisBlank As Boolean
        Dim columnOisBlank As Boolean
        For i = 1 To 100 ' Arbitrary loop values
            columnKisBlank = CellIsBlank(.Cells(i, "K"))
            columnMisBlank = CellIsBlank(.Cells(i, "M"))
            columnOisBlank = CellIsBlank(.Cells(i, "O"))
            If (Not columnKisBlank) And columnMisBlank And columnOisBlank Then
                myTerm = "Autumn"
            ElseIf (Not columnKisBlank) And (Not columnMisBlank) And columnOisBlank Then
                myTerm = "Spring"
            ElseIf (Not columnKisBlank) And (Not columnMisBlank) And (Not columnOisBlank) Then
                myTerm = "Summer"
            End If
        Next i
    End With
End Sub
Function CellIsBlank(ByRef cellToCheck As Range) As Boolean
    CellIsBlank = VBA.IsEmpty(cellToCheck.Value2) Or (Len(cellToCheck.Value2) = 0)
End Function
Couple of issues that I could see in your snippet:
SrcWsis not declared/instantiated anywhere in your snippet.
iis not assigned. If it's being implicitly converted to0(when usingCells()), then you're trying to access the zeroth row of the worksheet (which doesn't exist and will give you an error).
Say you want to assign a value to myTerm based on the values in columns K, M, O and rows 1 to 100 (for example's sake). You could then try something like the below:
Option Explicit
Sub ConditionallyAssignMyTerm()
    Dim SrcWs As Worksheet
    Set SrcWs = ThisWorkbook.Worksheets("Sheet1")
    Dim i As Long ' Might be better to rename this to rowIndex or something more descriptive than just i
    With SrcWs
        Dim myTerm As String
        Dim columnKisBlank As Boolean
        Dim columnMisBlank As Boolean
        Dim columnOisBlank As Boolean
        For i = 1 To 100 ' Arbitrary loop values
            columnKisBlank = CellIsBlank(.Cells(i, "K"))
            columnMisBlank = CellIsBlank(.Cells(i, "M"))
            columnOisBlank = CellIsBlank(.Cells(i, "O"))
            If (Not columnKisBlank) And columnMisBlank And columnOisBlank Then
                myTerm = "Autumn"
            ElseIf (Not columnKisBlank) And (Not columnMisBlank) And columnOisBlank Then
                myTerm = "Spring"
            ElseIf (Not columnKisBlank) And (Not columnMisBlank) And (Not columnOisBlank) Then
                myTerm = "Summer"
            End If
        Next i
    End With
End Sub
Function CellIsBlank(ByRef cellToCheck As Range) As Boolean
    CellIsBlank = VBA.IsEmpty(cellToCheck.Value2) Or (Len(cellToCheck.Value2) = 0)
End Function
answered Nov 22 at 13:36
chillin
874134
874134
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
 
But avoid …
- Asking for help, clarification, or responding to other answers.
 - Making statements based on opinion; back them up with references or personal experience.
 
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
 
But avoid …
- Asking for help, clarification, or responding to other answers.
 - Making statements based on opinion; back them up with references or personal experience.
 
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431672%2fsetting-the-string-value-of-myterm-based-on-various-cell-content-combinations%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your error has little to nothing to do with the fact that you are using
Ifstatements. Without a Minimal, Complete, and Verifiable example it is impossible to tell what is causing the error. The problem almost certainly lies in code that you have decided to not show.– John Coleman
Nov 22 at 13:12
What is Variable SrcWs? It needs to be defined. If it is a worksheet then you need. Dim SrcWs As Worksheet.
– Anthony
Nov 22 at 13:15
Thank you for these comments - the other variables are defined and set appropriately, I was trying to give you the minimal code that i have added which caused the error when testing. I'll try the change as outlined below and, if I still have issues, will post a fuller version of the code.
– gbra
Nov 22 at 14:05