+ Reply to Thread
Results 1 to 8 of 8

Looping Sum, If cell begins With

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-17-2006
    Posts
    152

    Looping Sum, If cell begins With

    Hi,

    my loop is not working and i can't figure out why?

    Also how do i get the macro to find cells that begin with that number and do not equal it?

    Thanks,

    Sub NomCalc()
    Dim i As Integer
    Dim ws1 As Worksheet
    Dim c As Integer
    Dim GVal As Long
    Set ws1 = Sheets("Sheet1")
    
    GVal = Sheets("P & L Major Headings").Range("B8").Value
    
    c = 23 ' Period 1
    
    If Sheets("P & L Major Headings").Range("B4") > 1.01 Then
        c = c + Sheets("P & L Major Headings").Range("B4") - 1
    Else
    End If
    
    For i = 3 To 5000
    
    If ws1.Cells(i, A) = "0124" Then 'can this search for cells that begin with this number?
        GVal = Sheets("P & L Major Headings").Range("B8").Value
        GVal = GVal + Cells(c, i).Value
    Else
    Exit Sub
    End If
    
    Next i
    
    Sheets("P & L Major Headings").Range("B8").Value = GVal
    
    End Sub
    Last edited by jonn; 06-06-2007 at 11:52 AM.

  2. #2
    mpeplow
    Guest
    I don't know about your loop, how is it not working? What is it doing?

    About this
    If ws1.Cells(i, A) = "0124" Then 'can this search for cells that begin with this number?
    try

    If Mid(ws1.Cells(i, A),1,4) = "0124" Then
    Last edited by mpeplow; 06-06-2007 at 03:27 PM.

  3. #3
    Forum Contributor
    Join Date
    11-17-2006
    Posts
    152
    hi what does that do?

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    If InStr(CStr(ws1.Cells(i, A)), "0124") = 1 Then

  5. #5
    Forum Contributor
    Join Date
    11-17-2006
    Posts
    152
    Quote Originally Posted by mikerickson
    If InStr(CStr(ws1.Cells(i, A)), "0124") = 1 Then
    hi,

    can you explain that formula to me

    Thanks,

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    InStr is VB's version of search.

    If InStr(CStr(ws1.Cells(i, A)), "0124") = 1 then 
    ' The first occurance of "0124" in CStr(ws1.Cells(i, A)) starts at character 1.
    
    ' i.e. ws1.cells(i,A) begins with "0124"

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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