so basically my code goes like this:
Sub clor2()
For j = 0 To 10
For i = 0 To 10
if i mod = 0 and and j mod=0 then
[D2].Offset(j, i).Interior.ColorIndex = 37
Else
[D2].Offset(j, i).Interior.ColorIndex = 36
End If
Next i
Next j
End Sub
The mod operator is used like this 5 mod 2 = 1 and 6 mod 2 = 0.VBA equivalent to Excel's mod function.
Thus, it should be i Mod SomeNumber = 0 for the condition.
Option Explicit
Sub clor2()
Dim j As Long, i As Long
For j = 0 To 10
For i = 0 To 10
If i Mod 3 = 0 And j Mod 7 = 0 Then
[D2].Offset(j, i).Interior.ColorIndex = 37
Else
[D2].Offset(j, i).Interior.ColorIndex = 36
End If
Next i
Next j
End Sub
Option Explicit is also a good practice in VBA - thus your variables i and j should be declared. - VBA: problems with defining variables in function if Option Explicit is used
Related
For j = 0 To 20
For i = 0 To 20
ReDim atoms(0 To 20, 0 To 20)
ReDim atomschange(0 To 20, 0 To 20)
atomschange(j, i) = 0
If i Mod 2 = 0 And j Mod 2 = 0 Then
[B2].Offset(j, i).Interior.ColorIndex = 37
atoms(j, i) = 1
Else
[B2].Offset(j, i).Interior.ColorIndex = 36
atoms(j, i) = 0
End If
Next i
Next j
End Sub
what am i doing wrong?
You are redim'ing your arrays inside a loop which reinitializes, then dropping any data inside the array. Use the PRESERVE keyword to insure you don't dump your array when redimensioning it ReDim Preserve atoms(0 To 20, 0 To 20)
Redimensioning your arrays to the exact same size makes no sense. They are already 0 To 20, 0 To 20 because you already set that. Redim them once outside of your for loops and then leave them alone.
Insure that your atoms and atomschange arrays are declared properly at the top of your code. It's complaining (probably when you redim them) that those two variables aren't arrays.
You know the size of the arrays before this code executes, so declare them and dim them properly in the declaration, then just use them (removing the superfluous and incorrect use of redim in your loop:
Sub somesumroutine()
Dim atoms(0 to 20, 0 to 20) as Integer
Dim atomschange(0 to 20, 0 to 20) as Integer
For j = 0 To 20
For i = 0 To 20
atomschange(j, i) = 0
If i Mod 2 = 0 And j Mod 2 = 0 Then
[B2].Offset(j, i).Interior.ColorIndex = 37
atoms(j, i) = 1
Else
[B2].Offset(j, i).Interior.ColorIndex = 36
atoms(j, i) = 0
End If
Next i
Next j
End Sub
Consider using UBound and LBound of your atoms array to set the loops so you don't have to use the 0 to 20 over and over again. This way you can dim your arrays once and leave the rest of your code alone:
Sub somesumroutine()
Dim atoms(0 to 20, 0 to 20) as Integer
Dim atomschange(0 to 20, 0 to 20) as Integer
For j = lBound(atoms, 1) To uBound(atoms, 1)
For i = lBound(atoms, 2) To uBound(atoms, 2)
atomschange(j, i) = 0
If i Mod 2 = 0 And j Mod 2 = 0 Then
[B2].Offset(j, i).Interior.ColorIndex = 37
atoms(j, i) = 1
Else
[B2].Offset(j, i).Interior.ColorIndex = 36
atoms(j, i) = 0
End If
Next i
Next j
End Sub
so I am getting errors for some reason "next without for"
here is the code:
Sub test()
Dim y As Integer
y = 0
For i = 1 To 7
For j = 1 To 7
If Cells(i, 1) = Cells(j, 1) Then
y = y + 1
Next j
Cells(i, 2).Value = y
y = 0
Next i
End Sub
The problem doesn't come from your For ... To ... Next but from your If condition that you forgot to close with the End If instruction.
Sub test()
Dim y As Integer
y = 0
For i = 1 To 7
For j = 1 To 7
If Cells(i, 1) = Cells(j, 1) Then
y = y + 1
End If 'You forgot to end the condition
Next j
Cells(i, 2).Value = y
y = 0
Next i
End Sub
So I've this challenge. But first a little information for you: p goes from 1 to 24.
When p is 1 and Cells(X,4)>0 then I want to store all the values when this is true (it might be true several times, while it loops down the 2000 rows) into a value in Worksheets("myvalues").Cells(6,11).Value = a.
After this, I want p to be 2 and then do the same, and store the sum of these into Worksheets("myvalues").Cells(7,11).Value = a.
And so on until p is 24 (incl. 24)
Option Explicit
Sub main()
Dim x As Integer
Dim rowshift As Integer
Dim a As Double
Dim b As Integer, p as Integer
For x = 2 To 2000
If Worksheets("DATA").Cells(x, 2) = p And Worksheets("DATA").Cells(x, 4) > 0 Then
a = a+ Worksheets("DATA").Cells(x, 5)
p=p+1
End If
For rowshift = 6 to 29
Worksheets("myvalues").Cells(rowshift, 11).Value = a
Might this do what you intend?
Sub Main()
Dim TargetRow As Long
Dim Spike As Double
Dim p As Integer
Dim R As Long
TargetRow = 6
For p = 1 To 24
For R = 2 To 2000
With Worksheets("DATA").Rows(R)
If (.Cells(2).Value = p) And (.Cells(4).Value > 0) Then
Spike = Spike + .Cells(5).Value
End If
End With
Next R
Worksheets("MyValues").Cells(TargetRow, 11).Value = Spike
Spike = 0
TargetRow = TargetRow + 1
Next p
End Sub
To obtain the equivalent of Excel's formula
=SUMIFS(Data!$E$2:$E$2000,Data!$B$2:$B$2000,ROW()-5,Data!$D$2:$D$2000,">0")
using VBA, I suggest you change your code to have a loop within a loop
Option Explicit
Sub main()
Dim x As Long
Dim rowshift As Long
Dim a As Double
For rowshift = 6 to 29
a = 0
For x = 2 To 2000
If Worksheets("DATA").Cells(x, 2) = rowshift - 5 And _
Worksheets("DATA").Cells(x, 4) > 0 Then
a = a + Worksheets("DATA").Cells(x, 5)
End If
Next
Worksheets("myvalues").Cells(rowshift, 11).Value = a
Next
End Sub
I think you need to set p value.
Please try this full code.
Option Explicit
Sub main()
Dim x As Integer
Dim rowshift As Integer
Dim a As Double
Dim b As Integer, p as Integer
p = 1
For x = 2 To 2000
a = 0
If p < 25 Then
If Worksheets("DATA").Cells(x, 2) = p And Worksheets("DATA").Cells(x, 4) > 0 Then
a = a+ Worksheets("DATA").Cells(x, 5)
p=p+1
End If
End If
If a > 0 Then
For rowshift = 6 to 29
Worksheets("myvalues").Cells(rowshift, 11).Value = a
Next rowshift
End If
Next x
Im having a trouble understanding For and Do while.
Im trying to put items in a listbox in this order:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
and so on.
And the same way around
1
1 2
1 2 3
Up intil 10
How should i attack this issue?
This is what im stuck with:
Dim counter1 As Integer
Dim counter2 As Integer
For counter1 = 10 To 1 Step -1
For counter2 = 1 To 10
ListBox1.Items.Add()
Next counter2
Next counter1
Your problem is not with using the Loops alone but with using the ListBox Methods. You can achieve what you want in 2 ways:
Initially concatenate (connect) the numbers into a single string and then add it on the Listbox using AddItem Method.
Populate a multicolumn ListBox using a nested loop using AddItem and List method.
The first one should be something like below:
Private Sub CommandButton1_Click()
Dim i As Integer, j As Integer, k As Integer
Dim s As String
k = 11 ' this determines the exit condition
For i = 1 To 10
For j = 1 To 10
If k = j Then Exit For
s = IIf(s = "", j, s & " " & j) ' construct the string
Next
Me.ListBox1.AddItem s ' add in listbox
s = ""
k = k - 1
Next
End Sub
Which will result to:
Edit1 No.2 Above
Private Sub CommandButton1_Click()
Dim i As Integer, j As Integer, k As Integer
With Me.ListBox1
.ColumnCount = 10
.ColumnWidths = "15;15;15;15;15;15;15;15;15;15"
k = 10 ' determines exit condition
For i = 1 To 10
For j = 1 To 10
If j = 1 Then
.AddItem j ' if it is the number 1, use AddItem method
Else
.List(.ListCount - 1, j - 1) = j ' else use the List method
End If
If k = j Then Exit For
Next
k = k - 1
Next
End With
End Sub
This time, we do not add concatenated numbers into the ListBox but we add it 1 by 1 in each row and column of a multicolumn ListBox. Result would be:
I leave the ascending numbers to you. :) I hope this gets you going.
I want to copy some columns of an excel sheet to another sheet.
I have written the code, which doesnt work. It gets into an infinite loop, and exits with an error. The code is:
Sub customCopy()
Dim i As Integer
Dim j As Integer
j = 1
For i = 1 To 700
If i Mod 5 = 2 Then
Columns(i).Copy Destination:=Sheets(2).Rows(j)
j = j + 1
End If
If i Mod 5 = 3 Then
Columns(i).Copy Destination:=Sheets(2).Rows(j)
j = j + 1
End If
If i Mod 5 = 4 Then
Columns(i).Copy Destination:=Sheets(2).Rows(j)
j = j + 1
End If
Next i
End Sub
Please help..
Copying a column into a row won't work.............a row is too small!