Have a problem with summing cells in vba using =sum(number, string) function.
Here is an example, that is working in regular excel: sum(5, A1)
But it doesn't work via VBA formula - asking for a number instead of a string. Is there a way to use =sum(5, A1) in VBA?
In VBA:
Sub dural()
MsgBox Application.WorksheetFunction.Sum(5, Range("A1").Value)
End Sub
or as JNevill points out:
Sub dural2()
MsgBox Evaluate("SUM(5,A1)")
End Sub
EDIT:
Here are some alternatives:
Sub poiuyt()
MsgBox Application.WorksheetFunction.Sum(5, [A1])
MsgBox 5 + [A1]
End Sub
To put a formula in a cell, say cell B9, use:
Sub PutFormulaInCell()
Range("B9").Formula = "=SUM(5,A1)"
End Sub
Related
I'm trying to put an array formula into a range of cells ("B2:B10") The formula should return multiple results dependent on the value in cell A2. When I do it the normal way (ctrl, shift, enter) it works ok, but when I try to do it with code it returns the same result in each cell which is the first instance found. Can anyone help me out to get the result I'm looking for?
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("$A$2").Address Then
With Range("B2:B10")
.FormulaArray = "=INDEX(Absence!$C$2:$C$151, SMALL(IF($A$2=Absence!$A$2:$A$151, ROW(Absence!$A$2:$A$151)-ROW(Absence!$A$2)+1), ROW(Absence!1:1)))"
.Value = .Value
End With
End If
End Sub
Is this any better:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("$A$2").Address Then
Range("B2").FormulaArray = "=INDEX(Absence!$C$2:$C$151, SMALL(IF($A$2=Absence!$A$2:$A$151, ROW(Absence!$A$2:$A$151)-ROW(Absence!$A$2)+1), ROW(Absence!1:1)))"
Range("B2").Copy Range("B3:B10")
Range("B2:B10").Value = Range("B2:B10").Value
End If
End Sub
The problem is that you are array-entering the formula into all of the cells at once instead of array-entering into the first cell and filling down. Without filling down, the ROW(1:1) does not progess. You need to put all of the possible k values for the SMALL function in at once with ROW(1:150).
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("$A$2").Address Then
With Range("B2:B10")
.FormulaArray = "=INDEX(Absence!$C$2:$C$151, SMALL(IF($A$2=Absence!$A$2:$A$151, ROW(2:151)-ROW(2:2)+1), ROW(1:9)))"
.Value = .Value
End With
End If
End Sub
Btw, when we use ROW(Absence!$A$2:$A$151) to achieve a number between 2 and 151, the worksheet and column letter are not necessary. ROW(2:151) will do fine and cleans up the formula a little.
I am NEW to VBA and need some help. I am trying to write something that will look in my sheet named 'ROLLUP' and if cell H5 contains the letters "jan", then I would like to paste the values from sheet named 'Project" cells I97:I102 in sheet named "Detail" cells H38:H43. The following is my code and nothing happens. Any help?
Option Explicit
Private Sub ETC_pop()
month = Worksheets("ROLLUP").Range("H5")
If ws.Name = "Detail" Then
If InStr(month, ("Jan")) > 0 Then
Worksheets("Detail").Range("H38:H43").Value = Worksheets("Project").Range("I97:I102").Value
End If
End If
End Sub
You can make this one If-statement with two parts
Private Sub ETC_pop()
If Sheets("ROLLUP").Range("H5").Text="Jan" Then
Sheets("Detail").Range("H38:H43").Copy
Sheets("Project").Range("I97:I102").PasteSpecial xlPasteValues
End If
End Sub
Edit:
To account for H5 containing a date, we want to use MONTH() function such as:
Private Sub ETC_pop()
If Month(Sheets("ROLLUP").Range("H5"))=1 Then
Sheets("Detail").Range("H38:H43").Copy
Sheets("Project").Range("I97:I102").PasteSpecial xlPasteValues
End If
End Sub
It's better not to use a variable Month which is similar to the function, but to use a distinct name, like myMonth.
Other notes are inside the code (as comments).
Option Explicit
Private Sub ETC_pop()
Dim myMonth As String
myMonth = Worksheets("ROLLUP").Range("H5")
'If ws.Name = "Detail" Then '< -- not needed according to your post
' another option >> use if Case In-sensitive
' If UCASE(myMonth) Like "*JAN*" Then
If myMonth Like "*jan*" Then
Worksheets("Detail").Range("H38:H43").Value = Worksheets("Project").Range("I97:I102").Value
End If
'End If
End Sub
I am trying to collect data from a cell from one sheet(3 Combined I) and pass it to a cell in another sheet(5 Gas I) using vba via a command button. This is what I have tried to use but it insteads copies the formula within that cell from sheet 3 Combined I?
Private Sub CommandButton1_Click()
Worksheets("3 Combined I").Range("e23").Copy Destination:=Worksheets("5 Gas I").Range("d10")
End Sub
Please try
Private Sub CommandButton1_Click()
Worksheets("5 Gas I").Range("d10").value = Worksheets("3 Combined I").Range("e23").value
End Sub
You can also try the PasteSpecial method of Range object.
Please see Excel VBA Copy Paste Values only( xlPasteValues )
Updated
#nbayly recommands Value2 property of Range object instead of Value property.
Private Sub CommandButton1_Click()
Worksheets("5 Gas I").Range("d10").value2 = Worksheets("3 Combined I").Range("e23").value2
End Sub
Reason as: What is the difference between .text, .value, and .value2?
I have added a private sub worksheet_calculate() in a sheet called Main. I have a value in column AP with formulas derived from other sheets and if that number is greater than value in X I want to display a message as a warning that it's over, but the code is not working any suggestions why?
Private Sub Worksheet_Calculate()
If Sheets("Main").Range("AP7").value > Sheets("Main").Range("x7").value Then
MsgBox "You Are Over Pieces Suggested"
End If
End Sub
Try this.
Private Sub Worksheet_Calculate()
If Range("AP7").Value > Range("X7").Value Then
MsgBox "You Are Over Pieces Suggested."
End If
End Sub
EDITED####
Edited the original code to run as a Worksheet_Calculate rather than a Change.
Working on trying to set the ranges to columns for you now.
EDIT#########
I flippin love a challenge. Try This.
Private Sub Worksheet_Calculate()
Set Target = Range("AP:AP").SpecialCells(xlCellTypeFormulas)
If Target Is Nothing Then Exit Sub
For Each c In Target
If c > Range("X" & c.Row) Then
MsgBox "You Are Over Pieces Suggested - Cell " & "AP" & c.Row
End If
Next
End Sub
Consider using Data Validation on cell AP7 using a "Custom" formula of: =AP7<=$X$7
Fill in the Error Alert tab on the validation menu: Stop; "You Are Over Pieces Suggested". I think this might achieve what you want without any macros. In fact, it can prevent an invalid number from being entered in the first place.
I am using data validation in excel 2007. I am using this code to make invalid data marked with red circle.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rc As Integer
Range(Target.Address).Select
ActiveSheet.ClearCircles
ActiveSheet.CircleInvalid
If Not Range(Target.Address).Validation.Value Then
rc = MsgBox("Data Validation errors exist! " & Range
(Target.Address).Validation.ErrorMessage & " Please correct circled entries!", vbCritical, "Failure")
Exit Sub
End If
End Sub
As you can see in the code when I put wrong data then first of that specific range is going to selected and then all invalid data is marked with red circle.
But I want that only that specific cell should be marked with red not all data .
Thanks.
You can try this code from an Excel MVP:
Dim TheCircledCell As Range
Sub CircleCells(CellToCircle As Range)
If Not CellToCircle Is Nothing Then
With CellToCircle
If .Count > 1 Then Exit Sub
Set TheCircledCell = CellToCircle
.Validation.Delete
.Validation.Add xlValidateTextLength, xlValidAlertInformation, xlEqual, 2147483647#
.Validation.IgnoreBlank = False
.Parent.CircleInvalid
End With
End If
End Sub
Sub ClearCircles()
If Not TheCircledCell Is Nothing Then
With TheCircledCell
.Validation.Delete
.Parent.ClearCircles
End With
End If
End Sub
Note that you can't use the Excel standard Validation function on these cells.
[Source and explanation of the code]