Changing VBA from “Sheet1” so I can use the VBA on any sheet

BraytonM

New Member
Joined
Jul 25, 2021
Messages
24
Office Version
  1. 365
Hello all,

Recently, y’all provided me with a macro to add unique values above in non blank row. The VBA works perfectly but I would like to be able to use that VBA on any sheet.

Right now, I can only use it on “Sheet1.” Can somebody change the VBA so that I can run it on whatever sheet I am on?

The code is below. Thank you.

VBA Code:
Sub DeleteAndAdd()
Dim r As Long
Dim lastrow As Long
lastrow = Cells(Rows.Count, "C").End(xlUp).Row
For r = lastrow To 1 Step -1
If Cells(r, "C") = 0 Then
Rows(r).Delete
End If
Next r

lastrow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = lastrow To 2 Step by - 1

a = Worksheets("Sheet1").Cells(i, 3).Value
For j = 1 To a

Worksheets("Sheet1").Rows(i).Select
Selection.Insert Shift:=xlDown

Next

Next

Worksheets("Sheet1").Cells(1, 1).Select

End Sub
 
Last edited by a moderator:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Replace Worksheets("Sheet1") with activesheet if you just want it to work on whatever sheet is active.
 
Upvote 0
Solution

Forum statistics

Threads
1,213,536
Messages
6,114,207
Members
448,554
Latest member
Gleisner2

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