VBA error 1004 in do until loop - excel

I'm pretty new to VBA, I've been succesfull before in programming loops, however, this time I can't seem to find the solution to make this code work.
The goal is to assemble data from one sheet (divided over 400+ seperate tables) to another sheet.
The program works without the outer loop, but when coding the loop around the other two loops I constantly get the error "run-time error '1004': Application-defined or object-defined error.
Dim x As String
Dim HStop As String
Dim y As String
Dim VStop As String
Dim z As Integer
Dim Row As Integer
Dim Column As Integer
Dim Counter As Integer
Sub Reform()
'1 maal te doen:
Row = 10
Column = 1
Counter = 6
'begin sub
Do
HStop = Worksheets("Find").Cells(2, 3).Value
VStop = Worksheets("Find").Cells(3, 3).Value
x = 1
y = 1
Do
Column = Column + 1
x = Worksheets("retour").Cells(Row, Column).Value
Loop Until x = HStop
'Checkpoint
Worksheets("Find").Cells(2, 12).Value = x
Do
Row = Row + 1
y = Worksheets("retour").Cells(Row + 7, 2).Value
Worksheets("Find").Cells(Counter, 12).Value = Worksheets("retour").Cells(Row, Column).Value
Counter = Counter + 1
Loop Until y = VStop
'Checkpoint
Worksheets("Find").Cells(3, 14).Value = y
Worksheets("Find").Cells(2, 14).Value = Row
Worksheets("Find").Cells(3, 14).Value = Column
z = z + 1
Loop Until z = 10
End Sub
Can someone help me?

Well, not sure how this code will help you to get desired result, however the error occurs because in the following peice of code
Do
HStop = Worksheets("Find").Cells(2, 3).Value
VStop = Worksheets("Find").Cells(3, 3).Value
x = 1
y = 1
Do
Column = Column + 1
x = Worksheets("retour").Cells(Row, Column).Value
Loop Until x = HStop
'Checkpoint
HStop is assigned a value, then in the loop you are assigning another value to x and looping till x = HStop. In Do loop you are also incrementing the value of Column. In case, x is not equal to HStop and Column value reaches 16385 i.e. one more than the last column of the sheet, it throws runtime error while assigning
x = Worksheets("retour").Cells(Row, 16385).Value

Related

For Loop with If-Or Statement VBA

I have hard-coded for ever two cells in an excel column to count if either the first or second have a value of 1. It worked this way (but also written out for every cell applicable so is inefficent):
If Range("D10").Value = 1 Or Range("D11").Value = 1 Then
x = x + 1
Else
x = x + 0
End If
I am trying to refactor it. My thought was to do a for loop, with an if and an or statement (similar to in my current code), but receive a syntax error. My attempt:
Sub refactor()
Dim x As Integer
x = 0
For Each c In Range("D2-D13").Cells
If c.Value =1 OR (c+1).Value = 1 Then
x = x + 1
Else
x = x + 0
End If
Range("A19") = x
End Sub
All I can find is for loops with if statements in them, but never the added "or" condition considered. VBA is new to me. Is there a way I can code this?
for loop in enough to run entire range itn't required "or c.offset(1,0).value = 1" because "or" operator used to any one condition is true it will allow to run the code.
"if c.Value = 1 and c.Offset(1, 0).Value = 1 Then" = this case is used for if both the statement is true then only if condition allow to run the code. are u looking for this?
Sub refactor()
Dim x As Integer
Dim c As Range
x = 0
For Each c In Range("D2:D13")
'if c.Value = 1 Or c.Offset(1, 0).Value = 1 Then'this line is same as below line
if c.value = 1 then
x = x + 1
End If
Next c
Range("A19") = x
End Sub

Error 1004 when adding Item value to worksheet and changing row

Hi I am trying to output my collection to a worksheet in a way that moves to a new row when a # is detected in the text of the current collection item. All of the items will print in one row but I cannot change the row with my current code.
For Each Item In jsonValues
e = InStr(1, p(r), "#")
If e = 1 Then y = y + 1 And currentcolumn = 1
Worksheets("jsonoutput").Cells(y, currentcolumn).Value = Item
If ActiveWorkbook.Worksheets("jsonoutput").Cells(y, currentcolumn).Value <> "" Then currentcolumn = currentcolumn + 1
r = r + 1
Next Item
P(r) has the same value as Item - I have tried both in the Instr line but both times I get a 1004 error when e=1 and it is then time to increase y to y+1 It looks like it is setting my y to 0 when I hover over it in the code break? I cannot understand why changing the y is causing an issue?
Your use of And there is a problem - it doesn't work like that.
Try something like this -
Dim ws As worksheet
Set ws = Worksheets("jsonoutput")
For Each Item In jsonValues
If Len(Item) > 0 Then
If InStr(1, Item, "#") = 1 Then
y = y + 1
currentcolumn = 1
End If
ws.Cells(y, currentcolumn).Value = Item
currentcolumn = currentcolumn + 1
end If
'r = r + 1
Next Item

Why does this vba macro ignores the value after an if else?

I have two columns, the first one is a date with Year/Month format and the other a numerical value of an evaluation that i have done. I want to get the average value for each month with a macro( i need to do it so many times an a lot of data on it). So, i decided to create an array of dates and a Matrix of evaluation results. The goal is to group all numeric values by date and get the average per month. The problem is that this code ignores the value when the actual and last cells are different.
Dim i As Integer 'number of rows
Dim J As Integer 'manage row change
Dim G As Integer 'manage column change
Dim Fecha(48) As String
Dim Matriz_FI(100, 100) As Double
'-------------------------------------------------------------- --
J = 0
G = 0
For i = 2 To 10
If i = 2 Then
Matriz_FI(J, G) = Sheets("Nueva Database").Cells(i, 11).Value
Fecha(J) = Sheets("Nueva Database").Cells(i, 3).Value
G = G + 1
Else
If (Sheets("Nueva Database").Cells(i, 3).Value = Sheets("NuevaDatabase").Cells(i - 1, 3).Value) Then
'Column change in Matriz_FI
Matriz_FI(J, G) = Sheets("Nueva Database").Cells(i, 11).Value
G = G + 1
MsgBox ("Same")
Else
'Row change in Matriz_FI
J = J + 1
Fecha(J) = Sheets("Nueva Database").Cells(i, 3)
G = 0
Matriz_FI(J, G) = Sheets("Nueva Database").Cells(i, 11).Value
MsgBox ("Different")
End If
End If
Next
End Sub

simple VBA function that I programmed produces #VALUE! error when used in excel

The function is designed to take in input - variable pg - that is in a cell on the spreadsheet, go through the rows of data to see which row in a column 1 matches variable pg. Once the match is found, it then goes through the columns to see which of the columns has "VRP23" Or "VRP24" in the first row. When that is found, it takes the number of the matching row/column and performs the "step1" modification. The issue is that in the spreadsheet the error #VALUE! appears and I'm not sure why this is.
Function getECONpgdimscore1(pg As String) As Double
Dim row As Integer
row = 2
Dim c As Integer
c = 1
Dim econ As Double
econ = 0
Dim x As Integer
Dim NumRows As Integer
NumRows = Range("A2", Range("A2").End(xlDown)).rows.count
Cells(row, 1).Select
For x = 1 To NumRows
If Cells(row, 1).Value = pg Then
Do While c < 48
Cells(row, 7 + c).Select
If Cells(1, 7 + c).Value = ("VRP23" Or "VRP24") Then
econ = econ + step1(Cells(1, 7 + c), Cells(row, 7 + c))
End If
c = c + 1
Loop
End If
row = row + 1
Next x
getECONpgdimscore1 = (econ / 100) * 2.5
End Function

Counting how many Lottery draws it takes for a range of numbers to appear

I'm practicing coding and as a Lottery player I thought I might dive into coding by making code in Excel VBA that counts how many draws a certain number I marked as "x" takes to appear and saves the number in "Counter" that increments whenever it doesn't find it. When it's found, it saves it in a cell I mark by "LineN" and "RowN" cursors and then it resets the "Counter" to zero, the "Line" increments to move from one draw to the next.
By the way, I play a 6/49 lottery and I have a list of 1667 draws.
First faulty code :
Sub Module1()
Dim Line As Integer
Dim Counter As Integer
Dim x As Integer
Dim LineN As Integer
Dim RowN As Integer
LineN = 1674
RowN = 2
Line = 3
Counter = 0
x = 1
Do Until Line > 1669
A = Worksheets("Sheet12").Cells(Line, 2).Value
B = Worksheets("Sheet12").Cells(Line, 3).Value
C = Worksheets("Sheet12").Cells(Line, 4).Value
D = Worksheets("Sheet12").Cells(Line, 5).Value
E = Worksheets("Sheet12").Cells(Line, 6).Value
F = Worksheets("Sheet12").Cells(Line, 7).Value
If A = x Or B = x Or C = x Or D = x Or E = x Or F = x Then
Worksheets("Sheet12").Cells(LineN, RowN).Value = Counter
RowN = RowN + 1
Line = Line + 1
Counter = 0
Else
Counter = Counter + 1
End If
Loop
End Sub
Current code : I fixed and edited my code a little bit to make it look for values ranging from 1 to 49 and place the results where the incrementing cursor LineN is pointing at, but for some reason it doesn't work, it only looks for the first value "1" and displays the results on the Excel sheet then it stops, meaning x isn't incrementing and LineN as well, it's as if the loop isn't working for some weird reason.
Any ideas ?
Sub Module1()
Dim Line As Integer
Dim Counter As Integer
Dim x As Integer
Dim LineN As Integer
Dim RowN As Integer
LineN = 1674
RowN = 2
Counter = 0
For x = 1 To 49
For Line = 3 To 1669
A = Worksheets("Sheet12").Cells(Line, 2).Value
B = Worksheets("Sheet12").Cells(Line, 3).Value
C = Worksheets("Sheet12").Cells(Line, 4).Value
D = Worksheets("Sheet12").Cells(Line, 5).Value
E = Worksheets("Sheet12").Cells(Line, 6).Value
F = Worksheets("Sheet12").Cells(Line, 7).Value
If A = x Or B = x Or C = x Or D = x Or E = x Or F = x Then
Worksheets("Sheet12").Cells(LineN, RowN).Value = Counter
RowN = RowN + 1
Line = Line + 1
Counter = 0
Else
Counter = Counter + 1
Line = Line + 1
End If
Next Line
LineN = LineN + 1
Next x
End Sub
I just want to post the final code that thanks to all the answers above is working now flawlessly and as intended, you can check it out below.
Sub Module1()
Dim Row As Integer
Dim Counter As Integer
Dim x As Integer
Dim ColN As Integer
Dim Col As Integer
Dim RowN As Integer
RowN = 1674
ColN = 2
Counter = 0
For x = 1 To 49
For Row = 3 To 1669
A = Worksheets("Sheet12").Cells(Row, 2).Value
B = Worksheets("Sheet12").Cells(Row, 3).Value
C = Worksheets("Sheet12").Cells(Row, 4).Value
D = Worksheets("Sheet12").Cells(Row, 5).Value
E = Worksheets("Sheet12").Cells(Row, 6).Value
F = Worksheets("Sheet12").Cells(Row, 7).Value
If A = x Or B = x Or C = x Or D = x Or E = x Or F = x Then
Worksheets("Sheet12").Cells(RowN, ColN).Value = Counter
ColN = ColN + 1
Counter = 0
Else
Counter = Counter + 1
End If
Next Row
RowN = RowN + 1
ColN = 2
Next x
End Sub
here is a different direction to attack the problem
create an array n(49) and init to zero
the array elements hold the draw# for each of the 49 numbers possible ( six array elements get updated at each draw with the drawn numbers being used as an index into the array)
each time a number is drawn then the corresponding array element is retrieved and checked (last draw# it was drawn)
that value gets subtracted from the current draw# and the difference is recorded as per your code
then the current draw# replaces the previous draw# in the array element
example: number 8 gets drawn in draw #1 and draw #14
draw# 1 ... 8,12,21,26,31,44 .... n(8)=1, n(12)=1, n(21)=1, n(26)=1, n(31)=1, n(44)=1
draw# 2 ... 2, 6,23,35,36,39 .... n(2)=2, n(6)=2, n(23)=2, n(35)=2, n(36)=2, n(39)=2
.
.
draw #14... 1, 3, 8,11,13,14 .... n(1)=14, n(3)=14, n(8) was 1, so record the difference of 13 and update n(8)=14
that way you are checking only six numbers at each draw

Resources