+ Reply to Thread
Results 1 to 5 of 5

code works running in visual basic editor but not when running from Excel macro

Hybrid View

smporco code works running in visual... 11-06-2016, 03:01 PM
alansidman Re: code works running in... 11-06-2016, 03:09 PM
xladept Re: code works running in... 11-06-2016, 03:38 PM
smporco Re: code works running in... 11-06-2016, 03:55 PM
xladept Re: code works running in... 11-06-2016, 05:44 PM
  1. #1
    Registered User
    Join Date
    09-20-2013
    Location
    las vegas, Nevada
    MS-Off Ver
    Excel 2003
    Posts
    4

    code works running in visual basic editor but not when running from Excel macro

    I've spent hours "googleing" and trying to troubleshoot this with no luck. The following code works great running from within visual basic editor but not when running from Excel macro. I suspect may be mixing up the use of objects & properties. I hope there may be someone in this group that can help and I thank you in advance to looking at this!


    Option Explicit
    
    Public i, j, k, l, m As Integer, row, lastRow, lastRowPastDue, lastRowPaidOff, lastRowToCxl, lastRowToTxf, matchFoundIndex As Long
    Public wb, wb2, wbsource, wbdest As Workbook
    Public ws, wssource, wsdest As Worksheet
    Public strDate As String
    Public CurrentDate, PriorDate As Date
    Public MonthNameSel, YearSel  As String
    Public EOMday As String
    Public SeqNum, PriorSeqNum As String
    Public MonthNm, PriorMoNm As String
    Public MonthNum, PriorMoNo As String
    Public PriorMoYear As String
    Public strSearch1, strSearch2 As String
    Public Pool, PoolName, PoolNo, PoolNo2, PoolNo3 As String
    Public r, rCell, SelectCell, Rng1, Rng2, TimeRng, SourceRng As Range
    Public filterCriteria, arr As Variant
    Dim answer, note, myString, ary As String
    
                    
    'set folder path file string naming conventions
    Public Function strPath() As String
        strPath = "\\usnvfsa0\accounting$\Office Records\Accounting - NEW\"
    End Function
    Public Function folderPath() As String
        folderPath = SeqNum & "   " & YearSel & " Business Records\Servicer Related Documents - FIN 220\WAAM Reporting\"
    End Function
    Public Function PriorFolderPath() As String
        PriorFolderPath = PriorSeqNum & "   " & PriorMoYear & " Business Records\Servicer Related Documents - FIN 220\WAAM Reporting\"
    End Function
    Public Function PriorFolderPath2() As String
        PriorFolderPath2 = PriorSeqNum & "   " & PriorMoYear & " Business Records\Servicer Related Documents - FIN 220\WAAM Reporting\"
    End Function
    Public Function FolderPath2() As String
        FolderPath2 = PriorSeqNum & "   " & PriorMoYear & " Business Records\Servicer Related Documents - FIN 220\WAAM Reporting\"
    End Function
    Public Function FolderMo() As String
        FolderMo = MonthNum & " " & MonthNameSel & "\"
    End Function
    Public Function PriorFolderMo() As String
        PriorFolderMo = PriorMoNo & " " & PriorMoNm & "\"
    End Function
    Public Function FolderMo2() As String
        FolderMo2 = MonthNum & " " & MonthNameSel
    End Function
    Public Function QueryFolder() As String
        QueryFolder = "HyperionQueries"
    End Function
    Public Function ServicerName() As String
        ServicerName = YearSel & "_" & MonthNum & "_" & Pool & " Servicer Report"
    End Function
    Public Function PriorServicerName() As String
        PriorServicerName = PriorMoYear & "_" & PriorMoNo & "_" & Pool & " Servicer Report"
    End Function
    Public Function SRInputName() As String
        SRInputName = YearSel & "_" & MonthNum & "_WAAM SRInput"
    End Function
    Public Function PriorSRInputName() As String
        PriorSRInputName = PriorMoYear & "_" & PriorMoNo & "_WAAM SRInput"
    End Function
    
    
    ''''''''''''HERE"S THE CODE   Prior to the below section of code, user forms collect values for month, year, and pool number which are used in the naming conventions.
    
    
    SRInputFilter()
         
        Set wbdest = Workbooks.Open(strPath & folderPath & FolderMo & ServicerName & ".xlsx", WriteResPassword:="XXXX", UpdateLinks:=0)    'open report (destination)
        Set wsdest = wbdest.Sheets("Manual Transfers")
                       
        With wsdest                                                                'capture any "processing error" when From pool(column E)  &  To pool (column F) codes are identical & highlight row yellow
                lastRow = wsdest.Range("C1000").End(xlUp).row
                For i = 3 To lastRow
                    
    '=====>>>>NONE OF THE FOLLOWING RUNS FROM THE EXCEL MACRO, BUT IT DOES WHEN RUNNING OUT OF VISUAL BASIC EDITOR
    
                       If Range("E" & i) = Range("F" & i) Then                 'if from pool code in column E = to pool code in column F
                        Range("G" & i).Copy                                       'copy value from balance (column G) to column H (for reference note - data not used)
                        Range("H" & i).PasteSpecial xlPasteValues
                        Range("G" & i).Interior.Color = vbYellow            'highlight column G's cell in yellow
                        Range("G" & i).ClearContents                           'clear column G's cell value
                    End If
                Next i
        End With
                                                                                          'move past due & paid off activities to lower tab areas     
        If PoolNo = "6" Then
        'With wsdest
                lastRow = wsdest.Range("C1000").End(xlUp).row
                j = 1039                    'to reference for beginning paid off 11-6 section data row1
                k = 1026                    'to reference for beginning past due 11-6 section data row1
                m = 1016                    'to reference for beginning transfers from 6-11 section row1
                l = 1001                    'to reference for beginning to be cxld 5-6 section data row1
                For i = 3 To lastRow        'data begins on row 3
                            
                    If Cells(i, 5) = "11" And Cells(i, 7) <> "" Then            'if from pool (column E) = 11 and balance (column G) is not empty
                        If Cells(i, 7) >= "0" Then                                  'if balance >= 0 need to move to Past Dues section
                            k = k + 1
                            Range("A" & i & ":G" & i).Copy                                      'move row values to past at bottom & clear top area row values
                            Range("A" & k & ":G" & k).PasteSpecial xlPasteValues
                            Range("A" & i & ":G" & i).ClearContents
                        End If
                        If Cells(i, 7) < "0" Then                                   'if balance <0 need to move to Paid Offs section
                            j = j + 1
                            Range("A" & i & ":G" & i).Copy                                  'move row values to past at bottom & clear top area row values
                            Range("A" & j & ":G" & j).PasteSpecial xlPasteValues
                            Range("A" & i & ":G" & i).ClearContents
                        End If
                    If Cells(i, 5) = "5" Then                                        'if balance from pool (column E) = 5, move to below (to be cancelled) section
                            l = l + 1
                            Range("A" & i & ":G" & i).Copy
                            Range("A" & l & ":G" & l).PasteSpecial xlPasteValues
                            Range("A" & i & ":G" & i).ClearContents
                    End If
                    End If
                Next i
                                                                    'hide empty rows in past due & paid off sections
            lastRowToCxl = wsdest.Range("G1012").End(xlUp).row
            lastRowToCxl = lastRowToCxl + 1
            wsdest.Rows(lastRowToCxl & ":1011").EntireRow.Hidden = True
    
            lastRowToTxf = wsdest.Range("G1025").End(xlUp).row
            lastRowToTxf = lastRowToTxf + 1
            wsdest.Rows(lastRowToTxf & ":1024").EntireRow.Hidden = True
            
            lastRowPastDue = wsdest.Range("G1038").End(xlUp).row
            lastRowPastDue = lastRowPastDue + 1
            wsdest.Rows(lastRowPastDue & ":1037").EntireRow.Hidden = True
                                    
            lastRowPaidOff = wsdest.Range("G1051").End(xlUp).row
            lastRowPaidOff = lastRowPaidOff + 1
            wsdest.Rows(lastRowPaidOff & ":1050").EntireRow.Hidden = True
        'End With
        End If
       
        If PoolNo = "5" Then
                                                                'hide empty rows in past due & paid off sections
            lastRowToTxf = wsdest.Range("G1010").End(xlUp).row
            lastRowToTxf = lastRowToCxl + 1
            wsdest.Rows(lastRowToTxf & ":1009").EntireRow.Hidden = True
        
        End If
           
        Set wbsource = Workbooks.Open(strPath & folderPath & FolderMo & SRInputName & ".xlsx", UpdateLinks:=0)     'open SRInput (source)
        Set wssource = wbsource.Sheets("Funding")
    
                                            'stamp destination sheet with time & doc source
        Set TimeRng = wsdest.Range("C1")
        Set SourceRng = wsdest.Range("E1")
        SourceRng.Value = strPath & folderPath & FolderMo & SRInputName & ".xlsx"   '
        TimeRng.Value = Format(Now(), "mm/dd/yy hh:mm")
                                                                                        
    'copy Removals - Paid off
        
        Set wssource = wbsource.Sheets("Removals")
        Set wsdest = wbdest.Sheets("Xfr To (Auto)-Paid Off")
                
            With wssource
                '.AutoFilterMode = False
                .UsedRange.AutoFilter
                .Range("C:C").AutoFilter Field:=3, Criteria1:="*" & PoolNo & "*"            'filter for pool code either from or to in column C
                .Range("I:I").AutoFilter Field:=9, Criteria1:="CONTRACT PAID OFF"           'Filter Contract Paid Off only
                .Range("A1:O99").Copy
            End With
            With wbdest.Sheets("Xfr To (Auto)-Paid Off").Range("A2")
                .PasteSpecial (xlPasteValues), Operation:=xlNone, skipblanks:=False, Transpose:=False   'copy source to destination
                Application.CutCopyMode = False
            End With
                                            'clear if any ASO duplicate processing entries
                lastRow = wsdest.Range("A99").End(xlUp).row
                row = 3
        
            With wsdest          
                    
            lastRow = lastRow + 1
            wsdest.Rows(lastRow & ":98").EntireRow.Hidden = True                              'hide section's empty data rows
                        
                                            'stamp destination sheet with time & doc source
                Set TimeRng = wsdest.Range("C1")
                Set SourceRng = wsdest.Range("E1")
                SourceRng.Value = strPath & folderPath & FolderMo & SRInputName & ".xlsx"
                TimeRng.Value = Format(Now(), "mm/dd/yy hh:mm")
            End With
                                                               
    'copy Transfers - Aged
    
        Set wsdest = wbdest.Sheets("Xfr To (Auto)-Aged")
        Set wssource = wbsource.Sheets("Removals")
    
            With wssource
                .AutoFilterMode = False
                .UsedRange.AutoFilter
                .Range("C:C").AutoFilter Field:=3, Criteria1:="*" & PoolNo & "*"            'filter for pool code either from or to in column C"
                .Range("I:I").AutoFilter Field:=9, Criteria1:="<>CONTRACT PAID OFF"         'filter all excluding Paid Off
                .Range("A1:O99").Copy
            End With
            With wbdest.Sheets("Xfr To (Auto)-Aged").Range("A2")
                .PasteSpecial (xlPasteValues), Operation:=xlNone, skipblanks:=False, Transpose:=False   'copy source to destination
                Application.CutCopyMode = False
            End With
                                            'clear if any ASO duplicate processing entries
                lastRow = wsdest.Range("A99").End(xlUp).row
                row = 3
    
            With wsdest  
                            
            lastRow = lastRow + 1
            wsdest.Rows(lastRow & ":98").EntireRow.Hidden = True                             'hide section's empty data rows
                        
                                            'stamp destination sheet with time & doc source
                Set TimeRng = wsdest.Range("C1")
                Set SourceRng = wsdest.Range("E1")
                SourceRng.Value = strPath & folderPath & FolderMo & SRInputName & ".xlsx"       'stamp for time & doc source
                TimeRng.Value = Format(Now(), "mm/dd/yy hh:mm")
            End With
                             
    'End If
    
    wbsource.Close savechanges:=False
    wbdest.Close savechanges:=True
    
        ThisWorkbook.Worksheets("Macros").Select
        MsgBox "Done!"
    
    End Sub
    Last edited by alansidman; 11-06-2016 at 03:08 PM. Reason: code tags added

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,754

    Re: code works running in visual basic editor but not when running from Excel macro

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: code works running in visual basic editor but not when running from Excel macro

    Try adding to this line:

    Set wsdest = wbdest.Sheets("Manual Transfers"):wsdest.Activate
    You don't have the dots in the first with block and you've commented out the others
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Registered User
    Join Date
    09-20-2013
    Location
    las vegas, Nevada
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: code works running in visual basic editor but not when running from Excel macro

    I see what you're pointing out. Maybe looking at this more simplistically would help.
    The 1st "with" block runs in debug but just seems to bypass the code when running from the Excel macro.
    As I'm working with multiple workbooks, I think I would prefer an object approach and avoid using activations and or selections.



    With wsdest                                                                'capture any "processing error" when From pool(column E)  &  To pool (column F) codes are identical & highlight row yellow
                lastRow = wsdest.Range("C1000").End(xlUp).row
                For i = 3 To lastRow
                    
    '=====>>>>NONE OF THE FOLLOWING RUNS FROM THE EXCEL MACRO, BUT IT DOES WHEN RUNNING OUT OF VISUAL BASIC EDITOR
    
                       If Range("E" & i) = Range("F" & i) Then                 'if from pool code in column E = to pool code in column F
                        Range("G" & i).Copy                                       'copy value from balance (column G) to column H (for reference note - data not used)
                        Range("H" & i).PasteSpecial xlPasteValues
                        Range("G" & i).Interior.Color = vbYellow            'highlight column G's cell in yellow
                        Range("G" & i).ClearContents                           'clear column G's cell value
                    End If
                Next i
        End With

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: code works running in visual basic editor but not when running from Excel macro

    What I was saying is that you're not using the dot operator

+ 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. Macro has errors if Visual Basic Editor is not open
    By Whizbang in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2022, 03:09 PM
  2. The Code Window in the Visual Basic Editor of Excel
    By Marilyaryott in forum Tips and Tutorials
    Replies: 0
    Last Post: 03-23-2012, 10:31 PM
  3. Visual basic Editor in Excel 2007
    By vijayiitbbs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-07-2011, 11:58 AM
  4. Visual basic Code or editor won't load in Excel
    By johnsday in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-02-2006, 02:10 PM
  5. Can I run Visual Basic procedure using Excel Visual Basic editor?
    By john.jacobs71 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-26-2005, 10:25 AM
  6. [SOLVED] Visual Basic Editor for Excel
    By JoeABC in forum Excel General
    Replies: 1
    Last Post: 08-11-2005, 11:05 PM
  7. [SOLVED] When you edit a macro in visual basic editor, why doesn't it upda.
    By kfrick@vg302.org in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2005, 01:06 PM
  8. Running an excel macro within Visual basic
    By pkrono in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-10-2005, 05:03 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