Consulting

Results 1 to 7 of 7

Thread: .Rows.Count not working correctly?

  1. #1
    VBAX Regular
    Joined
    Dec 2012
    Posts
    35
    Location

    Question .Rows.Count not working correctly?

    Hi Everyone,
    I have this code here that seems to be working great. I'm using it to output some data from a model, but I noticed that this code didn't catch all the populated rows. Any idea why the rows wouldn't be counted correctly? The .CSV file gets populated with data from A5:AW17, but what I really want is A5:AW19.

    Also, please feel free to offer suggestions to improve my code in other ways!

    Thanks!


    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        Dim Msg As String, Ans As Variant
        Dim wline As Variant
        Dim r As Integer
        Dim lcol As Integer
        Dim strDir As String
        
        If Sheets("Sheet1").OLEObjects("CheckBox1").Object.Value = True Then
            
            
            wline = ""
            With Worksheets("Output")
            For r = 5 To .Cells(.Rows.Count, "A").End(xlUp).Row
                lcol = .Cells(r, 256).End(xlToLeft).Column
                On Error Resume Next
                wline = wline & Join(Application.Transpose(Application.Transpose(.Range("A" & r).Resize(, lcol))), ",") & vbNewLine
            Next r
            End With
            
             
            'If current quarter folder doesn't exist, create it
            DataAsOf = Sheets("Output").Range("A2").Value
            
            strDir = Trim('\\yadadada" & DataAsOf)
            If Dir(strDir, vbDirectory) = NullString Then
                MkDir (strDir)
            Else: End If
            
            
            Open "file://yadadada" & DataAsOf & "\" & Sheets("Output").Range("C5").Value & " " & DataAsOf & " (" & Sheets("Output").Range("E5").Value & ")" & ".csv" For Output As #1 'Replaces existing file
            Print #1, wline
            Close #1
        Else
         Exit Sub
        End If
        
        
    Quit:
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Are A18 and A19 empty?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Dec 2012
    Posts
    35
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Are A18 and A19 empty?
    I double checked the model to make sure they were populated. It seems to be an anomaly since it works fine for most of the other models.

  4. #4
    VBAX Expert mperrah's Avatar
    Joined
    Mar 2005
    Posts
    744
    Location
    If you know you want to column AW19 every time try this:
        With Worksheets("Output") 
                lcol = 49 'same as AW
                For r = 5 To 19
    
                    On Error Resume Next 
                    wline = wline & Join(Application.Transpose(Application.Transpose(.Range("A" & r).Resize(, lcol))), ",") & vbNewLine 
                Next r 
            End With

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    1. If you comment out the On Error Resume Next, do you now get and error message?

    2. In the code

      strDir = Trim( '\\yadadada" & DataAsOf)
    Should that be a double quote\\yada....?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Regular
    Joined
    Dec 2012
    Posts
    35
    Location
    Quote Originally Posted by mperrah View Post
    If you know you want to column AW19 every time try this:
        With Worksheets("Output") 
                lcol = 49 'same as AW
                For r = 5 To 19
    
                    On Error Resume Next 
                    wline = wline & Join(Application.Transpose(Application.Transpose(.Range("A" & r).Resize(, lcol))), ",") & vbNewLine 
                Next r 
            End With
    This is certainly a solution as the range shouldn't be dynamic. Thanks for your input!

    Quote Originally Posted by Paul_Hossler View Post
    1. If you comment out the On Error Resume Next, do you now get and error message?

    2. In the code

      strDir = Trim( '\\yadadada" & DataAsOf)
    Should that be a double quote\\yada....?
    1. I do get an error message for the line of code right after the 'On Error Resume Next'...Type Mismatch
    2. Yup! I was just messing with the directory when typing up my post, fudged that a bit.

  7. #7
    VBAX Regular
    Joined
    Dec 2012
    Posts
    35
    Location
    I'm marking this thread as solved. The mishap seemed to be an anomaly and I'll with mperrah's suggestion -- use a fixed range.

Posting Permissions

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