Checking if cell is populated - excel

I have an HTML report that can be opened in Excel. I have written macros to format this report so it is sortable and can have filters applied.
I am trying to grab text between slashes. The report size, how many rows, is unknown.
Column A is blank. Column B will contain the full path name. IF column B is populated, column C should have the text between the slashes.
For example, column B contains R:\testdocuments\test.html. I want column C to contain testdocuments.
Code I have for column C:
Range("C9").Select
ActiveCell.FormulaR1C1 = _
"=MID(RC[-1], FIND(""\"",RC[-1])+1, FIND(""\"",RC[-1], FIND(""\"",RC[-1])+1)-FIND(""\"",RC[-1])-1)"
Range("C9").Select
Selection.AutoFill Destination:=Range("C9:C65000"), Type:=xlFillDefault
Range("C9:C20000").Select
This is not effective because it takes forever to sort/filter anything.
How can I check if column B is populated and if it is, to populate C with the text between slashes for the ability to filter by directory?

First of all, a few general tips:
When writing macros, you can work with Ranges without selecting them.
Use the following code at the beginning of your macro to prevent the application from animating (which slows it down): Application.ScreenUpdating = False
Edit: Make sure and set it back to 'True' after you're done.
Instead of Range("C9:C65000"), you can actually let Excel find the last row for you using Range("A1").SpecialCells(xlCellTypeLastCell).Row.
So, the macro might look something like this:
Sub FillFormulas()
Dim row As Integer
Application.ScreenUpdating = False
row = Range("B9").SpecialCells(xlCellTypeLastCell).row
Range("C9").Formula = "=MID(B9, FIND(""\"",B9)+1, FIND(""\"",B9, FIND(""\"",B9)+1)-FIND(""\"",B9)-1)"
Range(Range("C9"), Range("C" & row)).FillDown
End Sub

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 find a cell with any value and then find the next value that is different in VBA

I have a list of data with various different columns.
What I am trying to achieve is for excel to look through one of the columns and find the first value that occurs, copy that cell and then paste it in another cell. The data starts in row three (Rows 1 and 2 are headers). Let's say I want to paste the first value in G3.
Then, I need excel to find the next value that is different from the first in the same column and perform the same action as before: copy the value, and paste it in the next row in column G.
I have tried coding this but I'm not getting anywhere with it, and I haven't found a way to find a cell and then the next cell if the value within the cell is not defined (as the .Find function requires a value to search for). I know how to code the copy/paste functions but I cannot figure out how to get it to find the cell in the first place.
Any help would be much appreciated. Many thanks in advance.
In this sample, we are looking for values in column A:
Sub FillG()
Dim i As Long
i = Rows.Count
Range("A3:A" & i).Copy Range("G3:G" & i)
ActiveSheet.Range("G3:G" & i).RemoveDuplicates Columns:=1, Header:=xlNo
If Range("G3").Value = "" Then Range("G3").Delete Shift:=xlUp
End Sub
The method is to copy all of column A to G, then remove duplicates, then remove an empty in G3 if it exists.:

How to delete and add specific columns in multiple excel files

I have a file with 5 columns :
A || B || C || D || E
and I want to delete the E column and add 2 new ones with the same content in the cells. I tried to find a way in excel but it is impossible.
I used MACROS record in excel as people have suggested me to use the MACROS in my original query. But the problem is that the excel files I have, have different row numbers. So, in file one I need to add a new column with 10 rows (same text inside) and in another I need to add 2 rows.
How can I program that in the MACROS? More specifically, I want to convert this:
into this:
I only need the columns shown in picture 2. I no longer need to delete the rest. I use the files to import them on openCRM.
in a excel macro
sub ColReplaceInCurrentSheet ()
dim LastCellRow as integer
'uncomment for removing warning message
'Application.CutCopyMode = False
Columns("E:E").Delete Shift:=xlToLeft
' Define last row of data
Range("A65534").Select
Selection.End(xlUp).Select
LastCellRow = Selection.Row
for each ThisCell in Range ( "E1:F" & LastCellRow)
ThisCell.value='The Value I want here'
next
end sub
Assuming number of row is equal to the last (lower) filled cell in column A (arbitrary limited at 65534 here)
You could put any value that you want in the cell, i set it to an abritrary string here
macro is to put in a extra excel file so you can call it (file need to be open in excel) from any other excel
macro work on current active sheet.

Hide columns using VBA in Excel 2010

Based on the value stored in a specific cell such as A1 of a worksheet I would like to hide a number of columns in the worksheet starting with column B.
Examples of what I am trying to do:
If the value of cell A1 = 10, then hide column B plus 10 columns after B
If the value of cell A2 = 11, then hide column B plus 11 columns after B
The difficulty is actually the way Excel (or least my Excel files) uses the alphabet (A, B, ...) for the name of the columns. I have done this on rows before using code like rows("2:" & range("A1").value) and set .hide = true
I wanted to add a comment to Glenn's answer above but don't have enough reputation. What I was going to add was that you don't need to activate a sheet or select the columns, you can simply go ahead and hide the columns:
Worksheets("TheSheet").Columns(2).Resize(, numColumnsToHide).EntireColumn.Hidden = True
You can reference columns by their index number as such: Columns(indexnumber) and you can use Resize() to set the number of columns you want to select like so:
Sub HideColumns()
Dim numColumnsToHide
numColumnsToHide = Cells(1, 1).Value
Columns(2).Resize(, numColumnsToHide).Select
Selection.EntireColumn.Hidden = True
End Sub
Obviously, this code doesn't have any validation of value in A1 so if someone runs HideColumns() without an integer in A1, bad things are going to happen. This also doesn't unhide any hidden columns.
Please to not complicate just do it
Sub ocultaColunas()
Range("D:E").EntireColumn.Hidden = True
End Sub

How to view text of merged cells when filtering another cell?

The two columns look like on this image.
When I want to show only the cells which contain a letter 'b', I can no longer see the text "Title1" and "Title2" which is normally visible in the column B.
I guess although the cells in column B are merged, the text is still bound to A3, respectively to A7.
So how can I at the same time filter the visible content and preserve the merged text? In simple words, I want to filter content by letter 'b' and I still want to see the text "title 1/2" in the column B.
You tagged excel so here is a solution in excel:
You need to click on that column with the merged cells and unmerge all cells.
Then you need to put this formula at the top of your list and enter it with ctrl+shift+enter(this will enter it as an array formula):
=OFFSET(C3,MAX(IF(NOT(ISBLANK(C$3:C3)),ROW(C$3:C3),0))-ROW(C3),0)
Then you need to autofill that down.(this function seems a little verbose but I just got it online - there is probably a simpler way to do this - but it finds the last nonblank cell in a range).
I think openoffice has similar functions so you should be able do the same or something similar in openoffice.
Alternatively if you are using excel you could click on the column you want to unmerge and run this macro:
Sub UnMergeSelectedColumn()
Dim C As Range, CC As Range
Dim MA As Range, RepeatVal As Variant
For Each C In Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))
If C.MergeCells = True Then
Set MA = C.MergeArea
If RepeatVal = "" Then RepeatVal = C.Value
MA.MergeCells = False
For Each CC In MA
CC.Value = RepeatVal
Next
End If
RepeatVal = ""
Next
End Sub
Good Luck.
EDIT:
I found a Non-VBA solution that will work in both excel and openoffice and doesn't require you to enter it as an array formula(with ctrl+shift+enter):
=INDEX(B:B,ROUND(SUMPRODUCT(MAX((B$1:B1<>"")*(ROW(B$1:B1)))),0),1)
In open office I think you want to enter it like this:
=INDEX(B:B;ROUND(SUMPRODUCT(MAX((B$1:B2<>"")*(ROW(B$1:B2)))),0),1)
or maybe like this:
=INDEX(B:B;ROUND(SUMPRODUCT(MAX((B$1:B2<>"")*(ROW(B$1:B2)))),0))
You just need to autofill that formula down:
Your main problem seems to be the one "blank row" that you have left after the filter fields.
Remove it, and it will work fine.

Resources