another question open file

Asche

Board Regular
Joined
Jun 25, 2008
Messages
50
okay trying to open a txt so I can copy the numbers or words into a cell.
there are 5 numbers in each txt, that need to go into 5 cells.

I have the application.getopenfile and when I click on the test file, it gives me an error. that its can't open or do that operation.
can any one help?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Option Explicit

Private Sub cmdfind_Click()
Dim NextRow As Long
Application.GetOpenFilename
If Empty Then
ActiveCell.Paste
Else
NextRow = Range("A" & Rows.Count).End(xlUp).Row + 1
ActiveCell.Paste
End If


End Sub


not very good code I know
 
Upvote 0
GetOpenFileName does not OPEN the book, it just get's the name...you need an open command using getopenfilename...

Workbooks.Open Application.GetOpenFileName

That will open the book..


And what is the rest of the code supposed to do?
copy WHAT 5 cells? Paste them WHERE ?
Give specifics...
 
Upvote 0
not opening a work book.
opening a .txt file

but there are 5 numbers in each .txt file, that need to be copied into the next 5 available cells
 
Upvote 0
did that but it opens a new work book I need it to paste it in next 5 cells of the current book
 
Last edited:
Upvote 0
did that but it opens a new work book I need it to paste it in next 5 cells

OK, to quote "Jerry McQuire"
"Help Me....Help You!!"

You need it to paste WHAT, WHERE ?
Paste It (paste WHAT?) Nothing was copied...
in the Next 5 Cells....Next 5 Cells AFTER which Cell?

Take a step back, Deep breath..
Explain in Detail from step 1 to the end exactly what you want the code to do.

include
Book Names, Which one is the source, which one is the destination..
Sheet names and cell references
What exactly do you want copied, and where EXACTLY do you want it pasted?
 
Upvote 0
Try something like this. Call Test from your cmdFind_Click.
Code:
Option Explicit

Sub Test()

    Dim sFile As String
    Dim LastRow As Long
    Dim fileNum As Integer
    Dim sLine As String
    Dim i As Integer
    
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    If Range("A" & LastRow).Value = "" Then LastRow = 0 'column A is empty
    
    sFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    If sFile <> "False" Then
        fileNum = FreeFile
        Open sFile For Input As #fileNum
        For i = 1 To 5
            Line Input #fileNum, sLine
            Range("A" & LastRow + i).Value = sLine
        Next
        Close #fileNum
    End If

End Sub
However, you need to be more specific. Say precisely what you want. Which column or row should the items from the text file be written to? The above code writes them to column A on the active sheet, starting at the first empty row. What format are the items in the text file? Are they on the same line, separated by commas or tabs or spaces etc., or on separate lines (records). The above code assumes the latter. Without specific knowledge of the input data and where you want it written to on the sheet it's difficult to help you.
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,462
Members
448,965
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top