Range().Delete not working when I run macro from a different workbook

mghad

New Member
Joined
May 29, 2018
Messages
2
I have a main workbook that I open that has code in it. when I run the code on that workbook it copies some data, opens a template workbook, pastes the data into the template workbook, runs a macro that is in the template workbook, and saves the template workbook with a new name and closes it.

In the macro that is written in the template workbook I have it delete unneeded cells using the following code:

Code:
If rownum > 24 Then


    rownum = rownum + 1
    rowm = rowm - 2
    Sheet1.Range(Cells(rownum, 1), Cells(rowm, 26)).Delete Shift:=xlUp
    
ElseIf rownum < 24 Or rownum = 24 Then


    rowm = rowm - 1
    Sheet1.Range(Cells(26, 1), Cells(rowm, 26)).Delete Shift:=xlUp


End If

When I run the code directly from the template workbook it works, but when this macro is called from the main workbook the unwanted cells are not deleted. I want the cells to delete when the macro is called from my main workbook. Any help would be great, Thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
you need to reference that other workbook as well so maybe like this
Code:
Dim OtherWorkbook as Workbook

set OtherWorkbook = OpenFile("C://")
OtherWorkbook.Sheet1.Range... blah blah blah
 
Upvote 0
The template workbook is already the active workbook when the code is run to delete the cells.
 
Upvote 0

Forum statistics

Threads
1,213,526
Messages
6,114,136
Members
448,551
Latest member
Sienna de Souza

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