Sub sumtotal_of_month()
Dim a As Integer
a = 0
For i = 2 To 365
If (Sheets("Sheet1").Range("F" & i).Value = 1 And Sheets("Sheet1").Range("D" & i).Value <> "") Then
a = a + Sheets("Sheet1").Range("D" & i).Value
End If
Next i
MsgBox (a)
End Sub
Code above works fine, but when I try to substitute "1" with parameter month, it doesn't provide any output. This code is very amateur, because I am just a beginner. I use Excel 2007.
Sub sumtotal_of_month(month As Integer)
Dim a As Integer
a = 0
For i = 2 To 365
If (Sheets("Sheet1").Range("F" & i).Value = month And Sheets("Sheet1").Range("D" & i).Value <> "") Then
a = a + Sheets("Sheet1").Range("D" & i).Value
End If
Next i
MsgBox (a)
End Sub
given you have to check what value you are passing as month parameter to your sub, you can simplify your sub as follows:
Sub sumtotal_of_month(month As Integer)
Dim a As Double
With Worksheets("Sheet1")
a = WorksheetFunction.SumIf(.Range("F2:F365"), month, .Range("D2:D365"))
End With
MsgBox a
End Sub
Related
Would like to include a value to all my cells in column G if there are blank in column K, else no change.
If ActiveSheet.Range("K").Value, Criteria1:=" = " Then ActiveSheet.Range("G").Value = "Promo"
Else
Exit Sub
In order to test all the values you need to loop through the column. My example assumes the data doesn't have more than 100'000 lines and that you were testing for " = " to be included... addapt as needed.
Sub TestColumnK()
For i = 1 To ActiveSheet.Range("K100000").End(xlUp).Row
If InStr(1, ActiveSheet.Range("K" & i).Value, "=") > 0 Then
ActiveSheet.Range("G" & i).Value = "Promo"
End If
Next i
End Sub
Trying to use an Excel-macro that will automatically save the workbook once certain cells are filled in. The macro will check when changes are made to specific cells, then use variable data to save the workbook through a folder system organized by year and quarter, while giving the Workbook a name based on the Current date and a cell number. The macro will also check to see if the network path (it being on a server) is connected, and if not, exit the sub. I am getting a compile error "Expected: end of statement" at
Set mTitle = Year(Now)," & . & ", Month(Now), " &.& ", Day(Now), " & - & ", ActiveWorkbooks.Sheets("Control").Cells(1, "C")
I want to save the workbook with the following format: Year.Month.Day - CellValue, but it looks like VBA doesn't like periods. How can I solve this? Full code below.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cYear As String
Dim Quarter As String
Dim fdObj As Object
Dim mTitle As String
Dim sCheck
Application.ScreenUpdating = False
Set cYear = Year(Now)
Set Quarter = (Month(Now) + 2) \ 3
Set fdObj = CreateObject("Scripting.FileSystemObject")
sCheck = "S:\Estimating Data\Estimates\test.txt"
Set mTitle = Year(Now)," & . & ", Month(Now), " &.& ", Day(Now), " &.& ", ActiveWorkbooks.Sheets("Control").Cells(1, "C")
If Intersect(Target, Range("C1:C5")) Is Nothing Then
Exit Sub
Else
If WorksheetFunction.CountA(Range("C1:C5")) = 0 Then
Exit Sub
Else
Shell ("Net View \\S:\ > " & vsFileName)
If FileLen(vsFileName) = 0 Then
Exit Sub
Else
If fdObj.FolderExists("S:\Estimating Data\Estimates\" & cYear & "\""Q" & Quarter & ".*xlsm") Then
ActiveWorkbook.SaveAs Filename:="S:\Estimating Data\Estimates\" & cYear & "\""Q" & Quarter & "\" & mTitle & ".*xlsm"
Else
fdObj.CreateFolder ("S:\Estimating Data\Estimates\" & cYear & "\""Q" & Quarter & ".*xlsm")
End If
End If
End If
End If
End Sub
I'm trying to get the value from each cell from column A of the table compared to a value entered by the user, the problem is that in the line:
If TamañoCmb.Value = Range("A" & Trim(fila)).Value Then
The range value doesn't return any value when it should get the cell a2 in the first loop and enter the if. I debugged the code and isn't returning any value.
Private Sub CalcularBtn_Click()
If NombreTxt.Value = vbNullString Then
MsgBox "Debe ingresar todos los datos"
ElseIf TamañoCmb.Value = vbNullString Then
MsgBox "Debe ingresar todos los datos"
ElseIf CantidadTxt.Value = vbNullString Then
MsgBox "Debe ingresar todos los datos"
End If
Worksheets("Información").Activate
Dim fila As Single
Dim Produccion_Principal As String
Dim Precio_Principal As String
For fila = 2 To 8
If TamañoCmb.Value = Range("A" & Trim(fila)).Value Then
Produccion_Principal = Range("D" & Trim(fila)).Value
Exit For
End If
Next
ProduccionTxt.Value = Val(Produccion_Principal)
For fila = 2 To 8
If TamañoCmb.Value = Range("A" & Trim(fila)).Value Then
Precio_Principal = Range("C" & Trim(fila)).Value
Exit For
End If
Next
PrecioUTxt.Text = Val(Precio_Principal) * CantidadTxt
If Produccion_Principal < CantidadTxt.Value Then
MsgBox "Producto primario mayor a producción, Cambie la cantidad"
End If
If Producto2Marco.Enabled Then
Dim Produccion_Secundaria As String
Dim Precio_Secundario As String
For fila = 2 To 8
If Producto2Cmb.Value = Range("A" & Trim(fila)).Value Then
Produccion_Secundaria = Range("D" & Trim(fila)).Value
Exit For
End If
Next
If Producto2Cmb.Value = Range("A" & Trim(fila)).Value Then
Precio_Secundario = Range("C" & Trim(fila)).Value
End If
PrecioSecTxt.Value = Val(Precio_Secundario)
If Produccion_Secundaria < CantidadSecTxt.Value Then
MsgBox "Producto secundario mayor a Producción, Cambie la cantidad"
End If
End If
I fixed the problem using a variable to store the value of the range like this:
Dim celda As String
celda = Range("A" & Trim(fila)).Value
Thanks to everyone for helpimg solve the issue!
I am trying to loop through a list of data then and copy the rows that contain "WIP" into a second tab. yet the code will nor do anything when I hit execute. Can someone explain why?
Thank you.
Sub Update_LvL1_WIP()
Dim BrowFi As Integer
Dim BrowWIP1 As Integer
Dim dblSKU As Double
Dim strDescription As String
Dim strType As String
BrowFi = (ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row) + 1
Do While BrowFi > 4
If Range("G" & BrowFi).Value = "WIP" Then
strType = Range("G" & BrowFi).Value
strDescription = Range("F" & BrowFi).Value
dblSKU = Range("E" & BrowFi).Value
Worksheets("WIP 1").Activate
BrowWIP1 = (ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row) +1
Range("A" & BrowWIP1).Value = dblSKU
Range("B" & BrowWIP1).Value = strDescription
Range("B" & BrowWIP1).Value = strType
Else
BrowFi = BrowiF - 1
End If
Loop
End Sub
You have a typo in your decrement variable
BrowFi = BrowiF - 1
should be
BrowFi = BrowFi - 1
Since BrowiF was not defined it was treated as 0 and so you were setting BrowFi to -1 on the first run through the loop.
It may be worth using Option Explicit to help catch these errors.
I'm sure you will find the problem that I'm uncapable to do.
Below you can see a resume of the code in which I have the problem.
After changing the value in the combobox1 it shows an error "Compile error. Argument not optional", highlighting in yellow "Sub ComboBox1_Change()" and in blue "Call TextBox4_Exit".
I think I'm doing something wrong with the arguments needed but don't know how to handle.
Thank you for your help.
Sub ComboBox1_Change()
If TextBox4.Visible = True And TextBox4.Value <> "" Then
Call TextBox4_Exit
End If
Sub TextBox4_Exit(ByVal cancel As MSForms.ReturnBoolean)
Dim placas As String
placas = TextBox4.Value
I = 3
While Range("E" & I).Value <> ""
If Range("E" & I).Value = mensaje Then
If Range("L" & I).Value = mensaje2 Then
If sheet1 = "SIC" Then
Range("X" & I).Value = placas
TextBox11.Value = Range("Y" & I).Value
TextBox10.Value = Range("Z" & I).Value
Else
Range("U" & I).Value = placas
TextBox11.Value = Range("AN" & I).Value
End If
End If
End If
I = I + 1
Wend
End Sub
First, the Sub TextBox4_Exit(ByVal cancel As MSForms.ReturnBoolean) has one argument and it's not marked as Optional,so you need to pass a parameter to Call TextBox4_Exit.
Second, did you miss your End Sub of ComboBox1_Change()?