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
Related
Hi Everyone I'm new to VBA Code & I'm Stuck with this one. I hope someone help me..
Step 1: compare the row with every SU headers. Here in row2, the value in SU_BS is 211. Once there is a value in SU , the column NAME2 should display the corresponding NAME (s1) with SU value (211). So NAME2 should display s1 211.
Step 2: Once NAME2 display the value, want to move to the next row without further checking SU headers.
Step 3: As per step 1 & 2 the column NAME2 in Next row will also display S1 211. But what i need is while filling the value, NAME2 column should check for the duplicate value. If duplicate value exist the control moves to next SU header. If the SU header has no value, move on to the next SU header if value exist NAME2 should display the value (S1 ZY) in row3.
Use an array to store each row and previous row in turn, compare elements until different.
Option Explicit
Sub macro()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim r As Long, c As Integer, n As Integer
Dim k As Integer, kLast As Integer
Dim ar(4, 1) As String
r = 2 ' start row
While Len(ws.Cells(r, 1)) > 0
' fill array
For n = 0 To 3
c = n * 2 + 4
ar(n, 1) = ws.Cells(r, c)
Next
k = 0
If r > 2 Then
' compare
While k <= kLast And ar(k, 1) = ar(k, 0)
k = k + 1
Wend
' skip pass any blanks
While Len(ar(k, 1)) = 0 And k <= 3
k = k + 1
Wend
End If
' output
ws.Cells(r, 3) = ws.Cells(r, 2) & " " & ar(k, 1)
' remember last row
For n = 0 To 3
ar(n, 0) = ar(n, 1)
Next
kLast = k
r = r + 1
Wend
MsgBox "Done"
End Sub
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
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 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