+ Reply to Thread
Results 1 to 24 of 24

Expected: end of statement in Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Expected: end of statement in Macro

    Hi all -

    I have a macro that is supposed to communicate with Bloomberg to retrieve some static info from certain fields. The macro looks through my excel sheet that has columns A & B (Cusip and AbsRefID, respectively), and basically concatenates it into cell C so that Bloomberg can read it. Then I retrieve data points using Bloomberg's BDP function for each column. I'm getting an expected end of statement error in the highlighted portion of the code below. Any help is greatly appreciated.

    Sub Format_1st_Run()
     
        For Each ws In Sheets
        ws.Activate
        Dim A As Long, B As Long
        A = Range("B" & Rows.Count).End(xlUp).Row
        For B = A To 1 Step -1
        
        'Define Security Type so BB can read
        If Range("B" & B).Value Like "AGY*" Then Range("C" & B).Value = Range("A" & B).Value & " Corp"
        If Range("B" & B).Value Like "CD*" Then Range("C" & B).Value = Range("A" & B).Value & " Corp"
        If Range("B" & B).Value Like "CORP*" Then Range("C" & B).Value = Range("A" & B).Value & " Corp"
        If Range("B" & B).Value Like "MUNI*" Then Range("C" & B).Value = Range("A" & B).Value & " Muni"
        If Range("B" & B).Value Like "PREF*" Then Range("C" & B).Value = Range("A" & B).Value & " Pfd"
        If Range("B" & B).Value Like "PRST*" Then Range("C" & B).Value = Range("A" & B).Value & " Pfd"
        If Range("B" & B).Value Like "FOREIGN*" Then Range("C" & B).Value = Range("A" & B).Value & " Corp"
        
        'Look up BB Data Points
        If Range("C" & B).Value Like "*Corp" Then Range("D" & B).Formula = "=BDP(Range("C" & B).Value, ""CALLED_DT"")"
            If Range("C" & B).Value Like "*Corp" Then Range("E" & B).Formula = "=BDP(Range("C" & B).Value, ""CALLED_PX"")"
                If Range("C" & B).Value Like "*Corp" Then Range("F" & B).Formula = "=BDP(Range("C" & B).Value, ""MOST_RECENT_REPORTED_FACTOR"")"
        If Range("C" & B).Value Like "*Muni" Then Range("D" & B).Formula = "=BDP(Range("C" & B).Value, ""MUNI_RECENT_REDEMP_DT"")"
            If Range("C" & B).Value Like "*Muni" Then Range("E" & B).Formula = "=BDP(Range("C" & B).Value, ""MUNI_RECENT_REDEMP_PX"")"
                If Range("C" & B).Value Like "*Muni" Then Range("F" & B).Formula = "=BDP(Range("C" & B).Value, ""MUNI_RECENT_REDEMP_TYP"")"
        If Range("C" & B).Value Like "*Pfd" Then Range("D" & B).Formula = "=BDP(Range("C" & B).Value, ""CALLED_DT"")"
            If Range("C" & B).Value Like "*Pfd" Then Range("E" & B).Formula = "=BDP(Range("C" & B).Value, ""CALLED_PX"")"    
        'Check to make sure that the cells are NOT requesting data
        
        'Delete bad data from Corp
        If Range("C" & B).Value Like "*Corp" And Range("F" & B).Value Like "1" Then Rows(B).Delete
        If Range("C" & B).Value Like "*Corp" And Range("F" & B).Value Like "0" Then Rows(B).Delete
        If Range("C" & B).Value Like "*Corp" And Range("D" & B).Value Like "N/A*" Then Rows(B).Delete
        If Range("C" & B).Value Like "*Corp" And Range("E" & B).Value Like "N/A*" Then Rows(B).Delete
        If Range("C" & B).Value Like "*Corp" And Range("F" & B).Value Like "N/A*" Then Rows(B).Delete
        If Range("C" & B).Value Like "*Corp" And Month(Range("D" & B)) <> Month(Date) Then Rows(B).Delete
         
        'Delete bad data from Muni
        If Range("C" & B).Value Like "*Muni" And Range("F" & B).Value <> "Called in Full" Then Rows(B).Delete
        If Range("C" & B).Value Like "*Muni" And Month(Range("D" & B)) <> Month(Date) Then Rows(B).Delete
        
        'Delete bad data from Pfd
        If Range("C" & B).Value Like "*Pfd" And Range("D" & B).Value Like "N/A*" Then Rows(B).Delete
        If Range("C" & B).Value Like "*Pfd" And Range("E" & B).Value Like "N/A*" Then Rows(B).Delete
        If Range("C" & B).Value Like "*Pfd" And Month(Range("D" & B)) <> Month(Date) Then Rows(B).Delete
        
        'For Corp, put any security with a factor other than 0 or 1 into a new worksheet
        'Verify that factor is in current month
        
        Next B
        Next ws
        Application.ScreenUpdating = True
        Application.DisplayStatusBar = True
    End Sub
    Last edited by jeffreybrown; 10-16-2012 at 06:21 PM. Reason: Please use code tags...Thanks.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected: end of statement in Macro

    Hi, reltub,

    when I paste your code into a module the lines with the formulas to be inserted are highlighted.

    You would need to double up the quotes there from (just the first line here):
    If Range("C" & B).Value Like "*Corp" Then Range("D" & B).Formula = "=BDP(Range("C" & B).Value, ""CALLED_DT"")"
    to
    If Range("C" & B).Value Like "*Corp" Then Range("D" & B).Formula = "=BDP(Range(""C"" & B).Value, ""CALLED_DT"")"
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    Thanks for the quick reply Holger. I tried your suggestion, but it still didn't work. The formula for cells in column D would just display =BDP("C","CALLED_DT"). I need it to actually reference what is in column C and use that in the formula. I revised the code a little and here's what I have now. I'm having the same problem though. Formula in column D is displaying as =BDP("rng3.Value","CALLED_DT").

    Sub Backup()
    '
    ' Backup Macro
    ' backup of call cusips formatting
    
     For Each ws In Sheets
        ws.Activate
        Dim A As Long, B As Long
        A = Range("B" & Rows.Count).End(xlUp).Row
        For B = A To 1 Step -1
        
        Set rng1 = Range("A" & B)
        Set rng2 = Range("B" & B)
        Set rng3 = Range("C" & B)
        Set rng4 = Range("D" & B)
        Set rng5 = Range("E" & B)
        Set rng6 = Range("F" & B)
        
        'Define Security Type so BB can read
        If rng2.Value Like "AGY*" Then rng3.Value = rng1.Value & " Corp"
        If rng2.Value Like "CD*" Then rng3.Value = rng1.Value & " Corp"
        If rng2.Value Like "CORP*" Then rng3.Value = rng1.Value & " Corp"
        If rng2.Value Like "MUNI*" Then rng3.Value = rng1.Value & " Muni"
        If rng2.Value Like "PREF*" Then rng3.Value = rng1.Value & " Pfd"
        If rng2.Value Like "PRST*" Then rng3.Value = rng1.Value & " Pfd"
        If rng2.Value Like "FOREIGN*" Then rng3.Value = rng1.Value & " Corp"
        
        'Look up BB Data Points
        If rng3.Value Like "*Corp" Then rng4.Formula = "=BDP(""rng3.Value"", ""CALLED_DT"")"
            If rng3.Value Like "*Corp" Then rng5.Formula = "=BDP(""rng3.Value"", ""CALLED_PX"")"
                If rng3.Value Like "*Corp" Then rng6.Formula = "=BDP(""rng3.Value"", ""MOST_RECENT_REPORTED_FACTOR"")"
        If rng3.Value Like "*Muni" Then rng4.Formula = "=BDP(""rng3.Value"", ""MUNI_RECENT_REDEMP_DT"")"
            If rng3.Value Like "*Muni" Then rng5.Formula = "=BDP(""rng3.Value"", ""MUNI_RECENT_REDEMP_PX"")"
                If rng3.Value Like "*Muni" Then rng6.Formula = "=BDP(""rng3.Value"", ""MUNI_RECENT_REDEMP_TYP"")"
        If rng3.Value Like "*Pfd" Then rng4.Formula = "=BDP(""rng3.Value"", ""CALLED_DT"")"
            If rng3.Value Like "*Pfd" Then rng5.Formula = "=BDP(""rng3.Value"", ""CALLED_PX"")"
           
        Next B
        Next ws
        Application.ScreenUpdating = True
        Application.DisplayStatusBar = True
    
    End Sub

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected: end of statement in Macro

    Hi, reltub,

    I must excuse - it was way to early this morning to answer.

    "=BDP(" & Range("C" & B).Value & ", ""CALLED_DT"")"
    for the Value of the cell and
    "=BDP(" & Range("C" & B).Address & ", ""CALLED_DT"")"
    for the address with absolute reference.

    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    Works like a charm with .address! Thanks!
    I'm new to working with BB and with ranges like this, so please excuse any ignorance. In step 2 of my macro, I look at data that was pulled in from BB and delete certain rows based on criteria. The errors I get in this step are included in the comments in the code. I don't understand why I am getting a data type mismatch error for the Month(Date) criteria, which deletes rows where column D is not in the current month. I also don't understand why I'm getting an object required error for the <> "CALLED IN FULL" part of the code. This deletes rows where column F is not equal to CALLED IN FULL. Here is the code:

    Sub Step_2()
    
        'This step of the Callable Bonds Download macro deletes the bad data from each Security Type
        'Cuts and pastes and security where Security Type is Corp and 0<Factor<1 into a new spreadsheet
        'Verify that this factor is for the current month; if not, delete entire row
    
        For Each ws In Sheets
        ws.Activate
        Dim A As Long, B As Long
        A = Range("B" & Rows.Count).End(xlUp).Row
        For B = A To 1 Step -1
        
        Set rng1 = Range("A" & B)
        Set rng2 = Range("B" & B)
        Set rng3 = Range("C" & B)
        Set rng4 = Range("D" & B)
        Set rng5 = Range("E" & B)
        Set rng6 = Range("F" & B)
    
        'Check to make sure that the cells are NOT requesting data
        
           
        'Delete bad data from Corp
        If rng3.Value Like "*Corp" And rng6.Value Like "1" Then Rows(B).Delete
        If rng3.Value Like "*Corp" And rng6.Value Like "0" Then Rows(B).Delete
        If rng3.Value Like "*Corp" And rng4.Value Like "#N/A*" Then Rows(B).Delete
        If rng3.Value Like "*Corp" And rng5.Value Like "#N/A*" Then Rows(B).Delete
        If rng3.Value Like "*Corp" And rng6.Value Like "#N/A*" Then Rows(B).Delete
        If rng3.Value Like "*Corp" And rng4.Value <> Month(Date) Then Rows(B).Delete
        'Getting error:Data type mismatch above
         
        'Delete bad data from Muni
        If rng3.Value Like "*Muni" And rng6.Value <> "CALLED IN FULL" Then Rows(B).Delete
        'Getting error:Oject Required above
        If rng3.Value Like "*Muni" And rng4.Value <> Month(Date) Then Rows(B).Delete
        'Getting error:Data type mismatch above
        
        'Delete bad data from Pfd
        If rng3.Value Like "*Pfd" And rng4.Value Like "#N/A*" Then Rows(B).Delete
        If rng3.Value Like "*Pfd" And rng5.Value Like "#N/A*" Then Rows(B).Delete
        If rng3.Value Like "*Pfd" And rng4.Value <> Month(Date) Then Rows(B).Delete
        'Getting error:Data type mismatch above
        
        'For Corp, put any security with a factor other than 0 or 1 into a new spreadsheet
        'Verify that factor is in current month
        
        Next B
        Next ws
        Application.ScreenUpdating = True
        Application.DisplayStatusBar = True
        
    End Sub

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected: end of statement in Macro

    Hi, reltub,

    could you please attach a workbook with some sample data for a check? That would life a lot easier at least for me and especially at this time of the day.

    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    Sure thing. I've attached a sample worksheet. The formulas won't work because you have to be connected to a Bloomberg terminal, but you can see what I'm trying to do. Thanks for your help!
    Attached Files Attached Files

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected: end of statement in Macro

    Hi, reltub,

    as I donīt have access to Blomberg itīs hard to tell. But weīll try anyway. If you compare such a lot of lines to maybe be deleted Iīll try a slightly different setup as you have it - if your first check proves true the respective row is deleted but all further checks will be run as well.

    Letīs take the first part like
        'Delete bad data from Corp
        If rng3.Value Like "*Corp" And rng6.Value Like "1" Then Rows(B).Delete
        If rng3.Value Like "*Corp" And rng6.Value Like "0" Then Rows(B).Delete
        If rng3.Value Like "*Corp" And rng4.Value Like "#N/A*" Then Rows(B).Delete
        If rng3.Value Like "*Corp" And rng5.Value Like "#N/A*" Then Rows(B).Delete
        If rng3.Value Like "*Corp" And rng6.Value Like "#N/A*" Then Rows(B).Delete
        If rng3.Value Like "*Corp" And rng4.Value <> Month(Date) Then Rows(B).Delete
        'Getting error:Data type mismatch above
    Maybe you try to alter it to
            'Delete bad data from Corp
            If rng3.Value Like "*Corp" Then
              If rng6.Value Like "1" Or rng6.Value Like "0" Or rng6.Value Like "#N/A*" _
                  Or rng4.Value Like "#N/A*" Or rng5.Value Like "#N/A*" Or rng4.Value <> Month(Date) Then
                Rows(B).Delete
                Exit For
              End If
            End If
            'Getting error:Data type mismatch above
    I donīt know if that will help with the problem you encountered but when a criteria is met and the row is deleted the code will continue on the next row and not fulfill further checks for a row that maybe have been done before as well.

    Ciao,
    Holger

  9. #9
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    That is very fast and works, but it doesn't loop through the entire worksheet. I have to hit run for it to check each line. Is there something I'm missing here?

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected: end of statement in Macro

    Hi, reltub,

    you had a code which loops from the last line to the top. Insode that code thereīs a passge I cited which maybe could be replaced by the code I indicated. And if that works you could replace the other parts as well in the very same way.

    Ciao,
    Holger

  11. #11
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    So the new code could be somethin like this:

    Sub Step_2test()
    
        'This step of the Callable Bonds Download macro deletes the bad data from each Security Type
        'Cuts and pastes and security where Security Type is Corp and 0<Factor<1 into a new spreadsheet
        'Verify that this factor is for the current month; if not, delete entire row
    
        For Each ws In Sheets
        ws.Activate
     
        
        Set rng1 = Range("A1:A")
        Set rng2 = Range("B1:B")
        Set rng3 = Range("C1:C")
        Set rng4 = Range("D1:D")
        Set rng5 = Range("E1:E")
        Set rng6 = Range("F1:F")
    
    
        'Delete bad data from Corp
        If rng3.Address Like "*Corp" Then
              If rng6.Value Like "1" Or rng6.Value Like "0" Or rng6.Value Like "#N/A*" _
                  Or rng4.Value Like "#N/A*" Or rng5.Value Like "#N/A*" Or rng4.Value <> Month(Date) Then
                Rows(B).Delete
                Exit For
              End If
            End If
    
        Application.ScreenUpdating = True
        Application.DisplayStatusBar = True
    
    End Sub

  12. #12
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected: end of statement in Macro

    Hi, reltub,

    I think your compiler wouldnīt like that code sniplet (missing Next for both loops).

    Maybe the code may look like this:
    Sub Step_2()
    
        'This step of the Callable Bonds Download macro deletes the bad data from each Security Type
        'Cuts and pastes and security where Security Type is Corp and 0<Factor<1 into a new spreadsheet
        'Verify that this factor is for the current month; if not, delete entire row
    
        Dim ws As Worksheet
        Dim rng1 As Range, rng2 As Range, rng3 As Range
        Dim rng4 As Range, rng5 As Range, rng6 As Range
        Dim lngLast As Long, lngRow As Long
        
        For Each ws In Sheets
          ws.Activate
          lngLast = Range("B" & Rows.Count).End(xlUp).Row
          For lngRow = lngLast To 1 Step -1
          
            Set rng1 = Range("A" & lngRow)
            Set rng2 = Range("B" & lngRow)
            Set rng3 = Range("C" & lngRow)
            Set rng4 = Range("D" & lngRow)
            Set rng5 = Range("E" & lngRow)
            Set rng6 = Range("F" & lngRow)
        
            'Check to make sure that the cells are NOT requesting data
               
            'Delete bad data from Corp
            If rng3.Value Like "*Corp" Then
              If rng6.Value Like "1" Or rng6.Value Like "0" Or rng6.Value Like "#N/A*" _
                  Or rng4.Value Like "#N/A*" Or rng5.Value Like "#N/A*" Or rng4.Value <> Month(Date) Then
                Rows(lngRow).Delete
                Exit For
              End If
            End If
            'Getting error:Data type mismatch above
             
            'Delete bad data from Muni
            If rng3.Value Like "*Muni" Then
              If rng6.Value <> "CALLED IN FULL" Or rng4.Value <> Month(Date) Then
                Rows(lngRow).Delete
                Exit For
              End If
            End If
            
            'Delete bad data from Pfd
            If rng3.Value Like "*Pfd" Then
              If rng4.Value Like "#N/A*" Or rng5.Value Like "#N/A*" Or rng4.Value <> Month(Date) Then
                Rows(lngRow).Delete
                Exit For
              End If
            End If
            
            'For Corp, put any security with a factor other than 0 or 1 into a new spreadsheet
            'Verify that factor is in current month
          
          Next lngRow
        Next ws
    '    Application.ScreenUpdating = True
    '    Application.DisplayStatusBar = True
        
    End Sub
    Ciao,
    Holger

  13. #13
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    That makes sense, but for some reason it still isn't looping. It will find the last row where the description ends in Corp and delete it if the criteria are met, but it stops after that.

  14. #14
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected: end of statement in Macro

    Hi, reltub,

    now that was all my fault because of teh Exit For.

    Letīs try it with a slightly modified version:
    Sub Step_2()
    
        'This step of the Callable Bonds Download macro deletes the bad data from each Security Type
        'Cuts and pastes and security where Security Type is Corp and 0<Factor<1 into a new spreadsheet
        'Verify that this factor is for the current month; if not, delete entire row
    
        Dim ws As Worksheet
        Dim rng1 As Range, rng2 As Range, rng3 As Range
        Dim rng4 As Range, rng5 As Range, rng6 As Range
        Dim lngLast As Long, lngRow As Long
        Dim blnSkip As Boolean
        
        For Each ws In Sheets
          ws.Activate
          lngLast = Range("B" & Rows.Count).End(xlUp).Row
          For lngRow = lngLast To 1 Step -1
            
            blnSkip = False
            Set rng1 = Range("A" & lngRow)
            Set rng2 = Range("B" & lngRow)
            Set rng3 = Range("C" & lngRow)
            Set rng4 = Range("D" & lngRow)
            Set rng5 = Range("E" & lngRow)
            Set rng6 = Range("F" & lngRow)
        
            'Check to make sure that the cells are NOT requesting data
               
            'Delete bad data from Corp
            If rng3.Value Like "*Corp" Then
              If rng6.Value Like "1" Or rng6.Value Like "0" Or rng6.Value Like "#N/A*" _
                  Or rng4.Value Like "#N/A*" Or rng5.Value Like "#N/A*" Or rng4.Value <> Month(Date) Then
                Rows(lngRow).Delete
                blnSkip = True
              End If
            End If
            'Getting error:Data type mismatch above
             
            'Delete bad data from Muni
            If Not blnSkip Then
              If rng3.Value Like "*Muni" Then
                If rng6.Value <> "CALLED IN FULL" Or rng4.Value <> Month(Date) Then
                  Rows(lngRow).Delete
                  blnSkip = True
                End If
              End If
            End If
            
            'Delete bad data from Pfd
            If Not blnSkip Then
              If rng3.Value Like "*Pfd" Then
                If rng4.Value Like "#N/A*" Or rng5.Value Like "#N/A*" Or rng4.Value <> Month(Date) Then
                  Rows(lngRow).Delete
                End If
              End If
            End If
            
            'For Corp, put any security with a factor other than 0 or 1 into a new spreadsheet
            'Verify that factor is in current month
          
          Next lngRow
        Next ws
    '    Application.ScreenUpdating = True
    '    Application.DisplayStatusBar = True
        
    End Sub
    Ciao,
    Holger

  15. #15
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    That did it. I didn't notice that Exit For in there the first time, thanks. It it taking forever to go through the worksheet and test the criteria, but the good news is that I know why. Every time it deletes a row, all rows below shift up and thus, recalculate based on that BDP formula it inserted into the ranges in Step1. I need to copy and paste special values into G, H, and I, then delete the columns D, E, and F to speed this thing up.

  16. #16
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected: end of statement in Macro

    Hi, reltub,

    another option would be to put Application.Calculation = xlCalculationManual before the code and Application.Calculation = xlCalculationAutomatic at the end - by this calculation is only done once after the macro has finished deleting.

    Ciao,
    Holger

  17. #17
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    much, much better than using the clipboard! thank you for all your help, Holger!

  18. #18
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    Is this not the proper way to check if the date in C is in the current month? It is deleting everything on the sheet.

    rng4.Value <> Month(Date)

  19. #19
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected: end of statement in Macro

    Hi, reltub,

    I mentioned earlier on that I donīt have access to Blomberg so I donīt know whatīs the value of rng4.Value. I know for sure that Month(Date) will deliver 10 (a digit) for this month. If itīs a date in rng4 you may use Month(rng4.Value) but maybe itīs a string/text thatīs inside that cell.

    Let me give you a small example on that:
    Sub Sample()
    
    Dim varMonth As Variant
    
    varMonth = "10"
    
    If varMonth = Month(Date) Then
      Debug.Print "recognised Month as String"
    End If
    If varMonth * 1 = Month(Date) Then
      Debug.Print "recognised Month when multiplied"
    End If
    If CLng(varMonth) = Month(Date) Then
      Debug.Print "recognised Month when converted"
    End If
    
    varMonth = 10
    
    If varMonth = Month(Date) Then
      Debug.Print "fitting"
    End If
    
    End Sub
    Please indicate whatīs the content of a rng4.Value if this wonīt help.

    Ciao,
    Holger

  20. #20
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    rng4.value returns a date if a security has a call date, otherwise the cells will show "#n/a not applicable" if it does have a date, I want to see if the date is in the current month, if not I want to delete the entire row.

  21. #21
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    I get a type mismatch error when I use Month(rng4.Value) <> Month(Date). I can't figure out why it's doing this. For example, this macro should be deleting any row where the date in column D is not in the current month. The row below should be deleted:

    313375CP0 AGY230STEPUP 313375CP0 Corp 8/22/2012 100 .5231

    Sub test_corp()
    
      Application.Calculation = xlCalculationManual
        
        Dim ws As Worksheet
        Dim rng1 As Range, rng2 As Range, rng3 As Range
        Dim rng4 As Range, rng5 As Range, rng6 As Range
        Dim lngLast As Long, lngRow As Long
        Dim blnSkip As Boolean
        
        For Each ws In Sheets
          ws.Activate
          lngLast = Range("B" & Rows.Count).End(xlUp).Row
          For lngRow = lngLast To 1 Step -1
            
            blnSkip = False
            Set rng1 = Range("A" & lngRow)
            Set rng2 = Range("B" & lngRow)
            Set rng3 = Range("C" & lngRow)
            Set rng4 = Range("D" & lngRow)
            Set rng5 = Range("E" & lngRow)
            Set rng6 = Range("F" & lngRow)
        
            'Check to make sure that the cells are NOT requesting data
               
            'Delete bad data from Corp
            If rng3.Value Like "*Corp" Then
                If rng4.Value = "#N/A Field Not Applicable" And rng6.Value = "1" Or rng4.Value _
                = "#N/A Field Not Applicable" And rng6.Value = "0" Or Month(rng4.Value) <> Month(Date) Then
                Rows(lngRow).Delete
                blnSkip = True
              End If
            End If
          
          
          Next lngRow
        Next ws
        
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True
    Application.Calculation = xlCalculationAutomatic
    End Sub

  22. #22
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected: end of statement in Macro

    Hi, reltub,

    I hve to copy the line from this articel and paste it into the workbook you supplied. Although I use a different country setting as you the date is recognized as this by my system and thus the line is deleted. Right now I ran out of ideas of how to help solving this problem. Letīs see what this weekend brings (except extended coding ).

    Ciao,
    Holger

  23. #23
    Registered User
    Join Date
    10-16-2012
    Location
    tn
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Expected: end of statement in Macro

    Finally fixed this before I left work today. The code is basically fine, just had one small detail I was missing. I forgot to check and see if rng4 was actually something excel considered a date. Some cells contained #N/A and thus, confused excel. So I added this to the beginning of the code and it works great! thanks for all your help! I'm having another issue in another part of the code, but i'll save that for a new post! Have a good weekend!!

    If IsDate(rng4.Value) Then
                If Month(rng4.Value) <> Month(Now()) Then
                Rows(lngRow).Delete
                blnSkip = True
            End If
            End If

  24. #24
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Expected: end of statement in Macro

    @ reltub

    Welcome to the forum.

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ 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