Request for suggestion on building a macro - excel

In Excel, rows of Column A is merged and the rows of Column B are not merged, Now i want to write a macro for finding the count of rows of column B that are not blank for a corresponding value present in column A(which is merged).
Appreciate your help.

Some ideas...
'make sure to get the top-most merged cell
Set rng = ActiveSheet.Range("D3").MergeArea.Cells(1)
Set ma = rng.MergeArea
Debug.Print rng.Address(), ma.Address(), _
rng.Offset(0, 4).Resize(ma.Rows.Count, 1).Address()
Debug.Print Application.CountA(rng.Offset(0, 4) _
.Resize(ma.Rows.Count, 1))

To build a macro go to Developer > Record Macro and click OK. Press keys or buttons to suit and when the result is as desired click on Stop Recording.

Related

Ignoring specific rows with VBA Excel

I have a table in Excel like such, where the number of rows will vary each day:
Column A
Column B
Column C
Cell 1
Cell 2
Show
Cell 3
Cell 4
Show
Cell 5
Cell 6
Ignore
I am using vba to convert the range to a html table, and then email it.
I have a helper column (Column C), and I want to use a formula there to filter out certain rows.
However, that filter is not excluding hidden cells from being displayed in the html table.
I currently use this
Dim LastRow As Long LastRow = rInput.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
to find the last row of my table. This works great in projects where you want all of the table included.
I tried to change it to Find("Ignore", which gets me Object variable or With block variable not set
I tried including 'SpecialCells(xlCellTypeVisible)' in my
ConvertRangeToHTMLTable(Sheet2.Range("$A:$J").Rows("5:" & LastRow), 5)
and using a filter to hide the 'Ignore' cells. But that did not stop them showing in the emailed html table.
You probably have some sort of loop which goes over the rows right? It will not automatically skip the hidden rows just because they are filtered out, you need to specifically tell it to skip them. You can do something like:
For Each r In myRange.Rows
If Not r.EntireRow.Hidden Then
doSomething
End If
Next r
Ended up adjusting the table (and thus the range I cared about) to start at row 1 rather than row 5, and using
strBody = dsaEmailHeader & ConvertRangeToHTMLTable(Sheet2.Range("$A:$H").Rows("1:" & LastRow).SpecialCells(xlCellTypeVisible))
worked, where it didn't previously.

How to autopopulate excel column based on dropdown list selection from another column

I have a spreadsheet that has two buttons - To retrieve rows from a sql table using a macro and another one to update data changes back to the table from excel. I have also attached an image here for reference. The table columns are EmpID, EName, Grouping, CCNum,CCName, ResTypeNum, ResName and Status.
Now to update changes, I have drop down lists for CCName and ResName. I would like the ResTypeNum to change automatically whenever the value in ResName column from dropdown list changes. Using Vlookup doesn't seem to work as the formula gets wiped out every time I click on the Retrieve button to refresh data. Also, I have to drag down the formula which I don't want but instead the value in ResTypeNum should automatically update whenever the ResName column is changed. I would appreciate any new ideas to make this work.
Thank you,
Hema
Assumptions:
First value is in A4
ResName column is G
Sheet with data validation list and corresponding code in sheet "ResNameSheet"
In the tables sheet event module you place the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 And Target.Row > 3 Then
If Target.Value2 = "" Then
Target.Offset(0, -1).Value2 = ""
Exit Sub
End If
Dim rngDataValidation As Range
Dim rngRow As Range
Set rngDataValidation = ThisWorkbook.Sheets("ResNameSheet").UsedRange
For Each rngRow In rngDataValidation.Rows
If rngRow.Cells(1, 1).Value2 = Target.Value2 Then
Target.Offset(0, -1).Value2 = rngRow.Cells(1, 2).Value2
End If
Next
End If
End Sub
Explaining how the code works:
Fires on changes to the sheet
checks if changes are in column G (7) and that it occurs below the header row (3)
Checks that the change was not deleting from column G, if it is it clears all values on the corresponding column F
Loops through the Rows collection in the range with ResName list
Checks if values match
If it does it writes the corresponding code to the column to left of Target
Hope this helps.

How can I duplicate different rows in Excel?

I have single values in Ad Group column that I need to duplicate in the blank cells in the rows below. There should be 3 of each Ad Group name.
My first screenshot shows how it currently is:
My second screenshot shows how I need it:
There are 30k rows so I need a formula or dynamic way to achieve this.
Here is my answer:
Using VBA:
Sub selectBlankCells()
Dim r 'to store the number of the last row of data
Dim rng As Range
r = Range("A1").End(xlDown).Row 'to find the last row
Set rng = Range(Cells(1, 2), Cells(r, 2)) ' set the range of column B, of the data
rng.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C" ' this is where you put the formula to
'set the data of above cell in every empty cell of
'column B
End Sub
Or you can use the Excel option:
Select the the data of column B, from the last row to the cell B1, then press F5, and this window appears.
Then press Special...
After that, select Blanks and OK
You will select only the blank cells, and then you can put the formula to link the above cell from the active cell.
This way using ([Ctrl]+[Enter] is needed to put the formula into all of the cells at once as Jeeped says):
You will get this:

Is it possible to select a range of data via a macro if the start and end row is variable?

I need to add a piece of code to my macro that copies and pastes a range of data.
The problem that I am having is that the data can start and finish on any row.
Is it possible to do this?
e.g. Currently the data starts in cell C10 and ends in cell I20. But next month it could start in C5 and end in I40.
The column range will always remain the same i.e C:I, but the row will change each time.
How to a add code to search for the first instance of data in Column C and copy all the data that follows?
Thanks
Here are some code snippets that might help
Get the first cell in C
If Range("C1").Value <> "" Then
Set firstcell = Range("C1")
Else
Set firstcell = Range("C1").End(xlDown)
End If
I test C1 because it will go all the way to the bottom if that is the only cell in C with values.
After that, to get the bottom cell after that
Set lastcell = Cells(Rows.Count, "I").End(xlUp)
Another way might be to use a named range in the spreadsheet to define the range you want to copy.
=OFFSET(C1,MATCH(TRUE,INDEX(NOT(ISBLANK(C:C)),0),0)-1,0,COUNTA(C:C),7)
That formula will select any number of data rows starting anywhere in column C. It doesn't account for header rows or any other content. If your data would always start in at least, say, row 5, then you could adjust the C1 value and use C5:C1000 instead of C:C. Then, in the VBA you can just use Range("namedRange"). Copy instead of having to work out the start and end points.
If you highlight the required cells before running the macro, then you can use
Selection
to refer to the selected range
ie, instead of
Range("C10:i20").Clear
you can write
Selection.Clear

VBA - Enter a formula into all active rows of the current column (when you don't know the column letter, or if the column changes)

Hi guys this is my first post, I'm wondering if you can possibly assist me.
I'd like to write a macro / script that will allow me to put a formula into the column to the right of the currently selected one (for all active rows of the current column) based on what column I've selected. The issue I'm having is that I don't always know the current column letter (as my selection changes from worksheet to worksheet).
To give you an example:
One of my columns currently contains dates, that dates are entered in different formats though, some are separated with ".", some with "-", some with spaces and so on. I have a formula that will deal with this so I need to put this formula in the column to the right of the selected column (which has the dates).
I have been able to do this when I specify the column letter, but not if it changes.
Please can you help?
Give this a go,
Sub SomethingNeat()
Dim rng As Range, x
x = Selection.Column
On Error Resume Next
Set rng = Columns(x).SpecialCells(xlCellTypeConstants, 23)
If Not rng Is Nothing Then rng.Offset(, 1) = "'=MyFormula"
End Sub
You can use ActiveCell.Offset(0,1).Value = Variable
That means that whetever your current cell is you can move and "select" to put a value to the right cell of the one you have activated. You can move the selection using a loop.
Do
Workbooks("Yur workbook name").Worksheets(1).Range(Adress you want to start adding).Offset(0, 1).formula = "=FORMULA"
i = i + 1
ActiveCell.Offset(1, 0).Activate
Loop While i <= max_row
Edit: 2nd
Put the formula in a cell lets say C1
'Select a range
Set take = Worksheets(1).Range("C1")
take.Copy 'copy the formula
Worksheets(1).Paste Destination:=Worksheets(1).Range("B1:B10")
That will copy your function whenever you want it to

Resources