yeahumok
yeahumok

Reputation: 2952

Checking if cell is populated

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?

Upvotes: 0

Views: 566

Answers (1)

devuxer
devuxer

Reputation: 42354

First of all, a few general tips:

  1. When writing macros, you can work with Ranges without selecting them.
  2. 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.

  3. 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

Upvotes: 3

Related Questions