+ Reply to Thread
Results 1 to 11 of 11

Printing Results to a text file

Hybrid View

  1. #1
    Registered User
    Join Date
    05-09-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    8

    Printing Results to a text file

    Hi, I'm having a little trouble with this code below, the code works perfectly fine but stops when it finds an empty row. I intentionally include 1 blank row between groups of rows on the spreadsheet i'm working on. This cause's this code to stop after one group, i've tried telling the code to check the first empty row with the next row coming up to see if that row is empty also but my attempts failed & i'm not sure how to do it.
    I have included the code below.

    Can this code be made to only stop once it finds 2 empty rows and not just 1?

    Below working code if no blank rows are present.
    Sub ImageUpload()
        Dim intUnit As Integer
        Dim lngRow As Long
        Dim strFileName As String
        
        strFileName = "C:\Users\James\Desktop\ImageUpload.bps"
        intUnit = FreeFile
        Open strFileName For Output As intUnit
    
        lngRow = 6
        Do While Len(Cells(lngRow, 1).Value) > 0
            If Cells(lngRow, 53).Value = "m" Then
               Print #intUnit, Cells(lngRow, 55).Value
               Print #intUnit, "1"
               Print #intUnit, "2"
               Print #intUnit, "0"
               Print #intUnit, "?"
               Print #intUnit, Cells(lngRow, 56).Value
            End If
            lngRow = lngRow + 1
        Loop
        Close intUnit
        
    End Sub
    below code that i tried editing to check for 2 rows.

    Sub ImageUpload()
        Dim intUnit As Integer
        Dim lngRow As Long
        Dim strFileName As String
        
        strFileName = "C:\Users\James\Desktop\ImageUpload.bps"
        intUnit = FreeFile
        Open strFileName For Output As intUnit
    
        lngRow = 6
          Do While Len(Cells(lngRow, 1).Value) & Len(Cells(lngRow + 1, 1).Value) > 0
            If Cells(lngRow, 53).Value = "m" Then
               Print #intUnit, Cells(lngRow, 55).Value
               Print #intUnit, "1"
               Print #intUnit, "2"
               Print #intUnit, "0"
               Print #intUnit, "?"
               Print #intUnit, Cells(lngRow, 56).Value
            End If
            lngRow = lngRow + 1
        Loop
        Close intUnit
        
    End Sub
    Thanks

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Printing Results to a text file

    Hello chuckie2010,

    If you have only one blank between the groups you could find the end of the range using something like this...
    Sub ImageUpload()
    
        Dim intUnit As Integer
        Dim lngRow As Long
        Dim Rng As Range
        Dim strFileName As String
        
        strFileName = "C:\Users\James\Desktop\ImageUpload.bps"
        intUnit = FreeFile
        
        Open strFileName For Output As intUnit
          Set Rng = Range(Cells(6, 53), Cells(Rows.Count, 53)).End(xlUp)
          For Each Cell In Rng
            If Cell.Value = "m" Then
               Print #intUnit, Cell.Offset(0, 2).Value
               Print #intUnit, "1"
               Print #intUnit, "2"
               Print #intUnit, "0"
               Print #intUnit, "?"
               Print #intUnit, Cell.Offset(0, 4).Value
            End If
            lngRow = lngRow + 1
          Next Cell
        Close intUnit
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    05-09-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Printing Results to a text file

    Hi Leith, just tried that code in my spread sheet and it just produces an empty file. I've attached a copy of my excel sheet, and a copy of the exported results that my first lot of code created.

    Hopefully you can help elimate the problem.
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Printing Results to a text file

    Hello chuckie2010,

    My typing is my weakness. I corrected the type in the macro. Here is the working code. This has been added to the attached worksheet.
    Sub ImageUpload()
    
        Dim intUnit As Integer
        Dim lngRow As Long
        Dim Rng As Range
        Dim strFileName As String
        
        strFileName = "C:\ImageUpload.txt"
        intUnit = FreeFile
        
        Open strFileName For Output As intUnit
          Set Rng = Range(Cells(6, "BA"), Cells(Rows.Count, "BA").End(xlUp))
          Addx = Rng.Address
          For Each Cell In Rng
            If Cell.Value = "m" Then
               Print #intUnit, Cell.Offset(0, 2).Value
               Print #intUnit, "1"
               Print #intUnit, "2"
               Print #intUnit, "0"
               Print #intUnit, "?"
               Print #intUnit, Cell.Offset(0, 4).Value
            End If
            lngRow = lngRow + 1
          Next Cell
        Close intUnit
        
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    05-09-2010
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Printing Results to a text file

    Hi Leith,
    Your a star, thanks for fixing that code.
    This evening i've been working on another macro for the same spreadsheet which is based on the same code. Would you mind performing your magic on this please.

    Sub ProductImportList()
        Dim intUnit As Integer
        Dim lngRow As Long
        Dim strFileName As String
        Dim Txt1 As String, Txt2 As String, Txt3 As String, Txt4 As String, Txt5 As String
        Dim Txt6 As String, Txt7 As String, Txt8 As String, Txt9 As String, Txt10 As String
        Dim Txt11 As String, Txt12 As String, Txt13 As String, Txt14 As String, Txt15 As String
        Dim FileHeaders As String
        
        
        strFileName = "C:\Users\James\Desktop\productlist.csv"
        intUnit = xlCSV
        Open strFileName For Output As intUnit
    
            FileHeaders = "store,websites,attribute_set,type,category_ids,sku,has_options,gift_message_available,image,name,options_container,small_image,thumbnail,url_key,url_path,image_label,thumbnail_label,small_image_label,price,weight,description,short_description,custom_design_from,status,tax_class_id,visibility,enable_googlecheckout,qty,min_qty,use_config_min_qty,is_qty_decimal,backorders,use_config_backorders,min_sale_qty,use_config_min_sale_qty,max_sale_qty,use_config_max_sale_qty,is_in_stock,low_stock_date,notify_stock_qty,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,stock_status_changed_automatically,product_name,store_id,product_type_id"
            Print #intUnit, FileHeaders
        lngRow = 6
        Do While Len(Cells(lngRow, 1).Value) > 0
            If Cells(lngRow, 53).Value = "m" Then
             If Cells(lngRow, 50).Value > 0 Then
               
               
               Txt1 = "admin,base,Default,simple,"
               Txt2 = Cells(lngRow, 35).Value & "," & Cells(lngRow, 1).Value & ","
               Txt3 = "0,No,"
               Txt4 = Cells(lngRow, 57).Value & "," & Cells(lngRow, 6).Value & ","
               Txt5 = "Block after Info Column,"
               Txt6 = Cells(lngRow, 57).Value & "," & Cells(lngRow, 57).Value & ","
               Txt7 = ",,"
               Txt8 = Cells(lngRow, 6).Value & "," & Cells(lngRow, 6).Value & "," & Cells(lngRow, 6).Value & "," & Cells(lngRow, 15).Value & "," & Cells(lngRow, 32).Value & "," & Cells(lngRow, 33).Value & "," & Cells(lngRow, 34).Value & ","
               Txt9 = ",Enabled,Taxable Goods,"
               Txt10 = Cells(lngRow, 36).Value & ","
               Txt11 = "No,"
               Txt12 = Cells(lngRow, 50).Value & ","
               Txt13 = "0,1,0,0,1,1,1,100,1,1,,1,1,1,1,1,"
               Txt14 = Cells(lngRow, 6).Value & ","
               Txt15 = "0,simple"
               
               Print #intUnit, Txt1 & Txt2 & Txt3 & Txt4 & Txt5 & Txt6 & Txt7 & Txt8 & Txt9 & Txt10 & Txt11 & Txt12 & Txt13 & Txt14 & Txt15
               
            End If
            End If
            lngRow = lngRow + 1
        Loop
        Close intUnit
        
    End Sub
    Thanks Leith

    James

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Printing Results to a text file

    Hello James,

    I can help you but I am not sure what is you want help with. Can you provide me with some more detail and a before and after example of the file layout?

+ 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