Consulting

Results 1 to 6 of 6

Thread: VBA formula help

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    VBA formula help

    I have the following code

    Dim Track As String
    
        For x = 2 To Lr
            
            Track = ws.Cells(x, "B")
            
            If ws.Cells(x, "A") <> "" Then
                ws.Cells(x, "D") = Application.WorksheetFunction.VLookup(ws.Cells(x, "A"), wsEmail.Range("A2:E" & LrEmail), 4, False)
                ws.Cells(x, "E") = Application.WorksheetFunction.VLookup(ws.Cells(x, "A"), wsEmail.Range("A2:E" & LrEmail), 5, False)
                ws.Cells(x, "F") = "=ShipTrack(" & Track & ",""UPS"")"
                
            shipD = ws.Cells(x, "C")
                Days = DateDiff("d", shipD, nowD)
                
                ws.Cells(x, "G") = Days
            End If
        Next x
    I keep getting a runtime error and I was able to figure out that the problem is ws.cells(x, "B") always starts with a "1", but if I enter a letter infront of it this code works fine.
    How can I have it get that cell value even when it starts with a number?

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Can you supply the entire macro? as it stands your code will bug out when it hits the first ws.Cells as you haven't declared what ws is, remove ws. and you'll find the code will progress further.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Sorry about that

    Sub UpdateSh()
        Dim ws As Worksheet
        Dim wsEmail As Worksheet
        Dim shipD As Date
        Dim nowD As Date
        Dim Days As Integer
        Dim Track As String
        
        Set ws = Sheets("MV")
        Set wsEmail = Sheets("Emails")
        nowD = Date
        Lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
        LrEmail = wsEmail.Cells(Rows.Count, "A").End(xlUp).Row
        For x = 2 To Lr
            
            Track = ws.Cells(x, "B")
            
            If ws.Cells(x, "A") <> "" Then
                ws.Cells(x, "D") = Application.WorksheetFunction.VLookup(ws.Cells(x, "A"), wsEmail.Range("A2:E" & LrEmail), 4, False)
                ws.Cells(x, "E") = Application.WorksheetFunction.VLookup(ws.Cells(x, "A"), wsEmail.Range("A2:E" & LrEmail), 5, False)
                ws.Cells(x, "F") = "=ShipTrack(" & Track & ",""UPS"")"
                
                shipD = ws.Cells(x, "C")
                Days = DateDiff("d", shipD, nowD)
                
                ws.Cells(x, "G") = Days
            End If
        Next x
    
    End Sub

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    but if I enter a letter infront of it this code works fine.
    How can I have it get that cell value even when it starts with a number?
    Have you tried
    Track = ws.Cells(x, "B").Text
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Yes same result

  6. #6
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    After testing different ways I was able to get it to work with

    ws.Cells(x, "F") = "=ShipTrack(B" & x & ",""UPS"")"
    For some reason it didn't like the other setup.

Posting Permissions

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