Help! Importing CSV file into Excel using VBA but I do not want the first 10 rows imported

londa_vba

Board Regular
Joined
May 11, 2023
Messages
61
Office Version
  1. 365
Platform
  1. Windows
Hello
first of all, I am a scientist not a coder. I am working with pre-existing code that works. I just want to see if it is possible to exclude the first 10 rows from importing into the excel sheet.
Here is the working code:


Sub Import()

On Error GoTo eMessage

'set the directory for the exported results folder
If Len(Dir("C:\")) Then
ChDrive "C"
ChDir "C:\LabSave\"

'run the import code (ImportTextFile)
ImportTextFile Fname:=Application.GetOpenFilename(FileFilter:="CSV (Comma Delimited) (*.csv),*.csv"), Sep:=","
If Fname = False Then
Application.ScreenUpdating = True
Exit Sub
End If
Else
retVal = MsgBox("There was an error while connecting to the C: drive. " & vbNewLine & vbNewLine _
& " Select Yes to attempt to find the LabSave folder manually." & vbNewLine & vbNewLine _
& " Select No to cancel the import and verify your network connection.", vbYesNo, "C: drive connection error")
If retVal = vbYes Then
'run the import code (ImportTextFile)
ImportTextFile Fname:=Application.GetOpenFilename(FileFilter:="CSV (Comma delimited) (*.csv),*.csv"), Sep:=","
If Fname = False Then
Application.ScreenUpdating = True
Exit Sub
End If
ElseIf retVal = vbNo Then
Exit Sub
End If
End If
Exit Sub

eMessage:
MsgBox "There was an error while connecting to the C: drive. " & vbNewLine & vbNewLine _
& " Please verify network connection to the C: drive.", vbCritical, "C: drive connection error"
Exit Sub

End Sub

Public Sub ImportTextFile(Fname As String, Sep As String)

Application.ScreenUpdating = False
Application.Cursor = xlWait
Application.DisplayStatusBar = True
Application.StatusBar = "Importing the PLIMS worksheet... "

Dim RowNdx As Long
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer

On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row

Open Fname For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend

EndMacro:
On Error GoTo 0

Application.Cursor = xlDefault
Application.StatusBar = False
Application.ScreenUpdating = True

Close #1

Any help is definitely appreciated!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Insert the code below between these 2 lines:
VBA Code:
Open Fname For Input Access Read As #1

While Not EOF(1)

VBA Code:
    Dim i As Long
    i = 0
    While Not EOF(1) And i < 10
        Line Input #1, WholeLine
        i = i + 1
    Wend
 
Upvote 1
Solution

Forum statistics

Threads
1,223,865
Messages
6,175,058
Members
452,610
Latest member
Sherijoe

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