VBS Help for Removing Rows

Eko

New Member
Joined
Jan 10, 2012
Messages
2
I am using Excel 2007 and with a .vbs file and I am trying to delete the first 11 rows of data in a CSV, then delete the very last row after the first 11 are deleted. After this the file should be saved in XLXS format which currently does work.

Code:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = false
objExcel.displayalerts=false
srccsvfile = objFile
Set objWorkbook = objExcel.Workbooks.open(srccsvfile)
Set objWorksheet1 = objWorkbook.Worksheets(1)

'Need formatting code here

tgtxlsfile = "c:\file.xlsx"
objWorksheet1.SaveAs tgtxlsfile, 51
objExcel.Quit()
Set objWorksheet1 = Nothing
Set objWorkbook = Nothing
Set ObjExcel = Nothing
I have tried the methods below to remove the
first 11 rows but then I do not know how to then select the last used row and then delete that.

I know that if I used
Code:
For x = 1 to 11
    Set objRange = objExcel.Cells(i, 1).EntireRow
    objRange.Delete
Next
this should delete the first 11 lines I believe but is there a better way? Thanks and I hope this is in the right
place as its my first post. Hello All!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Welcome to the Forum,

Try this out to delete Row 1 to 11 and then last row with data in it.

Rows("1:11").Delete Shift:=xlUp
Range("A65000").Select
Selection.End(xlUp).Select
ActiveCell.Offset(-1).EntireRow.Delete
 
Upvote 0
I imported what you wrote into my code as below:

Code:
Set objExcel = CreateObject("Excel.Application")
            objExcel.Visible = false
            objExcel.displayalerts=false
            srccsvfile = objFile
            Set objWorkbook = objExcel.Workbooks.open(srccsvfile)
            Set objWorksheet1 = objWorkbook.Worksheets(1)
            
            objExcel.Rows("1:11").Delete Shift:=xlUp
            objExcel.Range("A65000").Select
            objExcel.Selection.End(xlUp).Select
            objExcel.ActiveCell.Offset(-1).EntireRow.Delete
            
            tgtxlsfile = "c:\file.xlsx"
            objWorksheet1.SaveAs tgtxlsfile, 51
            objExcel.Quit()
            Set objWorksheet1 = Nothing
            Set objWorkbook = Nothing
            Set ObjExcel = Nothing

except after running it I get "Microsoft VBScript compilation error: Expected statement" error on the line "objExcel.Rows("1:11").Delete Shift:=xlUp". I'm not sure if I'm formatting this wrong or if I'm not addressing the Worksheet in the right way. Please let me know what you think. Thanks!
 
Upvote 0
Im not a VB Script person, but what would happen if you place the code below the line that saves your file as a xlsx file and before you quit excel, first then see if it treats it like an excel workbook.
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,930
Members
452,367
Latest member
TePunaBloke

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