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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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