If I have a for/next loop, but have a column splitting up the range I need to apply the array print to, how would I go about splitting that?
Example:
For i = 0 To 4 '5 rows
For j = 3 To 5 'apply across 3 columns
Set c = wsA.Cells(fndRow + i, j + 43)
c.Interior.Color = VBA.RGB(255, 255, 0)
wsA.Cells(fndRow + i, j + 43).Value = rIterator.Offset(, j - 1).Value * multplr(i)
Next j
Next I
I want to split my array print between 3 columns skip over a column and then the next three columns like:
For j = 3 to 5, 7 to 9
I know this isn't the correct syntax, but it conveys my point in the question. Any thoughts? Would I need to create another for loop?
For j = 3 to 9
if j<>6 Then
'do stuff
end if
next j
Related
Sub Makro1()
For i = 3 To 8 'row 3 to row 8
For j = 34 To 36 'column 34 to 36
Cells(j, 7 + i) = Cells(j, i)
Next j
Next i
For i = 3 To 8
For j = 38 To 40
Cells(j, 7 + i) = Cells(j, i)
Next j
Next i
End Sub
The logic in both double for loops is the same. i am copying every cell from row 34 to 36 and column 3 to column 8. I place this block of cells one column next to it. So it looks like this:
Now i want to repeat this process many times, because there are many other data blocks like this in my sheet. the next block starts at row 38 to 40, the next one 42 to 44 and so on. So there is always one row distance between the blocks. How can i loop though this, anybody got an idea?
I think your column/row comments in your question might be the wrong way around, but I have tried to write something that should do what I think you want:
Sub test()
Dim ofst As Long, iterations As Long, stepchange As Long
iterations = 3 ' loop three times - change as required
stepchange = 4 ' perform the copy every 4 rows
With ActiveSheet
For ofst = 1 To iterations * stepchange Step stepchange
With .Range(.Cells(34 + ofst - 1, 3), .Cells(36 + ofst - 1, 8))
.Offset(, 7).Value = .Value
End With
Next
End With
End Sub
Right now I have a working formula
=IF(OR(COUNTIF(C2,"*"&$O$2:$O$13&"*")), "Yes", "")
It prints "Yes" if a string in column C matches a string in a list in column O.
However, I now have 2 lists, list A and list B, and now I need to be able to print "No" if a cell in column C matches any of the strings in list B, and if in neither list A or B, to print something like "NA".
I haven't been able to figure out the formula further than just the one list. Any help is appreciated.
I threw together a painfully simple version of what I am getting at, where "Jersey Color" is being populated by the formula, and "Team Red/Blue" are the lists.
Use below formula in B2 cell.
=IF(ISNUMBER(MATCH(A2,C:C,0)),"RED",IF(ISNUMBER(MATCH(A2,D:D,0)),"BLUE","NA"))
This loops through all the players and finds if they are matched to a team.
Cells.Clear
'data
For i = 1 To 20
Cells(i, 1) = Chr(64 + i)
Next i
For i = 1 To 20 Step 4
Cells((i + 3) / 4, 3) = Chr(64 + i + Int(Rnd * 2))
Cells((i + 3) / 4, 4) = Chr(66 + i + Int(Rnd * 2))
Next i
'code
Dim Players As Range, TeamA As Range, TeamB As Range
Set Players = Range("A1:A20")
Set TeamA = Range("C1:C5")
Set TeamB = Range("D1:D5")
Players.Offset(0, 1) = "N/A"
Players.Offset(0, 1).Font.Bold = True
Dim member As Range, player As Range
For Each player In Players
For Each member In TeamA
If member = player Then
player.Offset(0, 1).Interior.Color = RGB(255, 100, 100)
player.Offset(0, 1) = "RED"
End If
Next member
For Each member In TeamB
If member = player Then
player.Offset(0, 1).Interior.Color = RGB(100, 100, 255)
player.Offset(0, 1) = "BLUE"
End If
Next member
Next player
Output:
I'm pretty new to programming,and I have a university task where I'm need to calculate if person have more than 40 hours in week,If Yes then In row(H3:K3) need to be written about that.(each cell=1 week)
But I dont know how to change row after reaching K3 position.
So I only can check one person out of 5.
Please can somebody help me with that..
Thanks
Screenshot
VBA
Sub ssda()
x=3
i=2
j=8
Do
x=x+1
For i = 2 To 5
if Cells(x, i) > 40 Then
Cells(x, j) = "Ir parstrade"
j = j + 1
Else
Cells(x, j) = "Nav parstrades"
j = j + 1
End If
Next
Loop Until x=x+1
End Sub
Im need to all 5 persons have answer if they worked more than 40 hours.
It need to take numbers from cell(B3:D3) if there is more than 40,then In row(H3:K3) should be "Good" otherwise "Bad",afther that need to check next person.
This is untested, but I think it should be right:
First of all, you would have to reset j to 8 for each person.
But you can also use .Offset from a cell (6 cells to the right from each number), this seems easier to me.
Sub ssda()
x=3
' i=2 not needed
' j=8 wrong here
Do
' x=x+1 wrong here, if you want to start in row 3, not 4,
' in the first round. Put this at the end of the loop
' j = 8 would be ok here
For i = 2 To 5
if Cells(x, i) > 40 Then
'Cells(x, j) = "Ir parstrade"
' alternative: just use offset
Cells(x, i).Offset(0, 6) = "Ir parstrade"
Else
Cells(x, i).Offset(0, 6) = "Nav parstrades"
'Cells(x, j) = "Nav parstrades"
End If
j = j + 1
Next
x = x + 1
'Loop Until x=x+1 - this can never be true
Loop Until Cells(x, 1) = ""
End Sub
Hi I am trying to compare two sets of data by having indicators if they increased, decreased, or stayed the same. I was able to get it working on one column. My problem is I can't loop it on multiple columns.
Basically:
If A1 = C1 then D1.Value = 0
If A1 > C1 then D1.Value = 1
If A1 < C1 then D1.Value = 2
I've tried to do the "do while" to add increments on the columns but it did not work.
Sub ChangeIndicator2()
Dim i As Long
Dim a As Long
Dim b As Long
Dim x As Long
Dim y As Long
Dim ProgramCount As Long
i = 2
a = 8
b = 2
x = 0
y = 8
ProgramCount = 12
Do While y <= ProgramCount
For Each c In Worksheets("Sheet1").Range("A2:A20").Offset(x, y)
If Worksheets("Sheet1").Cells(i, a).Value = Worksheets("Sheet1").Cells(i, b).Value Then
c.Value = 0
ElseIf Worksheets("Sheet1").Cells(i, a).Value < Worksheets("Sheet1").Cells(i, b).Value Then
c.Value = 1
ElseIf Worksheets("Sheet1").Cells(i, a).Value > Worksheets("Sheet1").Cells(i, b).Value Then
c.Value = 2
End If
i = i + 1
Next c
a = a + 2
b = b + 2
y = y + 2
Loop
End Sub
Only the first column works, the second column only shows 0 values.
So basically, what you want to do is compare 2 columns which are 2 columns apart and repeat that on another pair of columns which is 8 columns from the first column. If my assumption is correct then have a go at this:
For i = 0 To (ProgramCount * 8) Step 8
With Worksheets("Sheet1").Range("A2:A20").Offset(, i + 3)
.FormulaR1C1 = "=IF(RC[-3]=RC[-1],0,IF(RC[-3]>RC[-1],1,2))"
.Value2 = .Value2
End With
Next
Adjust the offset to suit your needs (I may have misunderstood the actual columns you target to update). Hope this helps.
I am looking for a way to repeat a set of cells horizontally a certain number of times before moving on to the next set of cells. For example:
If I have this in 3 columns:
5 4 3
0 1 2
and I have 3 columns which dictate how many times I want the values iterated:
4 2 3
This function should give me this when dragged over a range:
5 5 5 5 4 4 3 3 3
0 0 0 0 1 1 2 2 2
Does anyone know the best manner to do this?
I have been using some convoluted reasoning to get through this with an array formula ( has the "{}" brackets around it and you have to use Shift+Enter). I am using SUMIF and COUNTIF functions to do some things, but it never really works out.
Here's a hacky VBA solution. This assumes the "repeat counts" are on the first row (4, 2, 3) and the "values to repeat" are on the second row (5, 4, 3, 0, 1, 2).
Sub outputRepeatedValues()
Dim xStart As Integer, yStart As Integer
Dim i As Integer, j As Integer
Dim valueToRepeat As Integer, numTimesRepeat As Integer
Dim xOffset As Integer, yOffset As Integer
xStart = ActiveCell.Column
yStart = ActiveCell.Row
i = 1
j = 1
xOffset = 0
yOffset = 0
While Cells(2, j) <> ""
If Cells(1, i) = "" Then
i = 1
yOffset = yOffset + 1
xOffset = 0
End If
numTimesRepeat = Cells(1, i)
valueToRepeat = Cells(2, j)
For k = 1 To numTimesRepeat
Cells(yStart + yOffset, xStart + xOffset) = valueToRepeat
xOffset = xOffset + 1
Next k
j = j + 1
i = i + 1
Wend
End Sub
Stick this in a new module. To use this code, select the cell representing the upper left corner of the output region. Then press Alt+F8 to bring up the Macro box, and then you can run the macro.