How to use same macro to read all worksheets - excel

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.

Related

Column header validation with different sheets using macros

In Excel, I have 4 column headers (Id, Name, Age, Place).
I have 3 Excel worksheets with same column headers and different data.
I want to make a consolidated worksheet and the constraint is I have to check whether the column header is the same in all 3 worksheets and move it into the consolidated worksheet.
In the 1st worksheet I have values in the first row.
In 2nd worksheet, the first row is empty and the second row has data.
In 3rd worksheet, the first and second rows are empty and the 3rd row has data.
The 4th worksheet is the consolidated one. I want all 3 worksheet's data in this sheet.
How to do it with macros in vba?
There's built-in functionality for data consolidation.
You'll need to move your data so it's all on the same row but in the end will probably be much easier than writing code from scratch.
How to consolidate
Follow these steps to consolidate several worksheets into a master worksheet:
If you haven't already, set up the data in each constituent sheet by doing the following:
Ensure that each range of data is in list format. Each column must have a label (header) in the first row and contain similar data. There must be no blank rows or columns anywhere in the list.
Put each range on a separate worksheet, but don't enter anything in the master worksheet where you plan to consolidate the data. Excel will do this for you.
Ensure that each range has the same layout.
In the master worksheet, click the upper-left cell of the area where you want the consolidated data to appear.
Note: To avoid overwriting existing data in the master worksheet, ensure that you leave enough cells to the right and below this cell for the consolidated data.
Click Data → Consolidate (in the Data Tools group).
In the Function box, click the summary function that you want Excel to use to consolidate the data. The default function is SUM.
Here is an example in which three worksheet ranges have been chosen:
Select your data.
More information and the remaining instructions are at the source.
If you want to automate the process, record a macro as you performs the necessary steps and then you can edit as required.
See: Recording a Macro to Generate Code as well as Revising Recorded Visual Basic Macros. If you're not familiar with Excel (or whatever you're planning on automating with VBA), do that before attempting VBA.
This VBA tutorial is highly recommended: Excel VBA For Beginners and Microsoft's Documentation.

Having a sheet that contains buttons for macros that run off of selections

Is it possible to use Selection to define data used in a macro button that is in another sheet.
I'm trying to make a template workbook that's first sheet is a bunch of macro buttons. I want to be able to select data in another sheet, click my button sheet, and click my button to run a macro on the data I have selected in the other sheet.
The problem that I'm running into is that sheets seem to have independent selections at the same time. So my macro always runs on the selection from the button sheet instead of the sheet I was on. Any thoughts on how I can make this work? The selection of the data needs to be dynamic, so I can't just say .Cells(1,1).value because it might not be the data I need.
Say there are two types of sheets:
a set of data-sheets
a single button-sheet which controls processing via macros tied to buttons
We code a single global range variable in a standard module. We code selection change event macros in all the data-sheets. Then:
We click on a data-sheet
We select cells on the data-sheet
the event macro on that data-sheet records the selection in the global variable
We click to get on the button-sheet
We click on a button
the button macro retrieves the global range
the button macro determines the sheet associated with the stored range (from the Parent Property) and also the cells on that sheet which we selected
the button macro processes the data
EDIT#1:
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Also just Google: Excel VBA Event Macro
(there are many examples in this forum, for example)
https://msdn.microsoft.com/en-us/VBA/Excel-VBA/articles/workbook-sheetselectionchange-event-excel
In this link is where I need to start. Every time that I change my selection it runs an event where I could maybe grab the cell values and store them in an array that will stay static when I change sheets. If I get it right I'll post it here. 5/31/2018
To Accomplish this I made rng a public variable and stored my selection from sheet2 in it. Then I used a click event in my buttons to to call the macros that I wanted to run on my range. Here are some screenshots of the codes in my modules and in my sheets. This answer is for anyone that finds this in the future.
https://imgur.com/a/DJDaQiM
The Biggest thing to take away from this is that Public Variables in vba only have to be declared once in any of your modules. NOT IN YOUR SHEET CODE. This was the biggest thing that held me back from getting this done. Good luck and contact me with any questions.

VBA- Delete entire row in sheet 1

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

How to make VBA code to loop via multiple sheets and copy selected (6) cells

How to make VBA code to loop via multiple sheets and copy selected (6) cells, and pasted into summary excel sheet with heading format cells made via the VBA code, including sheets numbers, the summary sheet is created via the code ..
Surely you can use the record button to record the macro doing exactly that, after which look at code and you can neaten if needed. There should be no problem doing this at all!
Please review below link.
http://office.microsoft.com/en-gb/excel-help/record-and-use-excel-macros-HA001054837.aspx

VBA to put code programmatically in sheet-level macro

I was trying to use Workbook_SheetFollowHyperlink() to trigger a macro if the user clicks the hyperlink.
However, this Workbook_SheetFollowHyperlink macro is to be inserted on a sheet-level, not on the module level.
Is there any way that I can programmatically add this Workbook_SheetFollowHyperlink macro to every sheet? The reason is, I create these sheets on the fly using VBA, and the number of sheets and their names are not known in advance beforehand.
Workbook_SheetFollowHyperlink is defined in the ThisWorkbook class module. (At Worksheet level, it's Worksheet_FollowHyperlink)
So, you already have what you need: an Event that responds to following a Hyperlink on any sheet in the workbook.

Resources