CodingHnnnnnnng Excel macro trouble

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  ALL
39930.1 
Can anyone help me with this problem? I have a whole load of cells for which I need to change a days to weeks conversion from "=days/7" to "=CEILING(days/7,1)".

I found this tutorial, with this code:
VBA code:
Sub SearchNReplace2()
    Dim sFindInitial As String
    Dim sReplaceInitial As String
    Dim iLenInitial As Integer
    Dim sFindFinal As String
    Dim sReplaceFinal As String
    Dim iLenFinal As Integer
    Dim sTemp As String
    Dim rCell As Range

    sFindInitial = "="
    sReplaceInitial = "=CEILING("
    sFindFinal = "7"
    sReplaceFinal = "7,1)"

    For Each rCell In Selection
        sTemp = rCell.Value
        iLenInitial = Len(sFindInitial)
        iLenFinal = Len(sFindFinal)
        If Left(sTemp, iLenInitial) = sFindInitial And Right(sTemp, iLenFinal) = sFindFinal Then
            sTemp = Mid(sTemp, iLenInitial + 1)
            sTemp = Left(sTemp, Len(sTemp) - iLenFinal)
            sTemp = sReplaceInitial & sTemp & sReplaceFinal
            rCell.Value = sTemp
        End If
    Next
    Set rCell = Nothing
End Sub

But, when I select a cell and run the macro, nothing happens (yes, this is a .xlsm file). When I select the cell and step into the macro, it highlights the subroutine alright, but nothing occurs after continuing. (I didn't originally have the backslash in the final find/replace, but its absence makes no difference, and I need it there really).

<penelope pitstop> HAYLP! HAYLP! </pp>

bastard by name
bastard by nature

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  99% of gargoyles look like (MR_BASTARD)     
39930.2 In reply to 39930.1 
It's probably failing because of the stupid hungarian notation. :C

I don't see anything obvious, but what happens if you simplify it - remove everything except For Each and Next parts, then put Cell.Value = "XXX" in the loop
0/0
 Reply   Quote More 

 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  Peter (BOUGHTONP)     
39930.3 In reply to 39930.2 
I'm not sure what you mean by "stupid hungarian notation", it just looks like crappy VBA to me. But then I haven't programmed VBA for over 10 years. So, I did this, and still no joy:
vba code:
Sub SearchNReplace1()
    Dim sFindInitial As String
    Dim sReplaceInitial As String
    Dim iLenInitial As Integer
    Dim sFindFinal As String
    Dim sReplaceFinal As String
    Dim iLenFinal As Integer
    Dim sTemp As String
    Dim rCell As Range

    For Each rCell In Selection
        sTemp = rCell.Value
        iLenInitial = Len("=")
        iLenFinal = Len("/7")
        If Left(sTemp, iLenInitial) = "=" And Right(sTemp, iLenFinal) = "/7" Then
            sTemp = Mid(sTemp, iLenInitial + 1)
            sTemp = Left(sTemp, Len(sTemp) - iLenFinal)
            sTemp = "=CEILING(" & sTemp & "/7,1)"
            rCell.Value = sTemp
        End If
    Next
    Set rCell = Nothing
End Sub

bastard by name
bastard by nature

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  99% of gargoyles look like (MR_BASTARD)     
39930.4 In reply to 39930.3 
The bit where there's a letter at the start of every variable. It's not necessary and is a fucking stupid way of writing code (in any language).

Anyway, what I was saying was try this:
code:
Sub SearchNReplace1()
    Dim CurrentCell As Range

    For Each CurrentCell In Selection
        CurrentCell.Value = "XXX"
    Next
End Sub

(making sure you have a backup first, just in case)

If that doesn't work then you've got something small to work with, if it does work then add back lines to reproduce original behaviour until it breaks again.
0/0
 Reply   Quote More 

 From:  Dan (HERMAND)  
 To:  99% of gargoyles look like (MR_BASTARD)     
39930.5 In reply to 39930.1 
Can you post a snippet of your spreadsheet with fake data?
0/0
 Reply   Quote More 

 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  Dan (HERMAND)     
39930.6 In reply to 39930.5 
Thanks Dan, and Peter, but I decided that since this is just a means to an end I'd just work around it. I replaced the "=" and "/7" with blanks (just leaving the days) and then carried out the calculations in new columns referencing the cells. I can hide the old columns to make t look less crap.

bastard by name
bastard by nature

0/0
 Reply   Quote More 

Reply to All    
 

1–6

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats