Ask Experts Questions for FREE Help !
Ask
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #1

    Jun 10, 2011, 12:11 PM
    VBA If Statement
    Ok. Here's my problem. I have a spreadsheet that I use which filters on 6 different filter criteria and copies the result set of each filter criteria to a separate worksheet. However, on occasion there are no results found for a given criteria.

    What I want to do is when no results are found for a given criteria to return the words "No Data Found" to a cell on a different spreadsheet.

    I'm trying to find the correct syntax for:

    If SpecialCells = "" then

    Sheet("Smith").Select
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "No Data"

    I do know that SpecialCells represents Filtered Records but that's about all I know what to do with that.

    Any ideas?

    Thanks guys.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #2

    Jun 13, 2011, 01:02 AM

    Now Jakester, I'm sure I've shown you many times in the past how to take out the "selecting" and "Activating" in your macros. Tsk, tsk.

    When I apply Autofilters, after turning on the filter, I test to see if any rows are showing, and only copy if there is data showing past the row1 titles.

    Code:
    Dim LR As Long
    
    'Code here to turn on your filter
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    If LR > 1 Then
       'Code here to copy
    Else
       'Code here to write a message instead of copying
    End If
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #3

    Jun 13, 2011, 02:56 PM
    Quote Originally Posted by JBeaucaire View Post
    Now Jakester, I'm sure I've shown you many times in the past how to take out the "selecting" and "Activating" in your macros. Tsk, tsk.

    When I apply Autofilters, after turning on the filter, I test to see if any rows are showing, and only copy if there is data showing past the row1 titles.

    Code:
    Dim LR As Long
    
    'Code here to turn on your filter
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    If LR > 1 Then
       'Code here to copy
    Else
       'Code here to write a message instead of copying
    End If
    JB - I don't recall the tip on taking out Selecting and Activating... I know I would've remembered because I'm still using the darn things. So how can you take those out and still refer to them?

    Thanks, again.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #4

    Jun 14, 2011, 11:59 PM

    By fully addressing your commands with parent/child relationship, you can issue commands to any sheet, any cell/range and insert values/formulas without having once activated or selected anything, much less even brought the sheet up onscreen.

    VBA is not human, it has no need to physically touch a cell prior to using it.

    Your code, probably recorded, so the recorder shows your human actions:
    Code:
    Sheet("Smith").Select
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "No Data"

    The same code merged down into a fully addressed single command:
    Code:
    Sheet("Smith").Range("C2") = "No Data"

    Notice, I even took out the erroneous FormulaR1C1 since you were inserting a text string, not a formula?
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #5

    Jun 15, 2011, 12:01 AM
    If you'd like to post your workbook and the rest of your macro, I'm sure we'll find a LOT of instances to improve efficiency and shorten code at the same time.

    I use the macro recorder every day, so I'm not scolding. The code recorded is a starting point only, we have to tweak it down into proper VBA syntax.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search


Check out some similar questions!

Access VBA IF Statement Multiple Criteria [ 1 Answers ]

I am attempting to create an if statement with multiple variables. I have a form that user will fill out but I do not want them to be able to save any information if any number of the three controls are left blank. Private Sub cmdSave_Click() On Error GoTo HandleError If me.txtdate.value <>...

Can VBA do this for me? [ 7 Answers ]

Refer attachment; For sheet "SPPA" cell "D18" value to be recorded on sheet "HO plan" cell "T7", and this is where it gets nasty, as the date rolls over on the "SPPA" sheet cell "F1", so must the macro record the new value in the next cell for sheet "HO plan", ie: If "SPPA" sheet cell "F1"...

Can this be done using VBA? [ 5 Answers ]

Ok - I have a spreadsheet that is exported from an application I use at work. It formats the data in a manner that makes it really difficult to manipulate but I think I've worked out a solution that gets me what I need; only it is very tedious. The data starts in row 16 of the sheet. There is...

Excel vba [ 1 Answers ]

I am working on an excel sheet vba, I wanted to un hide the cells with a click on one cell above it without using the command button


View more questions Search