View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Trying to determine if macro is possible

Hi Chris,

Been away for a couple of days so late getting back to you. Have amended my
previous code to disregard the first 7 lines. Note the comments in the code
because you can adjust the number of lines at the top to ignore by changing
just one line of code with the number.

Also note the comments on how to insert the actual path in lieu of
ThisWorkbook.Path

Sub AppendData()
Dim wbMaster As Workbook
Dim wsMaster As Worksheet
Dim lngLastRow As Long
Dim strPath As String
Dim strTxtFile As String
Dim rngToCopy As Range
Dim lngRows As Long

'Edit 7 in the following line to number
'of rows at top of text file to ignore
lngRows = 7

strPath = ThisWorkbook.Path & "\"

'Can use following method in lieu of previous
'line of code. (Delete above line and remove
'the single quote (comment symbol) in next line
'strPath = "C:\Temp\"

'Edit "Import Test.csv" to your csv file name
strTxtFile = "Import Test.csv"

Set wbMaster = ThisWorkbook

'Edit "Sheet1" in next line to your
'Master worksheet name
Set wsMaster = wbMaster.Sheets("Sheet1")

'Find last row of existing data in master sheet
'find first blank row below existing data)
With wsMaster
lngLastRow = .Cells.Find(What:="*", _
After:=.Cells(.Rows.Count, .Columns.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row + 1
End With

'Open csv file
Workbooks.Open Filename:=strPath & strTxtFile

'Set the range to copy from the .csv file.
'.Offset(lngRows, 0) moves down to start copy range at row 8.
'However, it now includes additional 7 blank rows at bottom.
'.Resize(.Rows.Count - lngRows, .Columns.Count) reduces
'the size by 7 rows to remove additional blank rows at bottom.

With ActiveSheet.UsedRange
Set rngToCopy = .Offset(lngRows, 0) _
.Resize(.Rows.Count - lngRows, .Columns.Count) _
.EntireRow
End With

'Copy the data from csv file and append to
'bottom of existing data in Master worksheet
'Note: the + 1 leaves one blank row between
'existing data and new appended data.
'Delete + 1 for no blank rows between existing
'data and new data.

rngToCopy.Copy _
wsMaster.Cells(lngLastRow + 1, "A")

'Close the csv file
Windows(strTxtFile).Close

End Sub

--
Regards,

OssieMac