Can't switch row after specific line number - excel

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

Related

How to check if name of an cells exists (name.name)?

I want to write the named area in a string (and go through column D unitl last row). But if the cell is not named I get an error on "Name = wsData.Cells(i, 4).Name.Name" and code is stopping there.
I need something like, if NamedArea is nothing/empty, then do next i.
Do you have an idea?
Thank you!!
For i = 1 To LastRow
Name = wsData.Cells(i, 4).Name.Name
If UCase(arrData(k, j)) = UCase(Name) Then
arrData(k, j) = arrNames(i, 1)
x = x + 1
k = 1
j = 1
i = 1
Exit For
End If
Next i

I have one loop into another but they don't work as I expected

I'm trying to put one loop called j into another called i. In one of the ways I've tried the j loop end before the i loop and copy the same number each time because the i loop is no advancing.
For i = 2 To lastRowK
For j = 5 To 500
If Worksheets(1).Cells(i, 5).Value = 7 Then
Worksheets(2).Cells(j, 6).Value = Worksheets(1).Cells(i, 1).Value
Next j
Next i
In the other option, I've tried to put a conditional for the Next j, but it gives to me a compile error: Next without for.
For i = 2 To lastRowK
For j = 5 To 500
If Worksheets(1).Cells(i, 5).Value = 7 Then
Worksheets(2).Cells(j, 6).Value = Worksheets(1).Cells(i, 1).Value
If KRData.Cells(i, 5).Value = 7 Then _
Next j
Next i
The third option I've tried is to put both of next in the conditional, but it returns a compile error: expected list separator or end of the statement.
For i = 2 To lastRowK
For j = 5 To 500
If Worksheets(1).Cells(i, 5).Value = 7 Then
Worksheets(2).Cells(j, 6).Value = Worksheets(1).Cells(i, 1).Value
If KRData.Cells(i, 5).Value = 7 Then _
Next j AND Next i
Also, the end if or else are not working.
Make sure your To value lastRowK is set before the start of the loop. If its value is less than the initial value, in your case 2, the expression will evaluate false and terminate the loop.
For i = 0 To 3
Next i
Will loop until i = 4. It will then terminate the loop and resume execution at the next line of code following the Next statement.
For i = 2 To 1
Next i
In this example the loop will immediately terminate and advance to the the first line of code preceding the next statement. I’d be willing to bet if you put a breakpoint at the initialization of your loop, and the checked the local variables in the tools menu, you’d see that lasRowK has a value of Empty or less than equal to 1.

How to split the 2nd dimension of a for loop?

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

How to add values from different sheets, and retain the formula pointing to the sheets+cells?

I am trying to add values from different sheets (Sheet 2 to 5) into my main sheet (Sheet 1). In Sheet 1 I want the cells to contain the right formula pointing to the different sheets (if possible).
Typically like this:
='Sheet2'!D5+'Sheet3'!D165
All my sheets have different products, but some sheets contain same products. So I want to search through them all and ADD them in my Main Sheet (Sheet 1).
Sub UpdateMainSheet()
' Kode for å Oppdatere Plukkeliste Alle Artikler Summert
Dim AktivtArk As String
Dim AktivtArkNavn As String
Dim K As Integer
Dim Count As Integer
'Line of code to delete old data in Main Sheet:
Worksheets("Sheet1").Range("A2:H10000").Clear
AktivtArkOverskrift = "List of Articles from Sheet 2 to 5"
'Creates Headline in Main Sheet:
eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("Sheet1").Cells(eRow, 1) = AktivtArkOverskrift
Worksheets("Sheet1").Cells(eRow, 1).Font.Bold = True
'Script to check and gather data from the other sheets (Sheet 2, 3, 4 and 5):
For K = 2 To 5
'For loop to check each line in sheet "K"
For I = 2 To 1000
'If function to check if the cell I in column F is empty, if so it_jumps to next row and do the same check:
If Worksheets(K).Cells(I, 6) > 0 Then
Count = 0
'For loop to check if I already have a row in the Main Sheet with the article I'm checking:
For L = 2 To 1000
'If function to check if the articles have the same article number:
If Worksheets(K).Cells(I, 1) = Worksheets("Sheet1").Cells(L, 1) Then
'Line of code that are supposed to ADD the values that is currently in the Main Sheet, togheter with the value in Sheet K:
Worksheets("Sheet1").Cells(I, 4).Formula = Worksheets("Sheet1").Cells(I, 4) + Worksheets(K).Cells(L, 4)
End If
Next L
End If
Next I
Next K
End Sub
So what I need to fix in my code is this part (located furthest inside the For Loop):
Worksheets("Sheet1").Cells(I, 4).Formula = Worksheets("Sheet1").Cells(I, 4) + Worksheets(K).Cells(L, 4)
And make it create a formula in the wanted cell, that looks something like this:
='Sheet2'!D5+'Sheet3'!D165
It must be able to add another cell as well, since the Loop are running through several Sheets (Sheet 2 to 5) that may contain the same products.
I.e. I only want one line in my Main Sheet for each product.
I managed to find the solution in the end.
It seemed I had switched the L and I in som of the looping, which resulted in the values not to be added togheter.
The following code (I did not translate to English, but can do this if someone wants/need it) solved my issue, and gave me the values from Sheet 2 to 5 sorted by product in Sheet 1:
Sub OppdaterePlukkelisteSummert()
'Kode for å Oppdatere Plukkeliste Alle Artikler Summert
Dim AktivtArk As String
Dim AktivtArkNavn As String
Dim K As Integer
Dim Teller As Integer
Dim value1 As Integer
Dim value2 As Integer
'Sletter Plukklisten for å oppdatere og sortere på nytt:
Worksheets(1).Range("A2:H10000").Clear
'HENTING AV DATA FRA ARKET "K":
AktivtArk = "Artikler Summert fra Alle Ark"
AktivtArkOverskrift = "Artikler Summert fra Alle Ark"
'Setter inn Overskrift som Forteller kva ark utstyret kommer fra:
eRow = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets(1).Cells(eRow, 1) = AktivtArkOverskrift
Worksheets(1).Cells(eRow, 1).Font.Bold = True
'Sjekker hvilke/hvilket rader/utstyr som skal plukkes, og legger det inn i "Ark1":
For K = 2 To 5
For I = 2 To 1000
If Worksheets(K).Cells(I, 6) > 0 Then
Teller = 0
For L = 2 To 1000
If Worksheets(K).Cells(I, 1) = Worksheets(1).Cells(L, 1) Then
value1 = Worksheets(1).Cells(L, 4)
value2 = Worksheets(K).Cells(I, 4)
Worksheets(1).Cells(L, 4) = value1 + value2
Worksheets(1).Cells(L, 6) = value1 + value2
Else
Teller = Teller + 1
End If
Next L
If Teller > 998 Then
eRow = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For J = 1 To 11
Worksheets(1).Cells(eRow, J) = Worksheets(K).Cells(I, J)
Next J
Worksheets(1).Cells(eRow, 6).Font.Color = RGB(0, 150, 0)
Worksheets(1).Cells(eRow, 7).Font.Color = RGB(0, 150, 0)
End If
End If
Next I
Next K
Worksheets(1).Range("A2").Select
End Sub
I hope this can be useful for someone else :-)
All help and suggestion in the comments are appreciated!
I was going to illustrate with this simple example:
I = 1 'for example
For K = 2 To 5
Worksheets("Sheet1").Cells(I, 4).Value = Worksheets("Sheet1").Cells(I, 4).Value + _
WorksheetFunction.SumIf(Worksheets(K).Range("A:A"), "Bananas", Worksheets(K).Range("D:D"))
Next K

Filling Array once worked, does not anymore (subscript out of range)

The code I have once worked to add/delete groups of rows (requirements). I needed to modify the code so that if the 1st row of the group met certain criteria (i.e, the requirement was not one we wanted to consider), (1) we would not count it and (2), we would hide the group (current and subsequent 2 rows). This all works fine.
The problem is that now that I incorporated these changes, I get an error in another section of the code and for the life of me I cannot figure out why. I have stepped through this and am extremely frustrated. I am reaching out for help, and am hoping someone can see my error(!)
We calculate the start and finish row numbers within a grouping, and store these calculations in Arrays called "Start" and "Finish." I use the ReDim statement to initialize my arrays, because I thought that could be part of the problem, but no.
Any insight as to why my "subscripts are out of range" would be appreciated. I have traced through the logic, investigated this error, and read about the syntax/usage of VBA arrays. I don't know what else to do. Thanks in advance. Here are the relevant lines:
Sub Button1_Click()
Cells.Select
Selection.ClearOutline
If Cells.EntireRow.Hidden Then Cells.EntireRow.Hidden = False
Dim Start() As Integer
Dim Finish() As Integer
Dim p As Integer, q As Integer
ReDim Start(0, 50)
ReDim Finish(0, 50)
The following is embedded in logic that loops through all the rows in the spreadsheet:
i = 1
For Row = 4 To Cells(1, 6).Value - 1
If Begin Then
If Cells(Row, 3).Interior.ColorIndex = 44 Then
Start(i) = Row + 1
j = Cells(Row, 2).Value
Begin = False
End If
Else
If Cells(Row, 2).Value = j + 1 Or Cells(Row, 2).Interior.ColorIndex = 37 Then
Finish(i) = Row - 1
Begin = True
i = i + 1
Row = Row - 1
End If
End If
Next
The block I changed is as follows (code I added is last block where I attempt to hide rows). It precedes the previous. I am wondering how my change could have affect the above(?!)
If Cells(Row, 5).Value = "Requirement" Then
Range(Cells(Row, 4), Cells(Row, 4)).Interior.ColorIndex = 40
Rows(Row).Font.Bold = True
Rows(Row).Font.Italic = False
Rows(Row).Font.ColorIndex = 1 'Black
If Cells(Row - 3, 4).Value = "" Then 'this is requirement #1
Cells(Row, 4).Value = 1
Else
Cells(Row, 4).Value = Cells(Row - 3, 4).Value + 1
End If
p = Row
q = p + 2
Rows(p & ":" & q).Select
If Cells(p, 19).Value = "4" Then
Selection.EntireRow.Hidden = True
Else
Selection.EntireRow.Hidden = False
End If
Redim Start(0,50) makes the array dimensions 0 to 0, 0 to 50 i.e. a 2d array.
This means that when you call the array you need to provide parameters for both dimensions I.E: Start(0,i) or Finish(0,i). Calling Start(i) will result in the error you mentioned.
The easiest way to get rid of the error is to change your Redim lines to
ReDim Start(50)
ReDim Finish(50)
which is what I assume you meant to do in the first place.
Note: Based upon the format you used, you may have been meaning to do Start(0 to 50) and Finish(0 to 50) initially. The comma in the dimensioning indicates another dimension instead of a separation between lower and upper bounds.

Resources