Error In Code Regarding Iteration Through Multiple For Each Loops
May 23, 2014
I am having some trouble with some code I am trying to write for some calculations. I am trying to make excel iterate through a range of cells in 2 columns and compare the cells in each row of the column to a set different number. If both the numbers in the row pass the test then I add them to a count. I know it is simple but I am really struggling with my basic vba knowledge to correctly iterate through 2 columns.
A
B
5
8
8
4
2
9
9
9
For example say my "cutoffs" are a value greater than 2 for numbers in column A and a value greater than 5 for numbers in column B. The program then should return me a value of 2 for this example.
Option Explicit
Function cutoff(phi_cutoff As Double, sw_cutoff As Long, gross As Double)
Dim count As Double
Dim my_range1 As Range
Dim my_range2 As Range
Dim c1 As Range
Dim c2 As Range
Dim value1 As Double
I currently have the following macro - as you can see it is quite repetitive. I know it is possible to do this using a loop any ideas on how to do this would be appreciated.
Sub Clean()
Rows("4:4").Select Selection.Delete Shift:=xlUp
Rows("5:5").Select Selection.Delete Shift:=xlUp
Rows("6:6").Select Selection.Delete Shift:=xlUp
Rows("7:7").Select Selection.Delete Shift:=xlUp
I thought that this would work - but not so probably some basic syntax error:
Sub Runny() For x = 4 To 17 Rows("x:x").Select Selection.Delete Shift:=xlUp Next x End Sub
I have a triple loop I would like to create a triple loop code at the moment
m = Worksheets("txt"). Range("AM5").Value
n = Worksheets("txt").Range("AM1").Value o = Worksheets("txt").Range("AM2").Value p = Worksheets("txt").Range("AM3").Value q = Worksheets("txt").Range("AN1").Value r = Worksheets("txt").Range("AN2").Value s = Worksheets("txt").Range("AN3").Value t = Worksheets("txt").Range("AO1").Value u = Worksheets("txt").Range("AO2").Value v = Worksheets("txt").Range("AO3").Value
Worksheets("txt").Select For myRow = 1 To m myCol = 5 Worksheets("txt").Cells(myRow, myCol).Select With Selection If .Value = "" Then .Value = n Else: .Value = .Value End If End With Next myRow
I would like to extract and compile the information contained in these cells: a5,c5,a6,c6,c7,a14,g14,e16,g16,e18,i18,a20,g20,h22,j22,h24,l24 all the sheets.
New sheet would contain the information of each sheet eg. Column A2= file name
And Row B2 to R2 or whatever will be the corresponding cells mentioned above for each cell. The code I have only brings back the file name but only a5 from the range but not the rest.
VB: Sub MergeAllWorkbooks() Dim SummarySheet As Worksheet Dim FolderPath As String Dim NRow As Long Dim FileName As String Dim WorkBk As Workbook Dim SourceRange As Range
I started with the following formula: =COUNTIF($D2:$D5694,D5724) I then copied it to the next line: =COUNTIF($D3:$D5695,D5725)
That's not supposed to happen if you use the $ sign. So I checked Tools > Options > calculation. That is set to automatic. What else could cause my problem?
I want to iterate a For Loop changing one variable each iteration until the Total from the For Loop equals zero. Each iteration of the Do Loop keeps the previous Total from the For Loop and adds it. How do I reset the For Loop to start from Total = 0 at the beginning of the For Loop?
I would like to start with a spending value, (SV), that would be the amount of money that I want to spend in a year, not counting taxes. The function (or functions) would then go through some sort of iterative calculation process that would compute the total spending level including taxes, (TS), the federal tax, (FT), and the state tax, (ST).
These values would have the relationship that TS=SV+FT+ST. When the iteration was finished, the federal tax and state tax amounts would be consistent with the taxes that would be due for this level of total spending as determined by the normal tax tables for both federal and state taxes.
I am not concerned that the tax figures be absolutely exact, so a lot of iteration is probably not necessary.
I'm looking for something one step deeper than a countif. I need something that will tell me what iteration number it actually is within the countif, in relation to date, as shown.
Name Date Iteration #
[Code]....
Any formula that could be used to do something like this?
I have a written a small sub to run solver. However, I need to run a secondary sub at each solver iteration. The secondary sub I want to run is called “BypassGoal”. My code currently looks like:
Sub OperatingPt() Dim WrkBk As Workbook Dim WrkSht1 As Worksheet Set WrkBk = ThisWorkbook Set WrkSht1 = WrkBk.Worksheets("Engine") Dim Mode As String Dim CellTarget As String Dim CellRange As String WrkSht1.Activate WrkSht1. Cells(30, 2).Value = 2 CellTarget = "H14" CellRange = "B30" SolverReset SolverOptions Iterations:=3000, Precision:=0.000001, AssumeNonNeg:=True, Derivatives:=2, StepThru:=True ....................
I had 4 initial temperature values and after some time i got new temperature values. Now for calculating 2nd iteration i need to use new calculated values and for third iteration values from 2nd iteration is to be used and so on.
How to calculate temperature values directly for 10th iteration without solving each iteration?
how can i make a loop throught the G colum and to check if the cell before the currect is bigger/ lower or equal to the currect cell. and if its higher then an arrow will be shown '^' if its lower an upside down arrow will be show 'v' equal will show -> arrow. example:
G2 = 200 G3 = ^ 300 G4 = -> 300 G5 = v 209
so how can i change the G3 to G4 G5 etc.. in a loop in the: Range("G3").Select command? and i need to change the
.Value = "=$G$2" to Value = "=$G$3" Value = "=$G$4" etc... in 2 places................
i have this code that upon opening of workboot it generates the next number thats available on sheet 2 i then enter the data that i need to and click the button it then transfers to sheet2 and prints 2 copies and then clears the cells that i have input data to
what i really need it to do is exactly as it does right now but to then change the next number available to what it is ....
I seem to be having trouble creating a nested loop. It seemed simple enough in my brain holder, however, in real life...Well I am here aren't I. Here is my
First of all, I'm completely new to both this forum and VBA. I have just done a programming course in java before. I hope you will forgive me if this have already been posted. No to my problem.
I am tryring to use different projected values to project other subparts of those units, which is done in the sheet mean needed weekly. Tje values are taken from projections. The answer that I get from mean needed weekly should be copied to a third sheet.
This is the code that I am using right know, but I cant get the for loop to work (or more exactly to be able to place the "i" in my text without making compilation errors).
One of the multitude of areas where I still need drastic improvement is with efficiency. Specifically, avoiding loops.
Are any of you aware of any good, basic level sites, blogs, posts, etc., about better methods or processes? I'm not particularly experienced with programming (I've been a software TESTER for 13 years and just started some programming in recent months), so the more basic the better.
I have recorded a macro that cleans up my data that I receive from an external source and inserts two columns.
I recorded a seperate macro to execute an "if formula" that evaluates Column A and B and if A is blank return the value in B, else return the value in A. I wrote the formula =if(a1=" ",b1,a1).
I referred to the text I got in the class and found that I can use a "Next Loop". I added to the formating macro the Next Loop critera and instead of inserting text I inserted the above formula.
It ran and returned data, however it was not in the column I intended, it cascaded off to the side of my data in a step down (kinda like stairs).
I need to maintain a KPI in excel2000 which details weekly progress, I normally do this using some condition formatting and hiding cells, I have to be honest its not pretty.
What I would like to do is to use the colorindex function to show the wk breakdown status and a Do loop on my target range, I think!...... I've a basic example below,
If I could show the benefits to my boss he might even let me take a VBA course.....
I'm trying to better understand loops and variables. I have an instance I'd like to try and implement but I'm struggling with it. I know how to set a variable as 1 and loop through the code increasing the number each time but this one is a little different.
At the start of my code, I scan the worksheets for the worksheet names -
Monday Tuesday Wednesday etc etc
and then assign these worksheets as
Monday = ws Tuesday = ws2 Wednesday = ws3 etc etc
That way in my code I can do things to the workbooks like -
ws.Range("D4").Value = "blah blah" and always know it will be tackling the right worksheet regardless of the sheets position in the workbook or sheetcode.
Well I have lots of changes to make on each of these sheets (Monday -> Sunday) which are identical. What I'd like to do is something like this -
For each VARIABLE in (MY VARIABLES WS,WS2,WS3 ETC) VARIABLE.Range("D4").Value = "blah blah" Next VARIABLE
Is it possible to use a variable to change a controls name in a For Loop? I have several worksheet combo boxes that have the same root name, e.g., cbobox1, cbobox2. cbobox3, etc. I want to be able to keep the main name and change the numeric part in a for loop and also perform some function with the control. For example:
For x = 1 To 10 Worksheets("Name").cbobox(x).Clear Next x
Is it possible to do 2 loops in one procedure? I want to copy and paste formula the cells in one column and again in another column so I did two Do Loops. It only ran for the first loop but not on the second, although they have almost the same code (I just edited the column from C to I). Please see below
I want to find some code that will search down a column until it finds one of a number of different words. when it finds one of the words it stops and deletes the row on which the word is contained. after deleting the data in the row, it continues searching down the column until it finds another of the words.
for example......see work sheet attached,
I want some code that will search down column B until it finds Sally or Robyn. As soon as it finds either of these names it stops and deletes all the data in that row and then continues searching down column B to find Sally or Robyn again........
Is using a loop the best way to go with this?
MOD: thread moved to parent Programming forum (VBA)
How I can control many loops directly after each other. For example I have this script:
If (Cells(x, column) > 35 And Cells(r, column) < 25 Then
If (cells(x, 75) = "GotU") Then xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Else
If (cells(x, 85) = "GotYah") Then xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Else
If (cells(x, 95) = "GotYou") Then xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx End if
Is this script correct if I want the loops to check all data that I write (for ex. GotYou, GotYah etc.) or do I need to end every if before next if? I want that the loops makes all 3 controls.
On my sheet in Range("a4:a20") I have the names of the "Named Ranges" that I set referenicng ranges on different worksheets. Range("b4:b20") I count the # of rows for each of my "Named Ranges." I need a loop that loops through the values in column "B" and if it is > 0 then to take the corresponding "Named Range" in column "A" and paste it starting in cell "E6".
This is the start to my code it isn't working
Dim CellV As Range 'i want this to be the Cell Value Dim CellValue_Range As Range 'this is the range B4:B20 Dim NRange As Range 'This is range A4:a20 lastrow = Range("a1").Value 'this counts the number of named ranges I am dealing with
For Each CellV In CellValue_Range If CellV.Value > "0" Then Range("a4").Value.Copy 'this range "a4" should change one row down each time it loops (this also correspondes to my "NRange" Range("e" & lastrow).Select Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _ xlNone, SkipBlanks:=False, Transpose:=False End If Next CellV
I have a column of 96 numbers (observations every 15 minutes for a 24 hour period), and I want to take the average of these numbers in groups of four (the hourly average). My data starts in cell A2 and goes to cell A97. My first batch of averages are labeled as follows:
For i = 10 To 260 Step 10 iStrg = i cntrl = "A" & iStrg Range(cntrl).Value = i Next i
I have been trying to use this code which displays 10 in A10, 20 in A20, 30 in A30....etc. What i am trying to do is display 0 to 260 with a step of 10 in the cells A2 to A28, could anyone be of assistance?