View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
CB CB is offline
external usenet poster
 
Posts: 97
Default Trying to determine if macro is possible

OK, found your first reply...

You asked if my input (guessing input, not master) file would always have
data in a given column - unfortunately no.

Basically, there's a title row, blank row, another title, another blank row,
etc., then data. Now, once you hit that data, it's solid data until the
bottom.

I just got rid of the title junk (seems to be a consistent 7 rows) and just
worked with the data.

Sorry for asking such a basic question, but looking at your macro(?), how do
I enter this? Do I just somehow add it in the VB window? And what do I need
to update / change to make it work?

You have:
'Can edit 'ThisWorkbook.Path' in next line to
'actual text file path between double quotes
strPath = ThisWorkbook.Path & "\"

So I just update it to point to where the file is located, such as:
strPath = ThisWorkbook.Path & "C:\Temp\"

thanks,
Chris

"OssieMac" wrote:

Hi Chris,

Ensure that you have a backup of your workbook before running the following
macro in case it does not do exactly what you expect.

Note the comments in the code where you might have to edit the code to
replace the master worksheet name, path of the csv file and csv file name. If
the csv file and master file are in the same folder then there is no need to
replace the csv file path.

Some people would use the last row of the used range to find the last row of
data on the master worksheet. Not terribly reliable under some circumstances
so I have used 'find the last cell with data'. Copying the used range from
the csv file is not a problem under the circumstances that I have used it.

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

'Can edit 'ThisWorkbook.Path' in next line to
'actual text file path between double quotes
strPath = ThisWorkbook.Path & "\"

'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
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

'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.
ActiveWorkbook.ActiveSheet.UsedRange.Copy _
wsMaster.Cells(lngLastRow + 1, "A")

'Close the csv file
Windows(strTxtFile).Close

End Sub


--
Regards,

OssieMac