+ Reply to Thread
Results 1 to 9 of 9

BAT File VBA - Vary based on number of rows in worksheet

  1. #1
    Registered User
    Join Date
    06-24-2013
    Location
    Vancouver
    MS-Off Ver
    Excel 2010
    Posts
    25

    BAT File VBA - Vary based on number of rows in worksheet

    I have a VBA that is creating a .BAT file and running it. In the script there is a part I need to alter. Right now it is:

    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E2").Value

    This is printing to the Batch File the contents of "Sheet 7" cell E2.
    What I NEED it to do is look at sheet 7 and for each row print the corresponding cell in column E.

    So for example if there were 10 rows on sheet 7 (Row 1 is a header) the result in the VBA would be:

    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E2").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E3").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E4").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E5").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E6").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E7").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E8").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E9").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E10").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E11").Value


    If there were only 3 rows the result would be:

    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E2").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E3").Value
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E4").Value

    How would I do this?
    Thanks

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: BAT File VBA - Vary based on number of rows in worksheet

    Hi,

    implement the textstream object and try
    Please Login or Register  to view this content.
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: BAT File VBA - Vary based on number of rows in worksheet

    Hi Mamero

    Firstly, can you try to remember to enclose your quotes with the hashtags (#) or you'll have the moderators onto you LOL. It should look like my code below.

    Now the problem:-

    Please Login or Register  to view this content.
    Elegant Simplicity............. Not Always

  4. #4
    Registered User
    Join Date
    06-24-2013
    Location
    Vancouver
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: BAT File VBA - Vary based on number of rows in worksheet

    Thanks for the reply. Would I paste this code in to my existing module? How would it fit in to my existing script? Here's the full script:

    Public Sub TEST()
    Application.ScreenUpdating = False
    Const MY_FILENAME = "TEST.BAT"

    Dim FileNumber As Integer
    Dim retVal As Variant

    FileNumber = FreeFile

    Open MY_FILENAME For Output As #FileNumber
    Print #FileNumber, WriteLine; ThisWorkbook.Sheets(7).Range("E2").Value
    Close #FileNumber

    retVal = Shell(MY_FILENAME, vbNormalFocus)

    If retVal = 0 Then
    MsgBox "An Error Occured"
    Close #FileNumber
    End
    End If

    UpdateLogAfterConvert

    ActiveWorkbook.Save

    End Sub

  5. #5
    Registered User
    Join Date
    06-24-2013
    Location
    Vancouver
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: BAT File VBA - Vary based on number of rows in worksheet

    Thanks Andy. That did the trick perfectly. You guys are awesome!

  6. #6
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: BAT File VBA - Vary based on number of rows in worksheet

    Nice one ! The For/Next loop goes between the FileOpen and close.
    The dim and lastrow happen before that ...

  7. #7
    Registered User
    Join Date
    06-24-2013
    Location
    Vancouver
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: BAT File VBA - Vary based on number of rows in worksheet

    Nicely done! Cheers.

  8. #8
    Registered User
    Join Date
    06-24-2013
    Location
    Vancouver
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: BAT File VBA - Vary based on number of rows in worksheet

    One more thought on this. This script is assuming the information is always in "Sheet 7" of "this workbook". What would be the best way make the file and location user definable so if the data is in another sheet or even workbook the user can specify and it will populate the VBA. For example, I also have a button that prompts the user for a directory and populates a corresponding cell with that location. Could I do the same with workbooks, worksheets, and columns? A user could click a button/or buttons and browse for the excel file and populate a cell. The VBA would run based on this rather than a "hard wired" code. Thanks!

  9. #9
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: BAT File VBA - Vary based on number of rows in worksheet

    Hi,

    please check this example file (take care to adapt the file location): Bat.xlsm

    contains some code in userform and:
    Please Login or Register  to view this content.

+ 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