Code deletes old data if I run F8 but not when I press button

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello guys,
Weird it seems but this is happening. When I generate xml the first time, it works perfectly. But the next time when the rows are less than the first time, the code doesn't erase the old data in the Master data sheet. This happens when I run the code with the button.
I ran the code with F8 to check which line needs to be edited. But mysteriously, the code worked perfectly.
First press Generate Master XML and run the code, check the masterdata sheet. Now delete the rows from 11 to end in the paste data sheet and run again. You will see that the master data sheet data is not fully deleted after row 24.
once again using F8 if you run the generate master xml code it will be fully deleted.
I hope I have made it clear what the problem is.
F8 working button not working.xlsm
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I have found the problem but I do not know how to edit the code.
Rich (BB code):
With Sheets("MasterData")
        .Range("B2:E2", .Range("B2:E2").End(xlDown)).ClearContents
        .Range("F2:I" & Cells(Rows.Count, 6).End(xlUp).Row).ClearContents
    End With
What is & Cells(Rows.Count, 6).End(xlUp).Row)...??
 
Last edited:
Upvote 0
Whose code is that in that section? It's not mine, I can tell, by the way it is written, & it wasn't commented. :)
 
Upvote 0
Rich (BB code):
.Range("B2:E2", .Range("B2:E2").End(xlDown)).ClearContents
I changed the above range to B2:I2 and commented the second line, but still it is not working. XlDown is working only till the first empty row and not after that.
Whose code is that in that section? It's not mine, I can tell, by the way it is written, & it wasn't commented. :)
You had edited some part of the code which you had not written.
 
Upvote 0
There is an empty column in the master data sheet column A. With the help of a code, If you fill that column with numbers against the particulars column then hopefully this will work after editing this line from
Rich (BB code):
.Range("B2:E2", .Range("B2:E2").End(xlDown)).ClearContents
to
Rich (BB code):
.Range("A2:I2", .Range("A2:I2").End(xlDown)).ClearContents
 
Upvote 0
To clear the 'Master Data' sheet data, replace:

VBA Code:
    With Sheets("MasterData")
        .Range("B2:E2", .Range("B2:E2").End(xlDown)).ClearContents
        .Range("F2:I" & Cells(Rows.Count, 6).End(xlUp).Row).ClearContents
    End With

with:

VBA Code:
    Sheets("MasterData").Range("B2:I" & Range("B" & Rows.Count).End(xlUp).Row).ClearContents            ' Erase all but the header row on MasterData sheet
 
Upvote 0
It worked. But I still don't understand how it counted the empty row in Column B. Alittle explanation will help me to understand.
 
Upvote 0
Yes, doesn't actually select it, but it looks at that column.
 
Upvote 0
Just a second, Checking the original data for the 4th time. Seems it is not working here. Will let you know in sometime.😛
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,080
Members
453,021
Latest member
Justyna P

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