Can not SUM selected cells With SUMIF - excel

I would like to be able to Check for criteria "owners" In column A and SUM cell from the same row column E with the cell above and repeat through the spread sheet where owner is found.
I have tried to use Function =SUMIF(A3,"*owner*",E2:E3) This returns the Value above the cell in the row with owner but does not SUM them.
I have also tried this VBA CODE and it does the same thing.
Sub vba_sumif()
Dim gRange As Range
Dim sRange As Range
Set gRange = Range("A3")
Set sRange = Range("E2:E3")
Range("G2") = _
WorksheetFunction.SumIf(gRange,"*Owner*", sRange)
End Sub
Ideally it would return the summed cells in the above cell. using VBA
Thanks again,
Aaron

Well first of all, I would suggest just using the formula unless you have a reason not to:
Formula:: Your first error with the formula is both ranges need to be the same size.
VBA:: Same thing here with range sizes, then you also need to remember to make sure Excel knows what sheet you're referring to. I like to do this with a With My_Worksheet_Namestatement, then include a . before any range from that sheet. Just make sure to change the sheet name to your sheet name
Option Explicit
Sub vba_sumif()
Dim TableSheet As Worksheet
Dim gRange As Range
Dim sRange As Range
Set TableSheet = Worksheets("TableSheet")
With TableSheet
Set gRange = .Range("A2:A13")
Set sRange = .Range("E2:E13")
.Range("G2").Value = _
WorksheetFunction.SumIf(gRange, "*Owner*", sRange)
End With
End Sub

Related

Copy non-contiguous cells and paste in a row retaining number format Excel VBA

I hope someone can help me with this as it's driving me up the wall!
There are 5 non-contiguous cells in a worksheet that I want to copy to the next empty row on another worksheet whilst retaining the number formatting (which varies). I have this so far but am struggling working out how to retain formatting. Can anyone please help? Thanks I anticipation.
`With wsCalc
For bRun = 1 To 4
bData(bRun) = Application.Choose(bRun, .Range("g2"), .Range("b2"), .Range("R2"), .Range("Q14"))
Next bRun
End With
wSResults.Cells(Rows.Count, "a").End(xlUp).Offset(1).Resize(, 4).Value = bData
`
Here's a possible solution, using your hard-coded cell addresses. You will have to set wsCalc and wsResults to their proper worksheets. Slightly more elegant would be to define a "non-contiguous" range on your wsCalc sheet (select the 1st cell, keep Ctrl pressed and select the next one etc, then type a name in the drop-down box just to the left of the formula bar).
Option Explicit
Sub CopyWithFormat()
Dim wsCalc As Worksheet
Set wsCalc = ActiveSheet 'Or whatever your calc sheet is
Dim rngSource As Range
Set rngSource = wsCalc.[G2,B2,R2,Q14]
Dim wsResults As Worksheet
Set wsResults = ActiveSheet 'Or whatever your result sheet is
Dim clDest As Range
Set clDest = wsResults.Cells(Rows.Count, "a").End(xlUp).Offset(1)
Dim cl As Range
For Each cl In rngSource.Cells
clDest.Value = cl.Value
clDest.NumberFormat = cl.NumberFormat
Set clDest = clDest.Offset(1)
Next cl
End Sub
Instead of using .Value, try .Text. It retains formatting. See below.
Gary's Student is right, text is read only, it should be used for the input not the output.
bData(bRun) = Application.Choose(bRun, .Range("g2").Text, .Range("b2").Text, .Range("R2").Text, .Range("Q14").Text)
I also agree with other answer the entire code could be set up more straight forward.

VBA: Clear multiple discontiguous cells

I have the following code right now:
Sub ClearTimeline()
Dim wsTimeline As Worksheet
Set wsTimeline = ThisWorkbook.Sheets(1)
wsTimeline.Range("B8, F25, F26, F28, F30, M17, M31, L17, L24:L25, L26, L28, L29:L30, J24:J25, J26, J28, J29:J30, K25, K26, K28, K30").ClearContents
End Sub
I want to replace the fixed cell reference by something more dynamic. The rows won't change, so I want to change how I reference the columns. I have a "reference cell", i.e. a cell I can always find/know the column of.
Sub ClearTimeline()
Dim wsTimeline As Worksheet
Dim referenceDay As Long
Set wsTimeline = ThisWorkbook.Sheets(1)
referenceDay = 10
'what works:
wsTimeline.Range(wsTimeline.Cells(referenceDay - 9, 1)).ClearContents 'clears single cell
wsTimeline.Range(wsTimeline.Cells(referenceDay - 9, 1), wsTimeline.Cells(referenceDay - 1, 1)).ClearContents 'clears range
End Sub
My goal is to refer to every column as 'referenceDay +/- X' because the distance to this day will stay the same even if the sheet is altered in some other way. But how do I tell Excel to do the same action for non-contiguous cells? I'm not sure how to reference them and what to put into the parentheses after wsTimeline.Range
If I understand correctly, the whole range of hardcoded cells should be moved to the right or to the left, depending on the column. Offset() is pretty good for this.
The "red" cells are the cells of the initial range, and the selected cells are the cells with the Offset(columnoffset:=2). I have used .Select to illustrate the difference, because there is overlapping and colors would not have been ok:
Sub ClearTimeline()
Dim wsTimeline As Worksheet
Set wsTimeline = ThisWorkbook.Worksheets(1)
Dim timelineRange As Range
Set timelineRange = wsTimeline.Range("B8, F25, F26, F28, F30, M17, M31, L17, L24:L25, L26, L28, L29:L30, J24:J25, J26, J28, J29:J30, K25, K26, K28, K30")
timelineRange.ClearContents
timelineRange.Interior.Color = vbRed
timelineRange.Offset(columnoffset:=2).Select 'But in general, do not use .Select
End Sub

Excel List of Blank Cells

So I have a big excel sheet with a bunch of empty cells in various locations. I want an easy to work with list of which cells are empty. I was hoping to make a new worksheet that was populated with the locations of the empty cells. I wanted to have this to just populate the cells I want it to. I kept the header from the worksheet I will be checking and added a blank cells count, so I want the following cells in the column to be populated by the list of empty cell locations.
Now I know I can use =ISBLANK to test if a cell is empty or not, but I only care about the cells that return TRUE. So I figure I'll need a loop. And I want the location of the cell so I can use =CELL. And to make this most readable I want to do this on a column by column basis.
But I want to populate a spreadsheet with this information in a manner similar to how functions work (I just want to copy and paste it to other cells and columns). But it's pretty clear that I am going to need VBA.
My question is how can I create a macro to populate my spreadsheet with a list of empty cells? How do I apply it to the cells?
I assume you have data in sheet1, I have used sample range// Range("A1:c15") however you can define range as per need and blank cells address will be published in next sheet.
Sub FindBlank()
Dim rng As Range
dim i as long
For Each rng In Sheet1.Range("A1:c15").SpecialCells(xlCellTypeBlanks)
i = i + 1
Sheet2.Cells(i, 1) = rng.Address
Next
End Sub
If you want a list of the cells that are empty, you can use Range().SpecialCells(xlCellTypeBlank):
Sub getEmptyCellAddresses()
Dim rng As Range
Dim ws as Worksheet
Set ws = Sheets("Sheet1") ' CHANGE AS NECESSARY
Set rng = ws.Range("A1:A15").SpecialCells(xlCellTypeBlanks) ' Edit/change range as necessary
ws.Cells(1, 2).Value = rng.Cells.Address ' Change `ws.cells(1, 2)` to whatever destination you like
End Sub
Edit: Ah, beaten by 16 seconds by #RamAnuragi ...but anyways, they're slightly different ways to tackle the question so I'll leave it.
Edit: For funsies, here's another way to put them all in a column, one row per cell...and more, per your comments.
Sub listEmptyCells()
Dim emptyAddresses() As String
Dim i As Long
Dim ws As Worksheet
Dim rng As Range
Set ws = Sheets("Sheet1") ' CHANGE AS NECESSARY
Set rng = ws.Range("A1:A15")
If WorksheetFunction.CountBlank(rng) = 0 Then
MsgBox ("No empty cells in the range")
Exit Sub
End If
emptyAddresses() = Split(rng.SpecialCells(xlCellTypeBlanks).Address, ",")
For i = LBound(emptyAddresses) To UBound(emptyAddresses)
ws.Cells(i + 1, 2).Value = emptyAddresses(i)
Next i
End Sub

Excel Macro or VBA for making selected range values absolute

Can anybody help me out in making all values of my selected range values Absolute (or applying absolute formula on each cell in a range) by single button using Excel VBA?
A B C
5.119999886 -13.06999969 -13.14000034
-5.76999998 -12.52000046 -12.78999996
-5.88000011 -13.69999981 -14.13000011
5.46999979 -12.61999989 -12.48999977
Consider:
Sub dural()
Dim rng As Range
Set rng = Range("A1:F10")
rng.Replace what:="-", lookat:=xlPart, replacement:=""
End Sub
Not exactly VBA, but if required can be recorded.
Select your Data Range, press Ctrl H (Replace), in Find what add " - " sign and replace all. This will remove the negative sign from all numbers and it will become absolute.
Note: As mentioned in comment that OP agrees that this works for him, hence posting as an answer.
You can do something like this:
Sub MakeAbsolute()
Dim c As Range
Dim rngToAbs As Range
'Set the worksheet name and range appropriately
Set rngToAbs = Worksheets("Sheet1").Range("A1:C2")
For Each c In rngToAbs
c.Value = Abs(c.Value)
Next c
End Sub
You need to change Sheet1 to your worksheet name and A1:C2 to whatever range of cells you want to take the absolute value of.
Hopefully the code is simple enough to understand, but the basic idea is that we will declare a specific range, loop through each cell in that range, and change the value of that cell to be the absolute value by applying the Abs function.

Visual Basic 2007 Adding values

To start with I'm not really a wise man. So I was trying to add up two values and display them in a third (that's easy) but I also wanted it to repeat an infinite amount of times (namely that it does the same for every row, let's say I place the result in I5, I want also, on every I under it (I6, I7, I8, etc...)
Should it be:
Private Sub Worksheet_Change()
if IsNumeric(Range("B1").sort) And IsNumeric(Range("B2").sort) then
Dim value1 As Single
Dim value2 As Single
range(I5).sort = value+1 + value2
End Sub
Or as I think I'm horribly mistaken?
You're using the .Sort property of Range where you should be using .Value.
There's a couple of ways to achieve what you're looking to do. First option is to iterate through the range and add the relevant value to each cell like so:
Public Sub addCells()
Dim rng As Range, cell As Range
'Set the sheet name
With ThisWorkbook.Worksheets("Sheet_Name")
'Set the range below:
Set rng = .Range("I1:I10")
'Loop through range and add cells together
For Each cell In rng
cell.Value = cell.Offset(0, 2) + cell.Offset(0, 1)
Next cell
End Sub
Another way to do it if the values to be added is ordered in for example column A and B would be:
Public Sub addCells()
Dim rng As Range, cell As Range
'Set the sheet name
With ThisWorkbook.Worksheets("Sheet1")
'Add the first formula to the sheet
.Range("C1").Value = "=SUM(A1+B1)"
'Change the range below to the range to be filled
.Range("C1").AutoFill Destination:=.Range("C1:C10")
End With
End Sub

Resources