I have an excel sheet that has 2 command buttons. 1 for input (ascii file) and 1 for export (back to ascii wtih updated data).
When I run the export, the last step seems to want to rename the spreadsheet and I lose my macros but I do not understand why. Here is the code I used.
ThisWorkbook.Sheets.Add after:=Sheet1
lrow = Sheet1.UsedRange.Rows.Count
Set r1 = Sheet1.Range(Cells(2, 1), Cells(lrow, 3))
Set r1 = Application.Union(r1, Range(Cells(2, 7), Cells(lrow, 9)))
Set r2 = Sheet1.Range(Cells(2, "K"), Cells(lrow, "K"))
'Concatanate the fields
Dim counter
For counter = 2 To lrow
Cells(counter, "K").Value = _
Cells(counter, 1).Value & Cells(counter, 2).Value & Cells(counter, 3).Value & _
Cells(counter, 7).Value & Cells(counter, 8).Value & Cells(counter, 9).Value
Next counter
'copy to new sheet
'r1.Copy (Sheets(Sheets.Count).[a1])
r2.Copy (Sheets(Sheets.Count).[a1])
'save the new sheet
Sheets(Sheets.Count).SaveAs Filename:="D:\My Data\FireHydrant Database\MainframeSide\testit.txt", FileFormat:=xlTextWindows
'delete the temporary sheet
Sheets(Sheets.Count).Delete
'close the text file
Close
'clear out the cells so it will be empty at the start
Sheet1.Range(Cells(1, "K"), Cells(lrow, "K")).ClearContents
any suggestions would be greatly appreciated.
lhuffst
When I run the export, the last step seems to want to rename the spreadsheet and I lose my macros but I do not understand why. Here is the code I used.
ThisWorkbook.Sheets.Add after:=Sheet1
lrow = Sheet1.UsedRange.Rows.Count
Set r1 = Sheet1.Range(Cells(2, 1), Cells(lrow, 3))
Set r1 = Application.Union(r1, Range(Cells(2, 7), Cells(lrow, 9)))
Set r2 = Sheet1.Range(Cells(2, "K"), Cells(lrow, "K"))
'Concatanate the fields
Dim counter
For counter = 2 To lrow
Cells(counter, "K").Value = _
Cells(counter, 1).Value & Cells(counter, 2).Value & Cells(counter, 3).Value & _
Cells(counter, 7).Value & Cells(counter, 8).Value & Cells(counter, 9).Value
Next counter
'copy to new sheet
'r1.Copy (Sheets(Sheets.Count).[a1])
r2.Copy (Sheets(Sheets.Count).[a1])
'save the new sheet
Sheets(Sheets.Count).SaveAs Filename:="D:\My Data\FireHydrant Database\MainframeSide\testit.txt", FileFormat:=xlTextWindows
'delete the temporary sheet
Sheets(Sheets.Count).Delete
'close the text file
Close
'clear out the cells so it will be empty at the start
Sheet1.Range(Cells(1, "K"), Cells(lrow, "K")).ClearContents
any suggestions would be greatly appreciated.
lhuffst