Results 1 to 5 of 5

Thread: Import / Export from excel worksheets to text file

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    5

    Question Import / Export from excel worksheets to text file

    HI GUys,

    need a little help with this, I need to transfer multiple cell contents from multiple sheets within a workbbook to a text file, and then be able to import the data back to the relevant cells at a later date.

    The cells in question will always be the same ones, but sometimes will be blank, so need to transfer blanks as well.

    I intend to run this from a command button (already created) and want it to save to either a text file or '.nfo' file i want the code to choose a default name and destination, but would like the user to have the option to change this when saving,
    Also when importing i need my workbook to check that the text file it is importing is for my workbook, i assume this can be done with a simple check to see if the first line has certain text in it (which can be inserted on saving the data in the first place)

    Thanks
    Gunslinger

  2. #2
    Banned
    Join Date
    Jul 2003
    Location
    New delhi
    Posts
    143
    hi Gunslinger


    the problem you have is a combination of Fso object and ecel file

    you can read and write a excel work book through excel object by
    getobject and createobject methoid
    with reference of micro soft excel object libery 8.0

    then the thing of text file we have to refer scripting object.

    so you have to take the file system object

    dim fs as new filesystem

    if you want to code then i can send to you after your response
    das

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    5

    sorted

    thank you for your reply ... i actually managed to get it sorted lastnight .... here is the 3 pieces of code i came up with ..... for those who may be trying something similar ....

    from a user for with 2 buttons run this code ...

    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     InOut_Select.Hide
    3.     getmydata
    4. End Sub
    5.  
    6. Private Sub CommandButton2_Click()
    7.     InOut_Select.Hide
    8.         On Error Resume Next
    9.         Dim Pt As String
    10.         Pt = ThisWorkbook.Path & "\Personal Information Files"
    11.         'Pt = 1909b workbook directory and new folder name
    12.         MkDir (Pt)
    13.     writemydata
    14. End Sub

    then to export .......

    VB Code:
    1. '
    2. Option Explicit
    3. Option Base 1
    4. '
    5. ' Saves Users Basic Details
    6. '
    7. Sub writemydata()
    8. '
    9. On Error GoTo errhandler
    10. '
    11. Dim Mycells(39) As Range
    12. Dim i As Integer
    13. Dim icell As Range
    14. Dim nme As String
    15.     nme = Sheets("1909").Range("I16").Text & " " & Sheets("1909").Range("E16").Text
    16. Dim Pth As String
    17.     Pth = ThisWorkbook.Path & "\Personal Information Files\" & "Basic Details for " & nme & " as of " & Format$(Now, "DD-MM-YY hhnn.ss") & ".csv"
    18. '
    19. Open Pth For Output As #1
    20. '
    21.    ' 1909 Monthly details to save
    22. '
    23. Set Mycells(1) = Sheets("1909").Range("C4")
    24. Set Mycells(2) = Sheets("1909").Range("C5")
    25. Set Mycells(3) = Sheets("1909").Range("C6")
    26. Set Mycells(4) = Sheets("1909").Range("K3")
    27. Set Mycells(5) = Sheets("1909").Range("F6")
    28. Set Mycells(6) = Sheets("1909").Range("K8")
    29. Set Mycells(7) = Sheets("1909").Range("C11")
    30. Set Mycells(8) = Sheets("1909").Range("B16")
    31. Set Mycells(9) = Sheets("1909").Range("E16")
    32. Set Mycells(10) = Sheets("1909").Range("I16")
    33. Set Mycells(11) = Sheets("1909").Range("L16")
    34. Set Mycells(12) = Sheets("1909").Range("O16")
    35. Set Mycells(13) = Sheets("1909").Range("G18")
    36. Set Mycells(14) = Sheets("1909").Range("K18")
    37. Set Mycells(15) = Sheets("1909").Range("N17")
    38. Set Mycells(16) = Sheets("1909").Range("C79")
    39. '
    40.     ' 1909 Certify details to save
    41. '
    42. Set Mycells(17) = Sheets("Certify").Range("C11")
    43. Set Mycells(18) = Sheets("Certify").Range("C13")
    44. Set Mycells(19) = Sheets("Certify").Range("C15")
    45. Set Mycells(20) = Sheets("Certify").Range("G11")
    46. Set Mycells(21) = Sheets("Certify").Range("G15")
    47. '
    48.     ' 1943 Detail to save
    49. '
    50. Set Mycells(22) = Sheets("1943").Range("F18")
    51. '
    52.     ' AL Details to save
    53. '
    54. Set Mycells(23) = Sheets("AL").Range("O8")
    55. Set Mycells(24) = Sheets("AL").Range("M9")
    56. Set Mycells(25) = Sheets("AL").Range("AJ8")
    57. Set Mycells(26) = Sheets("AL").Range("AH9")
    58. Set Mycells(27) = Sheets("AL").Range("I19")
    59. Set Mycells(28) = Sheets("AL").Range("AG17")
    60. Set Mycells(29) = Sheets("AL").Range("C22:I60")
    61. Set Mycells(30) = Sheets("AL").Range("U20:AG60")
    62. '
    63.     ' 1937 Details to save
    64. '
    65. Set Mycells(31) = Sheets("1937").Range("K28")
    66. Set Mycells(32) = Sheets("1937").Range("K29")
    67. Set Mycells(33) = Sheets("1937").Range("K30")
    68. Set Mycells(34) = Sheets("1937").Range("M31")
    69. Set Mycells(35) = Sheets("1937").Range("AP27")
    70. Set Mycells(36) = Sheets("1937").Range("Y34")
    71. Set Mycells(37) = Sheets("1937").Range("AM34")
    72. Set Mycells(38) = Sheets("1937").Range("BA34")
    73. Set Mycells(39) = Sheets("1937").Range("TextOld")
    74. '
    75. For i = LBound(Mycells) To UBound(Mycells)
    76. '
    77.     For Each icell In Mycells(i)
    78.         Write #1, icell.Value, icell.Worksheet.name, icell.Address
    79.     Next
    80. Next i
    81. '
    82. Close #1
    83. '
    84. MsgBox "Export complete, and appears to be ok", vbInformation, "Success !"
    85. '
    86. GoTo Resultok
    87. '
    88. errhandler:
    89.     MsgBox "An error was encountered during the export process, the data saved may be corrupt or missing completely.", vbExclamation, "Warning !"
    90.     Close #1
    91.     Exit Sub
    92. '
    93. Resultok:
    94. '
    95. End Sub

    then to import data back .....

    VB Code:
    1. '
    2. Option Private Module
    3. Option Base 1
    4. '
    5. Sub getmydata()
    6. '
    7. On Error GoTo oops
    8. '
    9. Dim mydata(3)
    10. Dim myFile As Variant
    11. Dim Checkarchive As String
    12. '
    13.     myFile = Application.GetOpenFilename("Text Files (*.csv), *.csv", , "Choose File to Import")
    14.     If myFile <> False Then
    15. '
    16. Application.ScreenUpdating = False
    17. '
    18. Open myFile For Input As #1
    19. '
    20. Do While Not EOF(1)
    21. '
    22. Input #1, mydata(1), mydata(2), mydata(3)
    23. '
    24. Checkarchive = mydata(2)
    25. '
    26. If Checkarchive <> "1909" Then
    27.     If Checkarchive <> "Certify" Then
    28.         If Checkarchive <> "1943" Then
    29.             If Checkarchive <> "AL" Then
    30.                 If Checkarchive <> "1937" Then
    31.                     MsgBox "An error occured during the importing process, the selected file may not be intended for this workbook, or may be corrupted, the requested data may be incorrect or incomplete, check all entries carefully", vbExclamation, "Warning !"
    32.     Close #1
    33.     Exit Sub
    34. '
    35.                 End If
    36.             End If
    37.         End If
    38.     End If
    39. End If
    40. '
    41.     Select Case mydata(1)
    42.         Case "#NOTHING#"
    43.         Sheets(mydata(2)).Range(mydata(3)).Value = ""
    44. '
    45.     Case Else
    46.         Sheets(mydata(2)).Range(mydata(3)).Value = mydata(1)
    47.     End Select
    48. '
    49. Loop
    50. '
    51. Close #1
    52. '
    53. Sheet16.Enquirytext.Text = Sheet16.Range("TextOld")
    54. '
    55. Application.ScreenUpdating = True
    56. '
    57. MsgBox "Import complete, and appears to be ok", vbInformation, "Success !"
    58. '
    59. GoTo Resultgood
    60.  
    61. oops:
    62.     Application.ScreenUpdating = True
    63. '
    64.     MsgBox "An error occured during the importing process, the selected file may not be intended for this workbook, or may be corrupted, the requested data may be incorrect or incomplete, check all entries carefully", vbExclamation, "Warning !"
    65.     Close #1
    66.     Exit Sub
    67. '
    68. Resultgood:
    69. '
    70. Else
    71.     Exit Sub
    72. End If
    73. '
    74. End Sub

    thats all folks ..... thanks for your help

    gunslinger
    Gunslinger

  4. #4
    Lively Member
    Join Date
    Mar 2002
    Location
    Virginia
    Posts
    88
    Can you please send working example of this process ? the code looks nice but with a working excel book it would be more clearer .
    thanks

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    5
    Originally posted by stono
    Can you please send working example of this process ? the code looks nice but with a working excel book it would be more clearer .
    thanks
    stono ... sure, just a couple of things tho ... the workbook alone
    that code is in is over 1.5mb, and it is also locked down fairly tight,
    so all you would see is the process running and not have access
    to the code.
    Plus some of that code would not be required in your project.

    what i suggest is email me, addy should be available, put in title
    "code request" ... then in the email explain a little about what you
    need to use it for and i will trim it and place the code into a blank
    workbook for you, so you can see how it works

    ok ?

    Gunslinger
    Gunslinger

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