VBA- Delete entire row in sheet 1 - excel

I have been using the following to delete a row.
Rows([2]).EntireRow.Delete
But this was when my workbook had only one sheet. A command button has been added in sheet2 which calls the macro present in sheet1.
How should I modify my above code so that the VBA deletes the second row of sheet1 ?

You only need to specify which which sheet the row is in, in addition to specifying the row as you already do. I.e.
Worksheets("Sheet1").Rows(2).EntireRow.Delete
Should do what you wish. If you have called the worksheet something different than "Sheet1", change what is inside the quotation marks. Strictly speaking you don't need to specify that it is the entire row you want to delete, as you've already said it is the row you are dealing with with Rows(2), but it shouldn't do any harm in there either.
If you additionally want to do the deletion from a different workbook (or just make sure that the deletion takes place in the correct workbook), you can also specify the workbook-name:
Workbooks("Workbookname.xlsm").Worksheets("Sheet1").Rows(2).EntireRow.Delete

If your button is in any other sheet and you wants to delete a row from another sheet, you simply have to activate that sheet. For example, you are trying to delete row from Sheet1 then you can activate the sheet by this one-liner.
Sheets(0).Activate

Related

How to use same macro to read all worksheets

I have a excel workbook which already has a macro say "covert". It has two worksheets. First includes configurations (variables) required to run macro and second includes rows (data) on which macro is applied. On first sheet there's one button on-click of this rows are converted into JSON. I checked configuration of this button, 'convert' macro is assigned to it.
Now, I want to make a copy of second sheet which will have similar data and I want to use same macro to read this newly created sheet with slight change in macro.
As soon as I copy sheet with data, I can see macro is also duplicated.
To make macro to decide which sheet should be read, I've added a row in first sheet and then I am adding below code to fetch config.
Dim configSheet As Worksheet
Set configSheet = ThisWorkbook.Worksheets("Configuration")
With configSheet
VAR_SHEET = .Range("B8").value
Then selecting particular sheet using below code.
With ThisWorkbook.Worksheets(VAR_SHEET)
Now, the problem is even after making changes in macro, it is always reading first sheet instead of considering variable.
When you copy a sheet with code like that, any buttons on the sheet do not "auto-adjust" to call code in the copy: they still call the same subs as they did originally (assuming non-activeX button).
Any buttons on the sheet which call code in the sheet module will need to be re-linked to the code in the copy.

Use common dropdown across multiple worksheets to change single cell

I have a large file with a Scenario Manager, where changing a single cell on the Summary worksheet changes the visible scenario throughout the rest of the workbook. Data Tables are working a treat providing the headline values for each option.
I'd like to have a drop down on each sheet that when changed will change the same single cell on the Summary worksheet, so I don't need to go back to the Summary sheet every time I want to switch visible scenarios.
This is a simple process if I'm using macros and would be the solution I'd normally jump straight to. But this needs to be done without macros and this is where I'm now struggling.
Does anyone know if this is possible (without macros) and point me in the right direction?
Josh
You can insert combo box (Developer Tab > Insert > Form Controls > Combo Box) on each sheet. Mention linked cell as a cell of the summary sheet (Absolute reference with sheet name). That cell will give you index of the item selected in the drop down list. Then you can insert index formula in the cell you want to change every time to get value of the drop down list. Once you insert it on one sheet you can copy it to other sheets. No macros required.

Is there a way to copy multiple user selected cells into another worksheet all at the same time?

My main problem is copying a certain range of user selected data into another worksheet.
I tried using selection.copy but i think it is better to avoid using that function.
Is there a way that the user can select a single cell; copy the value of the cell including the values of the next three cells to its right, and paste it into a different worksheet?
To copy/paste everything (values, formatting, etc), use:
ActiveCell.Resize(,3).Copy Destination:=Worksheets("myTargetWorksheetName").Range("A1")
Just change “myTargetWorksheetName” to your actual destination sheet name and “A1” to your actual destination sheet landing cell
To copy/paste values only:
Worksheets("myTargetWorksheetName").Range("A1:C1").Value = ActiveCell.Resize(,3).Value

Excel Protecting Rows and Columns but Allowing Cell Editing

I need to make a template where the rows and columns cannot be deleted but the cells can still be edited
Select all the cells in the sheet and unlock them
Take a cell that doesn't need to be edited (there must be at least one somewhere on the sheet) and lock it.
Activate sheet protection
You're done

Copy excel (2007) sheet to new workbook without formulas referencing original workbook

I have a workbook with two sheets. The first is full of data and calculations, the second is mostly cells with references to the first sheet. The second sheet also concatenates strings, and references to cells in the sheet, to form SQL commands used elsewhere.
There is also a second workbook (soon to be more). It has a sheet identical to the first sheet of the other workbook, except with different data. The problem I'm having is that the new workbook needs a sheet similar to the second of the original workbook (sorry if this is sounding confusing). I would like to simply duplicate the sheet and its formulas, which I tried using the 'move or copy...' option. Unfortunately, the formulas in the cells reference the first sheet from the old workbook, like this: =[foobar.xlsx]data!A1. Way too much data to remove them by hand. I can't just redo the formulas because I had to remove a lot of specific lines from the second sheet, so dragging the formula would not match up correctly. I'm currently trying to hack this together with REPLACE but if anyone can offer help it would be greatly appreciated.
CLARIFICATION:
When I copy the sheet, a formula will appear as =[foobar.xlsx]data!A1. I want it to just be data!A1.
Thanks :)
I hope this answers your problem, but I am a little unclear on your need!!!
Highlight all cells in the worksheet.
Perform a replace to replace = with say '=
This stops the formulas "being formulas"
Copy the sheet.
Perform another replace on the new sheet to replace '= with =
This converts back to formulas, referring to cells in your new workbook.

Resources