Results 1 to 6 of 6

Thread: Reproducing text in visual basic for excel

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    56

    Reproducing text in visual basic for excel

    I need to reproduce some text into a textfile and therefore I tried to use a for-loop.
    The text that needs to be reproduced is the following:

    new Array("r0c0","r0c1","r0c2"),
    new Array("r1c0","r1c1","r1c2"),
    new Array("r2c0","r2c1","r2c2"),
    new Array("r3c0","r3c1","r3c2"),
    new Array("r4c0","r4c1","r4c2")

    These are no visual basic commands (javascript text). Only the text needs to be reproduced into a textfile.
    r?c? which you see in the text represent cell values in excel.
    The above text is for 5 lines (text new array 1 till 5)and 3 cell values (e.g. r0c0 till r0c2).
    If we have for example 10 lines and 7 cell valuess then
    the text would look like:

    new Array("r0c0","r0c1","r0c2","r0c3","r0c4","r0c5","r0c6"),
    new Array("r1c0","r1c1","r1c2","r1c3","r1c4","r1c5","r1c6"),
    new Array("r2c0","r2c1","r2c2","r2c3","r2c4","r2c5","r2c6"),
    new Array("r3c0","r3c1","r3c2","r3c3","r3c4","r3c5","r3c6"),
    new Array("r4c0","r4c1","r4c2","r4c3","r4c4","r4c5","r4c6"),
    new Array("r5c0","r5c1","r5c2","r5c3","r5c4","r5c5","r5c6"),
    new Array("r6c0","r6c1","r6c2","r6c3","r6c4","r6c5","r6c6"),
    new Array("r7c0","r7c1","r7c2","r7c3","r7c4","r7c5","r7c6"),
    new Array("r8c0","r8c1","r8c2","r8c3","r8c4","r8c5","r8c6"),
    new Array("r9c0","r9c1","r9c2","r9c3","r9c4","r9c5","r9c6")

    So I created the following visual basic code:

    Sub create_file()
    Dim fso, txtfile
    Set fso = CreateObject("scripting.filesystemobject")
    Set txtfile = fso.createtextfile("c:\Windows\Desktop\qrt.txt", True)

    For x = 1 To 5
    txtfile.write ("new Array" & Chr(40) & Chr(34) & Cells(x, 1) & Chr(34) & vbNewLine)
    Next x

    End Sub

    If I execute this code then the output would look like:

    new Array("48"
    new Array("49"
    new Array("50"
    new Array("51"
    new Array("52"

    48 till 52 are the numbers that are given in cells A1 till A5. I know how to reproduce the
    number of lines but how do you create a loop to reproduce the number of cell values. Because
    sometimes my excelworksheet contains data for a range of e.g. 36 rows and 56 colums or
    e.g. 5 rows and 20 colums or any other number. Does anyone know how to create the second loop.

  2. #2
    Frenzied Member jjortiz's Avatar
    Join Date
    Mar 2001
    Location
    NYC
    Posts
    1,768
    Why not just read in the values from the Excel Spread sheet. Use the Excel object. Or are you trying to place the values from the text file to the spread sheet?

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    56

    RE: creating textfile

    No, what I want to is publish the values from of the excelsheet on the internet. That is why I want to create javascipt commands into a textfile. You can also use the build-in microsoft add-in for creating a table into a HTML file but then if you want to create 32 HTML files with all different tables of different sizes then you cannot use this add-in because the macro recorder does not give you the code.So you cannot automate this in a simple way. So the only thing I need is the text that is quoted above because that is the part of the javascript that is for each table different.
    So what I want to know is how to create e.g. the following text:
    new Array("48","85"),
    new Array("49","45"),
    new Array("50","78"),
    new Array("51","2"),
    new Array("52","90")

    This must be flexible for the number of lines. So above you see 5 lines and two cell values but it can also be 7 lines and 5 cell values (I mean by that a table with 35 values).So I want to know how the change the sub below that it is flexibel for both.


    Sub create_file()
    Dim fso, txtfile
    Set fso = CreateObject("scripting.filesystemobject")
    Set txtfile = fso.createtextfile("c:\Windows\Desktop\qrt.txt", True)

    For x = 1 To 5
    txtfile.write ("new Array" & Chr(40) & Chr(34) & Cells(x, 1) & Chr(34) & vbNewLine)
    Next x

    End Sub

  4. #4
    WorkHorse
    Guest

    Nested loop with variables

    Is this what you are looking for?

    VB Code:
    1. Dim intNumberOfRows As Integer
    2. Dim intNumberOfColumns As Integer
    3. Dim strLineText As String
    4.  
    5. intNumberOfRows = 5
    6. intNumberOfColumns = 2
    7.  
    8. For x = 1 To intNumberOfRows
    9.     For y = 1 To intNumberOfColumns
    10.         strLineText = strLineText & Chr(34) & Cells(x, y) & Chr(34) & ","
    11.     Next y
    12.     txtfile.write ("new Array" & Chr(40) & strLineText & vbNewLine)
    13. Next x

  5. #5
    WorkHorse
    Guest
    Opps. You need to clear your string each time:

    VB Code:
    1. Dim intNumberOfRows As Integer
    2. Dim intNumberOfColumns As Integer
    3. Dim strLineText As String
    4.  
    5. intNumberOfRows = 5
    6. intNumberOfColumns = 2
    7.  
    8. For x = 1 To intNumberOfRows
    9.    
    10.     strLineText = ""
    11.     For y = 1 To intNumberOfColumns
    12.         strLineText = strLineText & Chr(34) & Cells(x, y) & Chr(34) & ","
    13.     Next y
    14.    
    15.     strLineText = "new Array(" & Left$(strLineText, Len(strLineText) - 1) & ")"
    16.     If x < intNumberOfRows Then strLineText = strLineText & ","
    17.     strLineText = strLineText & vbNewLine
    18.    
    19.     txtfile.write strLineText
    20.    
    21. Next x

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    56
    Thank you WorkHorse you have solved my problem.

Posting Permissions

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



Click Here to Expand Forum to Full Width