I'm trying to paste "Area1" 50 times, and "Area2" 50 time right below "Area1" in the same column. My codes run the first 50 times, and replace the first 50 rows when running Area=2. How do I make "Area2" start on a new row, under "Area1"? Thank you :))
For Area = 1 To 2
For Row = 1 To 50
Sheets("A").Cells(Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
There are many ways to do this. A few of them would be:
For Area = 1 To 2
For Row = 1 To 50
Sheets("A").Cells((Area - 1) * 50 + Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
or
For Area = 1 To 2
For Row = (Area - 1) * 50 + 1 To (Area - 1) * 50 + 50
Sheets("A").Cells(Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
or changing Area to be zero-based
For Area = 0 To 1
For Row = 1 To 50
Sheets("A").Cells(Area * 50 + Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
or avoiding loops
Sheets("A").Cells(1, 2).Resize(50, 1) = Sheets("A").Cells(2, 31)
Sheets("A").Cells(51, 2).Resize(50, 1) = Sheets("A").Cells(2, 31)
or, because you are copying the same value to the destination cells, simply
Sheets("A").Cells(1, 2).Resize(100, 1) = Sheets("A").Cells(2, 31)
One way to do this is creating another variable and increment it, in second loop, each time +1. Like this:
For Area = 1 To 2
For Row = 1 To 50
myRow = myRow + 1
Sheets("A").Cells(myRow, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
Related
While running this code to randomize some values I find that the all the cells have the same number. I believe I'm missing a standard concept but I can't seem to wrap my head around the issue.
Randomize
LWeekDay = Int((400 - 150 + 1) * Rnd + 200)
LWeekEnd = Int((600 - 200 + 1) * Rnd + 200)
For Y = 3 To 10
For X = 2 To 8
Cells(Y, X) = LWeekDay
Next X
Next Y
You are currently only calculating LWeekDay once, and then using that value for every cell.
I assume you want to assign a new value every time through the loop:
Randomize
For Y = 3 To 10
For X = 2 To 8
LWeekDay = Int((400 - 150 + 1) * Rnd + 200)
LWeekEnd = Int((600 - 200 + 1) * Rnd + 200)
Cells(Y, X) = LWeekDay
Next X
Next Y
I am experiencing a problem with the outputs from my loop. As the sub is running I can see that the results from the final IF statement are being overwritten by the results from the second one. My code is structured as follows:
for i = 1 to 5
for j = 1 to 50
for each events.value in eventArray
if events.value = arrayElem then
if cells(i,j).value = "x" then
type = "col1"
elseif cells(i,j).value = "y" then
date = "col2"
elseif cells(i,j).value = "z" then
num = "col3"
end if
count = count + 1
activeworkbook.worksheets("output").cells(count + 1, 1) = type
activeworkbook.worksheets("output").cells(count + 1, 2) = date
activeworkbook.worksheets("output").cells(count + 1, 3) = num
end if
next arrayElem
if cells(i,j).value = "a" then
name = "row1"
elseif cells(i,j).value = "b" then
size = "row2"
elseif cells(i,j).value = "c" then
height = "row3"
end if
activeworkbook.worksheets("output").cells(count + 2, 1) = name
activeworkbook.worksheets("output").cells(count + 2, 2) = size
activeworkbook.worksheets("output").cells(count + 2, 3) = height
next j
next i
Obviously these are dumby variables and results, but the overall structure is the same as the real code. I can see "name","size", and "height" being printed, but then they get replaced by "type", "date", and "num". How do I prevent this from happening? Each time a new event is found I need it to print its associated characteristics printed into a new row in the "output" sheet.
Consider the following simplified version of your code:
For i = 1 To 100
If x = y Then
rowNum = rowNum + 1
Cells(rowNum + 1, 1) = "A"
End If
Cells(rowNum + 2, 1) = "B"
Next
Each time through the loop you are writing out either one or two things (two if x = y is true, one if it isn't) but you are only incrementing the row number by zero or one (one if x = y is true, zero if it isn't). Even if you know that x will always equal y, you are still trying to write two rows of information out but only increasing the row counter by one.
Assuming you are not trying to replace the "B"s in my example with the "A"s from the next iteration through the loop, you should change the code to something like:
For i = 1 To 100
If x = y Then
rowNum = rowNum + 1
Cells(rowNum, 1) = "A"
End If
rowNum = rowNum + 1
Cells(rowNum, 1) = "B"
Next
I have a column in excel full of numbers, like this:
1
A 100
B 200
C 300
D 400
E 500
F 600
G 700
H 800
I 900
J 1000
K 1100
etc
etc
I formatted the column with a macro so that it highlights the top 10 %. Unfortunately, I have to work with it now, as in I have to have a piece of code that says: ok, grab the first cell that is in the top 10 % (say it's K1). Get K1 and copy it somewhere else.
Question:
How do I point to an element in the top 10 %? How do I tell VBA "Grab the first top 10% value (K1), then grab that second top 10% value?
Many thanks
Copy Top 10% of values from each Column to a different Worksheet
Sub ProcessColumns()
Dim i As Integer, j As Integer, rowCount As Long
Dim minvalue As Double, largeValue As Double
Dim arr
With Worksheets("Source")
arr = Sheet1.UsedRange.Value
For j = 1 To UBound(arr, 2)
minvalue = WorksheetFunction.Percentile(.Columns(j), 0.9)
For i = 2 To UBound(arr, 1)
largeValue = WorksheetFunction.Large(.Columns(j), i - 1)
If largeValue >= minvalue Then
arr(i, j) = largeValue
rowCount = i
Else
Exit For
End If
Next
Next
End With
Worksheets("Target").Range("A1").Resize(rowCount, UBound(arr, 2)).Value = arr
End Sub
I have a nested for loop that is looking to take values in one row, generate a value based on an equation, then do the same for many rows following the first one, all while adding the value together.
Essentially, if row one has a value of 15, and row 2 and 3 return values of 10 and 12, the variable storing the total value (named genCost) will be 37.
I want to place the summed total values of genCost in a new sheet, separated by day, but when I run the code I get a Run-time 1004 error. I realize that this has something to do with the sheet that I am working on, and the sheet that I am trying to place the values into (the 2nd to last line in the code).
I understand my code may be ugly and simple, but can somebody help me troubleshoot this?
'Nested For loop for ALL OTHER DAYS of genCost...only 1 formula
For j = 2 To dayNumber
For i = 1 To increments
'IF(AND(U7=1,U6=0),R7,0)
If Cells(rowValue, 21) = 1 And Cells(rowValue - 1, 21) = 0 Then
ifValue = Cells(rowValue, 18)
Else
ifValue = 0
End If
'calculate value variable with second half of equation
value = (((Cells(rowValue, 23) * Cells(rowValue, 16) * (1 / 6)) + (Cells(rowValue, 25) * Cells(rowValue, 17) * (1 / 6)) + (Cells(rowValue, 21) * Cells(rowValue, 19) * (1 / 6)) + ifValue))
genCost = genCost + value
'set value and ifValue back to zero and step down one row and do again
value = 0
ifValue = 0
rowValue = rowValue + 1
Next i
Cells(3, j) = genCost
Dim genCostRefNum As Integer: genCostRefNum = 6
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4)) = genCost
genCost = 0
Next j
Instead of this
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4)) = genCost
write this
ThisWorkbook.Sheets(1).Cells(genCostRefNum, 4) = genCost
There is a default property of Cells statement, which is Value (same for Range). So your code was exactly
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4).Value).Value = genCost
and after executing Cells something like:
ThisWorkbook.Sheets(1).Range(6).Value = genCost
It's good practice to always write complete statements and don't rely on default properties.
I have two sets of data that need to be matched based on IDs and timestamp (+/- 3 units converted from time), and below is the formula that I've been using in Excel to do the matching. Recently I've had to run this formula on up to 1 million rows in Excel, and it takes a REALLY long time, crashes too. I'm wondering if there is a faster way to do this, if not in Excel?
=INDEX(A:A,MATCH(1,--(B:B=E3)*--(ABS(C:C-F3)<=3),0),1)
Data Set 1:
Column A: States
Column B: IDs
Column C: Timestamp
Data Set 2:
Column D: Email Addresses
Column E: IDs
Column F: Timestamp
Column G: =INDEX(A:A,MATCH(1,--(B:B=E3)*--(ABS(C:C-F3)<=3),0),1)
Goal: Append "States" Column to Data Set 2 matched on IDs and Timestamp (+/- 3 time units) match.
Just don't know how to run this formula on very large data sets.
Place the following VBA routines in a standard code module.
Run the MIAB1290() routine.
This emulates the precise outcome of your INDEX/MATCH formula, but it is much more efficient. On my computer, a million records are correctly correlated and the results displayed in Column G in just 10 seconds.
Public Sub MIAB1290()
Dim lastB&, k&, e, f, z, v, w, vErr, r As Range
With [a2]
Set r = .Resize(.Item(.Parent.Rows.Count - .Row + 1, 5).End(xlUp).Row - .Row + 1, .Item(, .Parent.Columns.Count - .Column + 1).End(xlToLeft).Column - .Column + 1)
lastB = .Item(.Parent.Rows.Count - .Row + 1, 2).End(xlUp).Row - .Row + 1
End With
With r
.Worksheet.Sort.SortFields.Clear
.Sort Key1:=.Item(1, 2), Order1:=1, Key2:=.Item(1, 2), Order2:=1, Header:=xlYes
v = .Value2
End With
ReDim w(1 To UBound(v), 1 To 1)
vErr = CVErr(xlErrNA)
For k = 2 To UBound(v)
e = v(k, 5)
f = v(k, 6)
w(k, 1) = vErr
z = BSearch(v, 2, e, 1, lastB)
If z Then
Do While v(z, 2) = e
If Abs(v(z, 3) - f) <= 3 Then
w(k, 1) = v(z, 1)
Exit Do
End If
z = z + 1
If z > UBound(v) Then Exit Do
Loop
End If
Next
r(1, 8).Resize(r.Rows.Count) = w
End Sub
Private Function BSearch(vA, col&, vVal, ByVal first&, ByVal last&)
Dim k&, middle&
While last >= first
middle = (last + first) / 2
Select Case True
Case vVal < vA(middle, col)
last = middle - 1
Case vVal > vA(middle, col)
first = middle + 1
Case Else
k = middle - 1
Do While vA(k, col) = vA(middle, col)
k = k - 1
If k > last Then Exit Do
Loop
BSearch = k + 1
Exit Function
End Select
Wend
BSearch = 0
End Function
Excel isn't really made for large ammount of data, and probably no code will do it faster for you then a builtin excel formula. In this case, I would sugest you to give a try to the PowerPivot addin, and see how it handles the situation.