I want to use if-then statement across two workbooks.
I've defined x as Long but x does not appear to hold the value of the sum of all cells in column B
But the code looks right, any thoughts?
Sub mycode()
Dim x As Long
myRange = Workbooks("Book2").Sheets("Sheet1").Range("B1", _
Range("B1").End(xlDown))
x = WorksheetFunction.Sum(myRange) '<<does not seem to hold value
If Workbooks("Book1").Sheets("Sheet1").Range("A1").Value = x Then
MsgBox ("values equal")
Else
MsgBox ("please review values")
End If
End Sub
As an example:
Sub MyCode()
Dim myRange As Range
Set myRange = Workbooks("Book2").Sheets("Sheet1").Columns(2)
If Workbooks("Book1").Sheets("Sheet1").Range("A1").Value = WorksheetFunction.Sum(myRange) Then
MsgBox "Values equal"
Else
MsgBox "Please review values"
End If
End Sub
Related
I want to check if a selection from a user has a blank cell in a column of that selection. The iteration is tricky for me using a selection as opposed to a preset range.
Any help on how to set the range properly would be most appreciated.
Dim cutrng As Range
Dim c As Range, t As Range
Set cutrng = Selection.EntireRow
For Each t In cutrng.Cells
For Each c In t.Cells
If IsEmpty(Cells(c, 53).Value) = True Then
MsgBox ("You have selected lines that do not have data.")
End
Else
End If
Next c
Next t
Try the following
Set myRow = Selection.Row
If IsEmpty(Cells(myRow, 53)) = True Then
MsgBox ("You have selected lines that do not have data.")
End If
There is no need to loop if you only want to check the cell in column 53 and you only have one cell selected. If you expect the user to select multiple rows then try:
Dim myRow as Range
For each myRow in Selection.Rows
If IsEmpty(Cells(myRow, 53)) = True Then
MsgBox ("You have selected lines that do not have data.")
End If
Next myRow
Ended up figuring it out. Thanks everyone for your help.
Dim cutrng As Range
Dim g As Long
Set cutrng = Selection.EntireRow
For g = 1 To Selection.Rows.count
If IsEmpty(cutrng.Cells(g, 53)) = True Then
MsgBox ("You have selected lines that do not have data.")
End
Else
End If
Next g
I prepared the if statement for checking my cells in the specific row. I have several cells, which I have to check. Their values are mostly "x" but sometimes they vary.
The problem is, that even if one of the value is different than "x", I am still getting the msgbox, that everything is good as per the code, which I prepared.
Sub AuditCheck()
If Range("C33,C39:C40,C43,C53:C54,C57:C59,C68").Value = "x" Or Range("C33,C39:C40,C43,C53:C54,C57:C59,C68").Value = "0" Then
'Rows(39).Delete
Range("C58").Activate 'taking a look just in case
MsgBox ("All good!") ' if so we can hide X
ActiveSheet.Range("$A$5:$IF$77").AutoFilter Field:=1, Criteria1:="<>x", Criteria2:="<>0"
Else
MsgBox ("You have to change the pricing!")
If Range("C39").Value <> "x" Then 'C39 CASE
MsgBox ("Install duct Upturns/upturn section must be removed!")
Call RemoveUpturn
Call New_version_upturn
End If
Exit Sub 'the macro is terminated because you have to change the prices
End If
End Sub
Is there something, which I haven't mentioned in the code?
Try the next code, please:
Sub AuditCheck()
Dim sh As Worksheet, rng As Range, ar As Range, countX As Long, zCount As Long
Set sh = ActiveSheet
Set rng = Range("C33,C39:C40,C43,C53:C54,C57:C59,C68")
For Each ar In rng.Areas
countX = countX + WorksheetFunction.CountIf(ar, "x")
zCount = zCount + WorksheetFunction.CountIf(ar, "0")
Next
If countX = rng.cells.count Or zCount = rng.cells.count Then 'here, you maybe want adding the two counts...
Range("C58").Activate 'taking a look just in case
MsgBox ("All good!") ' if so we can hide X
ActiveSheet.Range("$A$5:$IF$77").AutoFilter field:=1, Criteria1:="<>x", Criteria2:="<>0"
Else
MsgBox ("You have to change the pricing!")
If Range("C39").Value <> "x" Then 'C39 CASE
MsgBox ("Install duct Upturns/upturn section must be removed!")
Call RemoveUpturn
Call New_version_upturn
End If
End If
End Sub
It looks strange checking of counted "x" or "0". If you wanted to count them together, you should add them an compare with total range cells count...
If counting zero does not count (anymore), you just delete the second condition.
You must use CountIf (doc here) which will counts the number of cells within a range that meets the given criteria to do that you would have done something like that :
Sub Try_Me()
Dim Myrng As Range
Dim NumCheck as Long
Dim StrCheck as String
StrCheck = "x"
NumCheck = 0
Set Myrng = Range("C33,C39:C40,C43,C53:C54,C57:C59,C68")
If WorksheetFunction.CountIf(Myrng, NumCheck ) = Myrng.Count Or WorksheetFunction.CountIf(Myrng, StrCheck ) = Myrng.Count Then
Range("C58").Activate 'taking a look just in case
MsgBox ("All good!") ' if so we can hide X
ActiveSheet.Range("$A$5:$IF$77").AutoFilter Field:=1, Criteria1:="<>x", Criteria2:="<>0"
Else
MsgBox ("You have to change the pricing!")
If Range("C39").Value <> "x" Then 'C39 CASE
MsgBox ("Install duct Upturns/upturn section must be removed!")
Call RemoveUpturn
Call New_version_upturn
End If
Exit Sub 'the macro is terminated because you have to change the prices
End If
End Sub
EDIT According to #chris neilsen you should do as below since CountIf does not work with non-contiguous range.
So I would suggest you to just count the number of X in your range if it does match with the excepted number of x or 0 the the if condition will return true :
Sub Try_Me()
Dim Myrng As Range
Dim NumCheck as Long
Dim StrCheck as String
Dim NumExceptedX as Int
Dim NumeExceptedZ
NumExceptedX = 11
NumeExceptedZ = 15
StrCheck = "x"
NumCheck = 0
Set Myrng = Range("C33:C68")
If WorksheetFunction.CountIf(Myrng, NumCheck ) = NumeExceptedZ Or WorksheetFunction.CountIf(Myrng, StrCheck ) = NumExceptedX Then
Range("C58").Activate 'taking a look just in case
MsgBox ("All good!") ' if so we can hide X
ActiveSheet.Range("$A$5:$IF$77").AutoFilter Field:=1, Criteria1:="<>x", Criteria2:="<>0"
Else
MsgBox ("You have to change the pricing!")
If Range("C39").Value <> "x" Then 'C39 CASE
MsgBox ("Install duct Upturns/upturn section must be removed!")
Call RemoveUpturn
Call New_version_upturn
End If
Exit Sub 'the macro is terminated because you have to change the prices
End If
End Sub
I want to create a macro to compare two versions of a report in order to check if historical data is changed when creating the new report.
I want to loop through the Used Range of one sheet and then compare each cell in that range with the same address in another sheet.
This is the code so far:
Sub Compare_Report()
Dim vRange As Range
Dim v_Rangeversie As Range
On Error GoTo ErrorCatch
Debug.Print Now
Set v_Rangeversie = Worksheets("VorigeVersie").UsedRange
For Each v_range In Worksheets("1. Overzicht").UsedRange
For Each vCell In v_range
Debug.Print vCell.Address
'If vCell.Value != v_Rangeversie.Range.Cell(vCell.address)
'Then
' Debug.Print "Ongelijk"
' Cells are different.
' varSheetB.Cell.Interior.ColorIndex = 3
' varSheetB.Cell.Font.Color = 2
'End If
Next
Next
Exit Sub
ErrorCatch:
MsgBox Err.Description
End Sub
I can't get this If-statement to work:
'If vCell.Value != v_Rangeversie.Range.Cell(vCell.address)
What do I miss? Please help.
Regards, Jan
What about something like this:
Sub compareSheets(shtBefore As String, shtAfter As String)
'Retrieved from MrExcel.Com July 2012
Dim mycell As Range
Dim mydiffs As Integer
For Each mycell In ActiveWorkbook.Worksheets(shtAfter).UsedRange
If Not mycell.Value = _
ActiveWorkbook.Worksheets(shtBefore).Cells(mycell.Row, mycell.Column).Value Then
'Or use "mycell.Value <> othercell.Value"
mycell.Interior.Color = vbYellow
mydiffs = mydiffs + 1
End If
Next
MsgBox mydiffs & " differences found", vbInformation
ActiveWorkbook.Sheets(shtAfter).Select
End Sub
It highlights the differences in yellow.
For using it you should call it within another sub like this:
Sub highlight_differences()
Call compareSheets("1. Overzicht", "VorigeVersie")
End Sub
The conditional formatting alternative. You may want to write it in VBA in order to call it after generating the report... It's a one-liner:
Worksheets("1. Overzicht").UsedRange.FormatConditions.Add _
(xlExpression, , "=A1<>VorigeVersie!A1").Interior.ColorIndex = 3
I am trying to go through a column of empty cells in my excel spreadsheet in order to find the row in which the word "Yes" is found. Afterwards, upon finding the word in a particular row, for instance in cell D23, I want it to go over one column to cell E23 and paste the value in that cell into cell B100. Here is what I have so far, but it doesn't seem to be functioning correctly:
Sub Test3()
Dim x As String
x = "Yes"
' Dim found As Boolean
' Select first line of data.
Range("D4").Select
' Set search variable value.
' Set Boolean variable "found" to false.
found = False
' Set Do loop to stop at empty cell.
Do Until ActiveCell.Value = x
' Check active cell for search value.
If ActiveCell.Value = x Then
Range("B100").Value = ActiveCell.Offset(0, 1).Value
found = True
Exit Do
End If
' Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop
' Check for found.
If found = True Then
MsgBox "Value found in cell " & ActiveCell.Address
Else
MsgBox "Value not found"
End If
End Sub
Thanks!
As #tigeravatar mentioned in his comment, you'd probably be better off using Excel's native functions, but, if you want to do this via VBA, you can do it much more easily and efficiently using the Find function which returns a range if found or else 'Nothing' if not.
Using that, you can test to see what you got back. Try this:
Sub Test3()
Dim x As String
Dim rng As Range
x = "Yes"
Set rng = Range("D4:D10000").Find(x)
If Not rng Is Nothing Then
Range("B100").Value = rng.Offset(0, 1).Value
MsgBox "Value found in cell " & rng.Address
Else
MsgBox "Value not found"
End If
End Sub
Hope it does the trick.
I have a sheet in which I have data from two different sources.I've a blank row between them.I want to make this blank row as my delimiter.How can I find out if the entire row is blank or not.
If you're talking a literal entire row then code similar to this should work (so long as there are no formulas or spaces present in any of the cells as well):
If Application.CountA(ActiveCell.EntireRow)=0 Then
MsgBox "Row Empty"
Exit Sub
End If
Otherwise, for a range from a row:
Dim neValues As Range, neFormulas As Range, MyRange As Range
Set MyRange = Columns("C:AA")
On Error Resume Next
Set neValues = Intersect(ActiveCell.EntireRow.SpecialCells(xlConstants), MyRange)
Set neFormulas = Intersect(ActiveCell.EntireRow.SpecialCells(xlFormulas), MyRange)
On Error GoTo 0
If neValues Is Nothing And neFormulas Is Nothing Then
MsgBox "Nothing There"
Else
MsgBox "Something's There"
End If
(Source: http://www.ozgrid.com/forum/showthread.php?t=26509&page=1)
WorksheetFunction.CountA(), as demonstrated below:
Dim row As Range
Dim sheet As Worksheet
Set sheet = ActiveSheet
For i = 1 To sheet.UsedRange.Rows.Count
Set row = sheet.Rows(i)
If WorksheetFunction.CountA(row) = 0 Then
MsgBox "row " & i & " is empty"
End If
Next i