if statment based on cell format

bujaman

Board Regular
Joined
Apr 2, 2009
Messages
56
I am trying to select the cells that meet a couple of criteria, the value has to be greater than 0 and formatted as currency. I have tried the following with no success. Any suggestions?

Code:
If cell.Value > 0 And Selection.NumberFormat = "$#,##0.00" Then 
'do stuff
End If
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Maybe ...
Code:
If VarType(cell.Value) = vbDouble And InStr(cell.Text, "$") > 0 Then
 
Upvote 0
Perhaps:
Code:
If IsNumeric(cell) And cell > 0 And cell.Text = Format(cell, "Currency") Then
    MsgBox "All Criteria Met"
End If
 
Upvote 0
Thank you for the quick response, Hotpepper, you solution is working. Now I have another question with this if statement. The IF is inside a For Each statement that goes through each cell in a defined range. I need to activate each cell as I go through and do various thing based on each cell value. How do I activate the cell that the IF statement is examining?
 
Upvote 0
You don't need to activate or select cells in VBA to work with them, you can work with cells directly. What are you trying to do?
 
Upvote 0
I am trying to sort data from one worksheet to another. My source worksheet has pricing data broken into months (columns in the allData range) and various activities (Rows). I am trying to search through the allData range and if there is a price listed, copy various information from the source worksheet to the summarysheet, and arrange it into columns so that each cost has its own row. Here is what I have so far for code:

Code:
Sub sendtosummary()

Dim allData As Range
Dim NewLastRow As Long
Dim summarysheet As Worksheet
Dim actcell As Range

Worksheets("Data").Activate

Set allData = Worksheets("Data").Range("M3:AJ6")
Set summarysheet = Worksheets("Cost Summary")
Range("M3").Activate
Application.ScreenUpdating = False

For Each cell In allData

If IsNumeric(cell) And cell > 0 And cell.Text = Format(cell, "Currency") Then
   

    'Copy hours and cost
    
    Set actcell = ActiveCell
    Worksheets("Data").Activate
    ActiveCell.Select
    Selection.Copy
    Worksheets("Cost Summary").Activate
    NewLastRow = Range("A65536").End(xlUp).Row
    summarysheet.Cells(NewLastRow + 1, 6).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    Worksheets("Data").Activate
    ActiveCell.Offset(0, 1).Select
    Selection.Copy
    Worksheets("Cost Summary").Activate
    NewLastRow = Range("A65536").End(xlUp).Row
    summarysheet.Cells(NewLastRow + 1, 7).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    'Copy Date
    Worksheets("Data").Activate
    Cells(1, ActiveCell.Column - 1).Copy
    Worksheets("Cost Summary").Activate
    NewLastRow = Range("A65536").End(xlUp).Row
    summarysheet.Cells(NewLastRow + 1, 5).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    'Copy Employeer Class and Name from Summary to Cost Summery
    Worksheets("Data").Activate
    Range("C" & ActiveCell.Row).Select
    ActiveCell.Copy
    Worksheets("Cost Summary").Activate
    NewLastRow = Range("A65536").End(xlUp).Row
    summarysheet.Cells(NewLastRow + 1, 3).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    Worksheets("Data").Activate
    Range("D" & ActiveCell.Row).Select
    ActiveCell.Copy
    Worksheets("Cost Summary").Activate
    NewLastRow = Range("A65536").End(xlUp).Row
    summarysheet.Cells(NewLastRow + 1, 4).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False


    'Copy WBS from Summary to Cost Summery
    Worksheets("Data").Activate
    Range("B" & ActiveCell.Row).Select
    ActiveCell.Copy
    Worksheets("Cost Summary").Activate
    NewLastRow = Range("A65536").End(xlUp).Row
    summarysheet.Cells(NewLastRow + 1, 1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Worksheets("Data").Activate
 
End If
Next cell

Application.ScreenUpdating = True
End Sub

I am having trouble getting it to work properly. I am sure there are better ways to do this, but I am pretty green when it comes to VBA. Thanks for your help, I really appreciate it!
 
Upvote 0
OK, I figured it out, I needed to use Cell, not ActiveCell when referencing the cell the code was examining. Thanks for the help!
 
Upvote 0
You still don't need to select the cell in order to work with it:

For example:

Instead of:
Code:
 ActiveCell.Select
    Selection.Copy
Just work with the cell directly:

Code:
ActiveCell.Copy
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top