I have a report where I'm trying to get the sum of a dynamic number of rows in order to produce a subtotal.
If Cells(s, 1).Value = "start" Then
If Cells(r, 1).Value = "subtotal" Then
'Set the Monthly Subtotal Formulas
Cells(r, 44) = "=SUM(AR" & Trim(Str(s)) & ":AR" & Trim(Str(r - 1)) & ")"
Cells(r, 46) = "=SUM(AT" & Trim(Str(s)) & ":AT" & Trim(Str(r - 1)) & ")"
'Set the Weekly Subtotal Formulas
Cells(r, 48) = "=SUM(AV" & Trim(Str(s)) & ":AV" & Trim(Str(r - 1)) & ")"
Cells(r, 52) = "=SUM(AZ" & Trim(Str(s)) & ":AZ" & Trim(Str(r - 1)) & ")"
'Set the Daily Subtotal Formulas
Cells(r, 54) = "=SUM(BB" & Trim(Str(s)) & ":BB" & Trim(Str(r - 1)) & ")"
Cells(r, 56) = "=SUM(BD" & Trim(Str(s)) & ":BD" & Trim(Str(r - 1)) & ")"
'Set the Hourly Formulas
Cells(r, 60) = "=SUM(BH" & Trim(Str(s)) & ":BH" & Trim(Str(r - 1)) & ")"
Cells(r, 62) = "=SUM(BJ" & Trim(Str(s)) & ":BJ" & Trim(Str(r - 1)) & ")"
Cells(r, 1) = ""
End If
Cells(s, 1) = ""
End If
Basically, each work group is within the cell values "start" and "subtotal".
How can I find the 's' or row number and use that in the formula?
most of the time, built-in subtotals feature of Excel should be sufficient
in case you really need to use VBA solution and don't know how to iterate it over all "subtotal" tags already present in the data, place your code inside a loop like this:
header_column = Intersect(ActiveSheet.Range("A:A"), ActiveSheet.UsedRange).Value2
s = 1
For r = 1 To UBound(header_column)
If header_column(r, 1) = "start" Then
s = r
End If
If header_column(r, 1) = "subtotal" Then
' ... do your stuff here ... '
' s = r ' if the next "start" tag always follows a subtotal tag, no need for the "start" tags at all, just uncomment this line just before End If
End If
Next
P.S.: no need for "string" & Trim(Str(integer)), use "string" & integer instead
Related
I'm trying to compare 2 cells and if its true that row will be deleted, i tried using msgbox to return the value and it shows its true, but row is not deleting.
The first cell is derived using formula in 1 sheet and the other is just numbers, does that make a difference?
Dim r, s, i, t As Long
Dim com, cc, bl, acc As Long
Dim rDB, rInput As Range
Dim shDB, shInput As Worksheet
Set shDB = ActiveWorkbook.Sheets("Database")
Set rDB = shDB.Range("A1", "T1000")
Set shInput = ActiveWorkbook.Sheets("Data Input")
Set rInput = shInput.Range("A1", "R1000")
r = 2
Do While Len(shDB.Cells(r, 1).Formula) > 0
com = shInput.Cells(7, 5).Value
cc = shInput.Cells(5, 5).Value
bl = shInput.Cells(9, 5).Value
acc = shInput.Cells(5, 10).Value
MsgBox (com & " " & shDB.Cells(r, 1).Value & " " & cc & " " & rDB.Cells(r, 2).Value & " " & rDB.Cells(r, 3).Value & " " & bl & " " & rDB.Cells(r, 4).Value & " " & acc)
If shDB.Cells(r, 1).Value = com And rDB.Cells(r, 2).Value = cc And rDB.Cells(r, 3).Value = bl And rDB.Cells(r, 4).Value = acc Then
shDB.Rows(r).EntireRow.Delete
MsgBox ("deleting rows")
Else
r = r + 1
End If
Loop
When deleting alway go from the last index to the first. This applies to listboxes, comboboxes, ranges, ...etc.
If you delete from first to last then you will skip every other row
I am trying to get my code to insert a formula into an array based on how many data points there are in my data set. The code below almost works but after the first iteration of X is complete it does not insert the formula into the all the rows in the columns.
Worksheets(" Branded").Range("C3").Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
For Y = 1 To Column_Limit1 - 1
Range("C3").Offset(0, Y).Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
For X = 1 To Row_Limit1 - 1
Range("C3").Offset(X, 0).Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
Next X
Next Y
Change to this:
With Worksheets(" Branded")
.Range(.Cells(3, 3), .Cells(Row_Limit1 + 2, Column_Limit1 + 2)).FormulaR1C1 = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
.Range(.Cells(Row_Limit1 + 3, 3), .Cells(Row_Limit1 + 3, Column_Limit1 + 2)).FormulaR1C1 = "=SUM(R3C:R[-1]C)"
End With
When using R1C1 there is no need of a loop.
I'm trying to get a script to find the appropriate column and create a formula with that cells "name" in it. This is the script:
'Search for value
Dim i As Integer
i = 4
Do Until Cells(9, i).Value = ddLeveranciers.Value Or Cells(9, i).Value = ""
i = i + 1
Loop
'Add formulas
Range("D5").Formula = "=IF(" & Cells(15, i) & "<>"""",D4*" & Cells(15, i) & ","""")"
This now returns the formula "=IF(1.23<>"",D4*1.23,"")", 1.23 being the value of cells(15,i). I would like the script to return (for example) "=IF(D15<>"",D4*D15,"")". How do I do that?
You can use the .Address property.
Range("D5").Formula = "=IF(" & Cells(15, i).Address & "<>"""",D4*" & Cells(15, i).Address & ","""")"
For example:
MsgBox(Cells(1,1).Address)
Would return $A$1
I have some code that searches a column of values in sheet(3) in the format of a string "value1 - value 2"
value2 is the first value in a column in sheet(2) and value1 is a value in the same column, in a cell further down the sheet.
The setup I have is:
In sheet(1), cells C2:C6 have values a-e respectively
In sheet(2), cell C1 has value "yes" and cells C2:C6 have values 1-5
respectively
In sheet(3), cell A2 has the value "4 - yes"
So the code should countif a column in sheet2 with the first value being yes and look for cells with the value 4, and put the result in cell B2 on sheet(3)
What it actually does is find the yes column (column C) and search the same column on sheet(1) (so the message boxes show letters rather than numbers).
Is there a way I can more precisely specify the sheet the countif function uses?
I'm using Excel 2000 on Windows 7
Private Sub test_click()
scenario_count = 6
Dim i As Integer
i = 1
Sheets(2).Select
For j = 2 To 24
If Sheets(2).Cells(1, j).Value = Right(Sheets(3).Cells(i + 1, 1).Value, Len(Sheets(3).Cells(i + 1, 1).Value) - InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 1) Then
MsgBox ("number of scenarios is " & scenario_count)
MsgBox ("value searching for is " & "'" & Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2) & "'")
MsgBox ("Range searched is " & Range(Cells(2, j), Cells(scenario_count, j)).Address & " in " & ActiveSheet.Name)
MsgBox ("Number of occurrences " & Sheets(2).Application.WorksheetFunction.CountIf(Range(Cells(2, j), Cells(scenario_count, j)), Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2)))
Sheets(2).Select
Sheets(3).Cells(i + 1, 2).Value = Sheets(2).Application.WorksheetFunction.CountIf(Range(Cells(2, j), Cells(scenario_count, j)), Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2))
For Each c In Range(Cells(2, j), Cells(scenario_count, j))
MsgBox ("comparing " & c.Address & " " & c.Value & " with " & Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2))
Next c
GoTo endofif2
End If
Next
endofif2:
End Sub
Where you have 'WorksheetFunction.CountIf(Range(Cells(2, j)', simply insert the sheet before the range reference, like so:
Sheets(2).Range(Sheets(2).Cells(2, j), Sheets(2).Cells(scenario_count, j))
EDIT full formula which references the sheet for both the Cells and the Range functions blatently taken from #Rory's comment.
i'm trying to execute these code in my excel sheet
ActiveCell.Offset(0, 3).Formula = "=if(SUM(N" & i + 2 & ":N" & i + 5 & ")>0;MEDIAN(N" & i + 2 & ":N" & i + 5 & ");0)"
and i'm get an #1004 error with no more informations. Can anybody eyplain my failure?
I hav some others formulars insert in the same way...thx
EDIT:
My Tables look like that
This should be a projectmanagement tool - Breitband Delphi Method ;)
So my code goes through all the rows and check in which column the descripton is (level 1,2,3,4).
Next the code is adding the rows 8-12 for example.. here i can enter some informations for the project... and now my script should add the formula at column k-n.
My code is not very nice (as my english :) ) - it is just a prototype..
This is my Loop
i = 5
canSkip = False
Do
' fist first the level
If Not IsEmpty(Range("B" & i).Value) Then
level = 1
If Not IsEmpty(Range("D" & i + 1)) Then
' ye we can - so skip this loop
canSkip = True
End If
ElseIf Not IsEmpty(Range("D" & i).Value) Then
level = 2
If Not IsEmpty(Range("F" & i + 1)) Then
' ye we can - so skip this loop
canSkip = True
End If
ElseIf Not IsEmpty(Range("F" & i).Value) Then
level = 3
If Not IsEmpty(Range("H" & i + 1)) Then
' ye we can - so skip this loop
canSkip = True
End If
ElseIf Not IsEmpty(Range("H" & i).Value) Then
level = 4
canSkip = False
End If
If canSkip = True Then
i = i + 1
Else
' First insert some... and bang it to a group
' Insert Formula
Range("K" & i).Activate
ActiveCell.Formula = "=min(L" & i + 2 & ":L" & i + 5 & ")"
ActiveCell.Offset(0, 1).Formula = "=max(L" & i + 2 & ":L" & i + 5 & ")"
'Range("T1").FormulaLocal = insertMedianFormula
'ActiveCell.Offset(0, 3).Formula = "=WENN(SUMME(N" & i + 2 & ":N" & i + 5 & ")>0;MITTELWERT(N" & i + 2 & ":N" & i + 5 & ");0)"
Range("A" & i + 1).Activate
For x = 1 To 5
ActiveCell.EntireRow.Insert
If x = 5 Then
If level = 1 Then
ActiveCell.Offset(0, 1).Value = "Experte"
ActiveCell.Offset(0, 2).Value = "Aufw."
ActiveCell.Offset(0, 3).Value = "Bemerkung"
ElseIf level = 2 Then
ActiveCell.Offset(0, 3).Value = "Experte"
ActiveCell.Offset(0, 4).Value = "Aufw."
ActiveCell.Offset(0, 5).Value = "Bemerkung"
ElseIf level = 3 Then
ActiveCell.Offset(0, 5).Value = "Experte"
ActiveCell.Offset(0, 6).Value = "Aufw."
ActiveCell.Offset(0, 7).Value = "Bemerkung"
ElseIf level = 4 Then
ActiveCell.Offset(0, 7).Value = "Experte"
ActiveCell.Offset(0, 8).Value = "Aufw."
ActiveCell.Offset(0, 9).Value = "Bemerkung"
End If
' now just bang it to a group
ActiveCell.Resize(5, 10).Rows.Group
End If
Next x
i = i + 6
End If
' are we finshed?
If i > lastUsedRow Then
Exit Do
End If
canSkip = False
Loop
Original formula (MS standard) uses "," instead of ";"
ActiveCell.Offset(0, 3).Formula = "=IF(SUM(N" & i + 2 & ":N" & i + 5 & ")>0,MEDIAN(N" & i + 2 & ":N" & i + 5 & "),0)"
or use:
ActiveCell.Offset(0, 3).FormulaLocal = "=IF(SUM(N" & i + 2 & ":N" & i + 5 & ")>0;MEDIAN(N" & i + 2 & ":N" & i + 5 & ");0)"
Please, refer this:
Formula
FormulaLocal
[EDIT]
First of all...
IsEmpty indicates whether a variable (of variant) has been initialized. So, if you want to check if cell is empty (does not contains any value), use:
Range("B" & i)<>""
Second of all..
Your code has no context. What it means? Using ActiveCell or Range("") or Cell() depends on what workbook (and its sheet) is actually in usage!
You should use code in context:
With ThisWorkbook.Worksheets("SheetName")
.Range("A1").Offset(0,i).Formula = "='Hello Kitty'"
.Cell(2,i) = "123.45"
End With
Third of all...
Review and debug you code and start again using above tips ;)