View Full Version : output to textfile
jeff06
09-26-2007, 11:28 AM
I have a sheets with 100,000 rows, there cols
id   name  salary
34   jj        2334
33   uu       4556
99   kk        22333
----
 
 
I want to get a text for this sheet contains:
 
insert into #t values(34, 'jj', 2334)
insert into #t values(33, 'uu', 4556)
insert into #t values(99, 'kk', 22333)
----------
 
can some one give me a sample VBA code to do that?
Thanks
jeff06
09-26-2007, 01:06 PM
I tried following code, but it doest not work
 
 
Sub exportrange()
Dim filename As String, affix, rowstr
Dim numrows As Long, numcols As Long
Dim r As Long, c As Integer
Dim Data
Dim exprng As Range
Set exprng = Selection
numcols = exprng.Columns.Count
numrows = exprng.Rows.Count
affix = "insert into #midcntcrd values("
rowstr = affix
filename = "c:\cdrive\midnbr.txt"
Open filename For Output As #1
   For r = 1 To numrows
       For c = 1 To numcols
         Data = exprng.Cells(r, c).Value
         If c <> numcols Then
            rowstr = rowstr + " '" + Value + "',"
         Else
            rowstr = rowstr + "," + Value + "')\n"
         End If
    Next c
 Next r
 Print #1, rowstr
 Close #1
 
End Sub
 
Can someone kindly give some suggestions?
 
Thanks
anandbohra
09-26-2007, 11:20 PM
the below one is not my code but found it earlier & modified it as per my requirement
attached is excel file with example
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.