+ Reply to Thread
Results 1 to 20 of 20

Excel VBA not recognizing cell value

Hybrid View

  1. #1
    Forum Contributor her.rockstar's Avatar
    Join Date
    01-11-2013
    Location
    Saint John, NB
    MS-Off Ver
    Office 365
    Posts
    194

    Excel VBA not recognizing cell value

    Hello, in VBA, I am trying to find a value in a cell using Range("A1").Value

    The problem I'm running into is "A1" contains a formula displaying the value. If I copy and paste A1 as just text the code works, but that's not a solution. The cell needs to remain variable. Any way to get VBA to recognize the text written in the cell?

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Excel VBA not recognizing cell value

    I don't follow what you are asking. Can you elaborate?
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,082

    Re: Excel VBA not recognizing cell value

    I think the OP is saying

    A1 contains a formula.
    Using Range("A1").value results in...presumably the formula not the value ? (Insert your answer here OP)
    However if the OP overwrites the value in A1 using copy and paste values the same Range("A1").value codes results in the expected value.

    Am not sure why Range("A1").value would return anything other than the value that A1 contains.

    Prob best to show your code or attach a spreadsheet.
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  4. #4
    Forum Contributor her.rockstar's Avatar
    Join Date
    01-11-2013
    Location
    Saint John, NB
    MS-Off Ver
    Office 365
    Posts
    194

    Re: Excel VBA not recognizing cell value

    Special-K, that's exactly what I'm saying, yes. The VBA codes doesn't recognize the result of the formula in A1, it only sees that there is a formula.

    If I replace A1 with the text value of said content, the VBA code works properly.

    Maybe something like Range("A1").Text .. not sure if that's a thing or not.

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Excel VBA not recognizing cell value

    Range("A1").Value should return the result of the formula and not the formula. If it doesn't, there is something else going on in your code that's not apparent from your description. Can you post your code?

  6. #6
    Forum Contributor her.rockstar's Avatar
    Join Date
    01-11-2013
    Location
    Saint John, NB
    MS-Off Ver
    Office 365
    Posts
    194

    Re: Excel VBA not recognizing cell value

    Hey, sorry about that, here's my code.

    Private Sub RecordHours_Click()
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    Dim rowCell As Range
    Dim colCell As Range
    Dim wsSource As Worksheet
    Dim wsDest As Worksheet
    Dim valChange As Double
    Dim strCol As Date
    Dim strRow As String
    Dim dosubtract As Boolean
    
    Set wsSource = Worksheets("Main")
    Set wsDest = Worksheets("Hours")
    
    'Gather data
    With wsSource
        valChange = .Range("C16").Value
        strCol = .Range("C14").Value
        strRow = .Range("C18").Value
    End With
    
    With wsDest
        Set colCell = .Range("1:1").Find(strCol)
        Set rowCell = .Range("A:A").Find(strRow)
        
        'Error check
        If colCell Is Nothing Then
            MsgBox "Could not find Date"
            Exit Sub
        ElseIf rowCell Is Nothing Then
            MsgBox "Could not find Company"
            Exit Sub
        End If
    
    End With
    
    With wsDest
        Set colCell = .Range("1:1").Find(strCol)
        Set rowCell = .Range("A:A").Find(strRow)
        
        'Error check
        If colCell Is Nothing Then
            MsgBox "Could not find Group name"
            Exit Sub
        ElseIf rowCell Is Nothing Then
            MsgBox "Could not find Date value"
            Exit Sub
        End If
    
        With .Cells(rowCell.Row, colCell.Column)
            If dosubtract Then
                .Value = .Value - valChange
            Else
                .Value = .Value + valChange
            End If
        End With
    End With
    
    
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End Sub
    Basically on the worksheet Hours!, the dates are the columns across the top and the companies are the rows in column A. The amount of hours go in the table.

    If I change the dates from formulas to actual text, the code works. Otherwise, it returns the error "Could not find date"

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Excel VBA not recognizing cell value

    Dates are funny things in Excel. They are numeric values that are formated to display as dates.

    The quick answer is this...

    strCol = Format(.Range("C14").Value, "MM-DD-YYYY")
    Change the date format to the same date format as in row 1

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Excel VBA not recognizing cell value

    Can you post an example of the formulas that are returning dates?
    If posting code please use code tags, see here.

  9. #9
    Forum Contributor her.rockstar's Avatar
    Join Date
    01-11-2013
    Location
    Saint John, NB
    MS-Off Ver
    Office 365
    Posts
    194

    Re: Excel VBA not recognizing cell value

    AlphaFrog I'm afraid that didn't work.

    For the code were you suggesting this?:

    With wsSource
        valChange = .Range("C16").Value
        strCol = Format(.Range("C14").Value, "MM-DD-YYYY")
        strRow = .Range("C18").Value
    End With
    Also, Norie, here's my formula: =Main!$D$42

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Excel VBA not recognizing cell value

    Quote Originally Posted by her.rockstar View Post
    AlphaFrog I'm afraid that didn't work.

    For the code were you suggesting this?:

    With wsSource
        valChange = .Range("C16").Value
        strCol = Format(.Range("C14").Value, "MM-DD-YYYY")
        strRow = .Range("C18").Value
    End With
    That's what I was suggesting. Is MM-DD-YYYY the correct date format? I don't know what date format you are actually using.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Excel VBA not recognizing cell value

    What happens if you try Application.Match instead of Find?

    For example instead of this,
    With wsDest
        Set colCell = .Range("1:1").Find(strCol)
        Set rowCell = .Range("A:A").Find(strRow)
        
        'Error check
        If colCell Is Nothing Then
            MsgBox "Could not find Group name"
            Exit Sub
        ElseIf rowCell Is Nothing Then
            MsgBox "Could not find Date value"
            Exit Sub
        End If
    
        With .Cells(rowCell.Row, colCell.Column)
            If dosubtract Then
                .Value = .Value - valChange
            Else
                .Value = .Value + valChange
            End If
        End With
    End With
    something like this.
    With wsDest
        Res = Application.Match(strCol, .Range("1:1"),0)
        Set rowCell = .Range("A:A").Find(strRow)
        
        'Error check
        If IsError(Res) Then
            MsgBox "Could not find Group name"
            Exit Sub
        ElseIf rowCell Is Nothing Then
            MsgBox "Could not find Date value"
            Exit Sub
        End If
    
        With .Cells(rowCell.Row, Res)
            If dosubtract Then
                .Value = .Value - valChange
            Else
                .Value = .Value + valChange
            End If
        End With
    End With

  12. #12
    Forum Contributor her.rockstar's Avatar
    Join Date
    01-11-2013
    Location
    Saint John, NB
    MS-Off Ver
    Office 365
    Posts
    194

    Re: Excel VBA not recognizing cell value

    Norie it's saying Res is not defined. I'd be willing to try anything though!

    Alpha, sorry about that. The result of the formula I had displaying as mm/dd/yyyy. I did try that with your suggestion and it made no difference. Sorry for the confusion.

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Excel VBA not recognizing cell value

    Try adding this.
    Dim Res As Variant

  14. #14
    Forum Contributor her.rockstar's Avatar
    Join Date
    01-11-2013
    Location
    Saint John, NB
    MS-Off Ver
    Office 365
    Posts
    194

    Re: Excel VBA not recognizing cell value

    Still could not find date

  15. #15
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Excel VBA not recognizing cell value

    Try this...

    Private Sub RecordHours_Click()
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    Dim rowCell As Range
    Dim colCell As Range
    Dim wsSource As Worksheet
    Dim wsDest As Worksheet
    Dim valChange As Double
    Dim strCol As Date
    Dim strRow As String
    Dim dosubtract As Boolean
    
    Set wsSource = Worksheets("Main")
    Set wsDest = Worksheets("Hours")
    
    'Gather data
    With wsSource
        valChange = .Range("C16").Value
        strCol = Format(.Range("C14").Value, "mm/dd/yyyy")
        strRow = .Range("C18").Value
    End With
    
    With wsDest
        Set colCell = .Range("1:1").Find(strCol, , xlValues, xlWhole, , xlNext, False)
        Set rowCell = .Range("A:A").Find(strRow, , xlValues, xlWhole, , xlNext, False)
        
        'Error check
        If colCell Is Nothing Then
            MsgBox "Could not find Date"
        ElseIf rowCell Is Nothing Then
            MsgBox "Could not find Company"
        Else
            With .Cells(rowCell.Row, colCell.Column)
                If dosubtract Then
                    .Value = .Value - valChange
                Else
                    .Value = .Value + valChange
                End If
            End With
        End If
    
    End With
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End Sub

  16. #16
    Forum Contributor her.rockstar's Avatar
    Join Date
    01-11-2013
    Location
    Saint John, NB
    MS-Off Ver
    Office 365
    Posts
    194

    Re: Excel VBA not recognizing cell value

    That worked! Like you widened the umbrella of what type of value it could search for right?

    Thank you so much for doing this. I kind of borrowed this code from a different workbook I had - do you know if there's a simple chance I can make for a subtraction button?

  17. #17
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Excel VBA not recognizing cell value

    Quote Originally Posted by her.rockstar View Post
    That worked! Like you widened the umbrella of what type of value it could search for right?

    Thank you so much for doing this. I kind of borrowed this code from a different workbook I had - do you know if there's a simple chance I can make for a subtraction button?
    You're welcome.

    I don't know what you mean by a subtraction button. I have little understanding what you are attempting to do.

    Also in the code, you don't set the variable dosubtract to anything so it is always False by default.

  18. #18
    Forum Contributor her.rockstar's Avatar
    Join Date
    01-11-2013
    Location
    Saint John, NB
    MS-Off Ver
    Office 365
    Posts
    194

    Re: Excel VBA not recognizing cell value

    Sorry for the fogginess. I was attempting to build a test workbook for you, but you solved the problem too quickly!

    Right now it's simply taking the number from C16 and adding it to the Hours worksheet in a specific cell. I'd like to have a button that would remove the amount in C16 from the specific cell, if possible. Not hugely necessary, though.
    Last edited by her.rockstar; 06-07-2017 at 10:27 AM.

  19. #19
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Excel VBA not recognizing cell value

    Quote Originally Posted by her.rockstar View Post
    Sorry for the fogginess. I was attempting to build a test workbook for you, but you solved the problem too quickly!

    Right now it's simply taking the number from C16 and adding it to the Hours worksheet in a specific cell. I'd like to have a button that would remove the amount in C16 from the specific cell, if possible. Not hugely necessary, though.
    If you set the variable dosubtract = True that's exactly what the code will do.

    Alternatively, make the value in C16 negative to subtract.

  20. #20
    Forum Contributor her.rockstar's Avatar
    Join Date
    01-11-2013
    Location
    Saint John, NB
    MS-Off Ver
    Office 365
    Posts
    194

    Re: Excel VBA not recognizing cell value

    That's great - thanks again for all your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Excel 2013 not recognizing cell as 'date' after a formula was entered
    By Marijke in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-23-2016, 11:51 AM
  2. Is there a formula for recognizing the address of the active cell?
    By DMumme in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-23-2016, 09:34 PM
  3. Excel not recognizing Cell in formula
    By FerociousSteve in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-17-2015, 03:14 PM
  4. Workaround for recognizing reference cell changes in Change_Event
    By goomblar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2014, 07:44 AM
  5. recognizing the first of the month in a cell containing the date
    By jsnclns in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-24-2013, 01:21 PM
  6. Recognizing Populated Cell, Using It To Populate Another
    By eyeball11 in forum Excel General
    Replies: 4
    Last Post: 04-13-2012, 07:59 AM
  7. Macro not recognizing blank cell
    By shak1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-02-2009, 11:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1