Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Rows in Another Workbook

Tags:

excel

vba

Objective: Write VBA script in one Excel workbook (Default.xls) to open another Excel workbook with a variable name (NFL_*). Then the VBA script in Default will delete several superfluous and merged-cell-type rows (1:12) in NFL. Then it will count the number of populated records in NFL (i.e., number of rows) and delete the last row (because it is not really a record, it is just a cell that sums up one of the columns).

Problem: I can't get the latter part to work - the part about deleting the last "row." I can get it to work if the script is written in the NFL workbook itself, but I can't get it to work with the script written in the Default workbook. Though - I can get the VBA script in the Default workbook to delete rows 1:12 in the NFL workbook. Why won't it work for the latter part?

Problem Restated: The Default workbook has 1 VBA script that performs 2 different actions on another workbook. 1 action - deleting rows 1:12 - works. The second action - deleting the last "row" - does not work.

Thank you in advance for any help!

Sub Chop_FR_NFL()

'Set parameters
Dim wb As Excel.Workbook
Dim xlApp As Excel.Application

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True

'Open latest NFL report and delete rows 1:12

Set wb = xlApp.Workbooks.Open("T:\xx\xx\xx\xx\xx\xx\xx\" & "NFL_*", True, False)

wb.Sheets(1).Rows("1:12").Delete

'Delete Total UPB Grand Total cell

Dim UPB As Long
UPB = Range("I" & Rows.Count).End(xlUp).Row
Range("I" & UPB).Select
Selection.Delete

wb.Save
wb.Close

End Sub
like image 267
Chase Avatar asked May 01 '15 19:05

Chase


People also ask

How do I delete rows from different sheets in Excel?

1. Right click on any sheet tab in current workbook, then click Select All Sheets from the right-clicking menu. 2. Now all worksheets are selected, if you delete certain rows or ranges in a worksheet, the same rows or ranges in other worksheets are deleted together.

Can we delete rows in shared workbook?

You can't delete sheets from a shared workbook. You can access the following features only if you stop sharing the workbook. You cannot use shared workbooks (shared workbook: A workbook set up to allow multiple users on a network to view and make changes at the same time.

Can you create a macro to delete rows in Excel?

By using the power of Excel Macros, you can automate the process so that Excel will eliminate any and all flagged rows instantaneously. Let's take a real-world example.


1 Answers

This should work:

'...
With wb.Sheets(1)
    .Rows("1:12").Delete
    .Cells(.Rows.Count, "I").End(xlUp).EntireRow.Delete
End With 
'...
like image 191
Tim Williams Avatar answered Oct 17 '22 08:10

Tim Williams