Excel VBA Code Doesn't Work Properly in Group - excel

I am new to VBA coding just like most of the people who ask questions like this one here. :)
I wrote a code which runs without any error but for some reason the second part is not executed if it is run in a group. Surprisingly, it does its job if I run the second part of the code on its own.
Can someone give me a bit of guidance what is causing this problem?
Sub Macro1()
Endrow1 = Cells(Rows.Count, 1).End(xlUp).Row
Dim j As Integer
Dim k As Integer
For i = 3 To Endrow1
With Sheets("Sheet1").Cells(i, 1)
If Cells(i, 1).Value = "No" Then Cells(i, 1).Value = ""
If Left(.Value, 3) = "Yes" Then
Rows(i).Font.Bold = True
a = Cells(i, 3).Value
For j = 0 To 30
Cells(j + i, 12).Value = a
Next j
End If
End With
Next i
'------------------------------------------------------------------- Second Part:
For j = 3 To 300
With Sheets("Sheet1").Cells(j, 7)
If Cells(j, 7).Value >= a And Cells(j, 7) <= Cells(1, 11) Then
If Cells(j, 12) = Cells(j, 12) Then
On Error Resume Next
For k = 0 To 30
If Cells(j + k, 12) = Cells(j, 12) Then Cells(j + k, 12).Interior.Color = 255
If Cells(j - k, 12) = Cells(j, 12) Then Cells(j - k, 12).Interior.Color = 255
Next k
End If
End If
End With
Next j
End Sub

When you debug this, do you get past this condition "If Cells(j, 7).Value >= a And Cells(j, 7) <= Cells(1, 11) Then"? Also, what is the 'on error resume next' hiding? Lastly, I don't think your 'With' structure is doing anything.
Thank you for your answer Martin. The problem was caused by my first if statement in the second part.
This is the correct version:
For j = 3 To 300
With Sheets("Sheet1").Cells(j, 7)
If Cells(j, 7).Value <= Cells(1, 11) Then
If Cells(j, 12) = Cells(j, 12) Then
On Error Resume Next
For k = 0 To 30
If Cells(j + k, 12) = Cells(j, 12) Then Cells(j + k, 12).Interior.Color = 255
If Cells(j - k, 12) = Cells(j, 12) Then Cells(j - k, 12).Interior.Color = 255
Next k
End If
End If
End With
Next j
I will get rid off the with statement later. :)

Related

VBA code not entering else if and end if loop

I have the below loop written, however I am having issues with the code entering the else if loop and to have the loop until (end if) the ranges equal each other. Essentially I want the loop to add 2 to the cells that contain "Corporates" first and afterwards add 1 to the cells that have a the number 2.
Dim x As Long
Dim V As Variant
V = Cells(Rows.Count, 1).End(xlUp).Row
JV = Range("J" & Rows.Count).End(xlUp).Row
For x = 1 To V
If Cells(x, 2).Value = "Corporates" Then
Cells(x, 7).Value = Cells(x, 7).Value + 1
ElseIf Cells(x, 12).Value < "3%" And Cells(x, 2).Value = "Corporates" Then
Cells(x, 7).Value = Cells(x, 7).Value + 1
End If
Range("J" & JV + 3).Value = Range("J" & JV + 2).Value
Next x
End Sub
Use a nested If instead of ElseIf
I'm guessing that < "3%" should be < 0.03 ("3%" is text, not a number).
If Cells(x, 2).Value = "Corporates" Then
If Cells(x, 12).Value < 0.03 Then
Cells(x, 7).Value = Cells(x, 7).Value + 2
Else
Cells(x, 7).Value = Cells(x, 7).Value + 1
End If
End If

Sum of all the iterations of a variable in VBA

I have the following code
Dim i As Integer
For i = 3 To 10
If Range("H3").Value = Cells(i, 2).Value And Range("I3").Value < Cells(i, 4).Value And _
Range("I3").Value >= Cells(i, 3).Value Then
Range("J3").Value = Cells(i, 5).Value
End If
Next i
I want the value of J3 to represent the sum of all the iterations and not just the last iteration if i. Can it be done?
While there are certainly better methods of adding up cells, for your particular method this should work.
Dim i As long, lTotal as long
For i = 3 To 10
If Range("H3").Value = Cells(i, 2).Value And Range("I3").Value < Cells(i, 4).Value And _
Range("I3").Value >= Cells(i, 3).Value Then
lTotal = Cells(i, 5).Value + lTotal
End If
Next i
Range("J3").Value = lTotal
Keep a running total of of your loop, then use the running total as your cell's value after you've finished the loop
Change this line
Range("J3").Value = Cells(i, 5).Value
To:
Range("J3").Value = Range("J3").Value + Cells(i, 5).Value

Excel based tracking database

I have a code which Sheet "RAW" is updated each day with more rows and updates the existing rows, I'm trying to get the number in Column B to match Column A in sheet data, then depending on what information is in other columns add 1 to a value in a column (17 different options)
It's basically going to be used as a tracker to check how many days something is on a specific status and I need to keep it for historical Measuring indefintely. here is what I have so far which doesn't seem to work.
Additionally I would also like it to measure an 18th catagory if it is missing from the data list if this is possibble?
'status tracking
Sub Status_Track()
Dim a As Long 'topic number
Dim Z As Long
Dim R As Long
Dim i As Long
Dim S As Long
Dim D As Long
Worksheets("RAW").Activate
R = Cells(Rows.Count, 2).End(xlUp).Row
C = Cells(1, Columns.Count).End(xlToLeft).Column
Z = 0
i = 2
Do Until i > R
'ident
If Cells(i, 2) = Worksheets("Data").Cells(i, 1) And (Cells(i, 13) = "ERKA") Then
Z = Worksheets("Data").Cells(i, 6) + 1
Worksheets("Data").Cells(i, 6).Value = Z
ElseIf Cells(i, 2) = Worksheets("Data").Cells(i, 1) And (Cells(i, 13) = "INBA") Then
'Inba
Z = Worksheets("Data").Cells(i, 7) + 1
Worksheets("Data").Cells(i, 7).Value = Z
ElseIf Cells(i, 2) = Worksheets("Data").Cells(i, 1) And (Cells(i, 13) = "ABGE") Then
'Abge
Z = Worksheets("Data").Cells(i, 8) + 1
Worksheets("Data").Cells(i, 8).Value = Z
ElseIf Cells(i, 2) = Worksheets("Data").Cells(i, 1) And (Cells(i, 13) = "GELO") Then
'Gelo
Z = Worksheets("Data").Cells(i, 5) + 1
Worksheets("Data").Cells(i, 5).Value = Z
ElseIf Cells(i, 2) = Worksheets("Data").Cells(i, 1) And (Cells(i, 13) = "UEBE") And (Cells(i, 11) = 0) Then
'UEBE
Z = Worksheets("Data").Cells(i, 9) + 1
Worksheets("Data").Cells(i, 9).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "<1") Then
'1
Z = Worksheets("Data").Cells(i, 10) + 1
Worksheets("Data").Cells(i, 10).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "6") Then
'6
Z = Worksheets("Data").Cells(i, 11) + 1
Worksheets("Data").Cells(i, 11).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "9") Then
'9
Z = Worksheets("Data").Cells(i, 12) + 1
Worksheets("Data").Cells(i, 12).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "10") Then
'10
Z = Worksheets("Data").Cells(i, 13) + 1
Worksheets("Data").Cells(i, 13).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "15") Then
'15
Z = Worksheets("Data").Cells(i, 14) + 1
Worksheets("Data").Cells(i, 14).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "30") Then
'30
Z = Worksheets("Data").Cells(i, 15) + 1
Worksheets("Data").Cells(i, 15).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "50") Then
'50
Z = Worksheets("Data").Cells(i, 16) + 1
Worksheets("Data").Cells(i, 16).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "60") Then
'60
Z = Worksheets("Data").Cells(i, 17) + 1
Worksheets("Data").Cells(i, 17).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "70") Then
'70
Z = Worksheets("Data").Cells(i, 18) + 1
Worksheets("Data").Cells(i, 18).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "80") Then
'80
Z = Worksheets("Data").Cells(i, 19) + 1
Worksheets("Data").Cells(i, 19).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "90") Then
'90
Z = Worksheets("Data").Cells(i, 20) + 1
Worksheets("Data").Cells(i, 20).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "97") Then
'97
Z = Worksheets("Data").Cells(i, 21) + 1
Worksheets("Data").Cells(i, 21).Value = Z
ElseIf Cells(i, 11) = 1 And (Cells(i, 28) = "100") Then
'100
Z = Worksheets("Data").Cells(i, 22) + 1
Worksheets("Data").Cells(i, 22).Value = Z
End If
Loop
End Sub
It could look something like that to find the corresponding identifier
Option Explicit 'must be the first line in a module: forces you to declare any variables before use
'status tracking
Sub Status_Track_Extended()
Dim wsRaw As Worksheet, wsData As Worksheet
Set wsRaw = ThisWorkbook.Worksheets("RAW")
Set wsData = ThisWorkbook.Worksheets("Data")
Dim LastRow As Long
LastRow = wsRaw.Cells(wsRaw.Rows.Count, 2).End(xlUp).Row 'find last row in sheet RAW
Dim FoundCell As Range, FoundRow As Long
Dim DataCol As Long
Dim i As Long
For i = 2 To LastRow 'start at row 2 up to last used row
'find corresponding row by identifier (column 2) in sheet Data
Set FoundCell = wsData.Columns(1).Find(wsRaw.Cells(i, 2))
If Not FoundCell Is Nothing Then 'only do the follwing if the identifier was found in sheet Data
FoundRow = FoundCell.Row
'ident
If wsRaw.Cells(i, 13) = "ERKA" Then
wsData.Cells(FoundRow, 6).Value = wsData.Cells(FoundRow, 6).Value + 1
ElseIf wsRaw.Cells(i, 13) = "INBA" Then
'Inba
wsData.Cells(FoundRow, 7).Value = wsData.Cells(FoundRow, 7).Value + 1
ElseIf wsRaw.Cells(i, 13) = "ABGE" Then
'Abge
wsData.Cells(FoundRow, 8).Value = wsData.Cells(FoundRow, 8).Value + 1
ElseIf wsRaw.Cells(i, 13) = "GELO" Then
'Gelo
wsData.Cells(FoundRow, 5).Value = wsData.Cells(FoundRow, 5).Value + 1
ElseIf wsRaw.Cells(i, 13) = "UEBE" And wsRaw.Cells(i, 11) = 0 Then
'UEBE
wsData.Cells(FoundRow, 9).Value = wsData.Cells(FoundRow, 9).Value + 1
ElseIf wsRaw.Cells(i, 11) = 1 Then
Select Case wsRaw.Cells(i, 28)
Case "<1"
wsData.Cells(FoundRow, 10).Value = wsData.Cells(FoundRow, 10).Value + 1
Case "6"
wsData.Cells(FoundRow, 11).Value = wsData.Cells(FoundRow, 11).Value + 1
Case "9"
wsData.Cells(FoundRow, 12).Value = wsData.Cells(FoundRow, 12).Value + 1
Case "10"
wsData.Cells(FoundRow, 13).Value = wsData.Cells(FoundRow, 13).Value + 1
Case "15"
wsData.Cells(FoundRow, 14).Value = wsData.Cells(FoundRow, 14).Value + 1
Case "30"
wsData.Cells(FoundRow, 15).Value = wsData.Cells(FoundRow, 15).Value + 1
Case "50"
wsData.Cells(FoundRow, 16).Value = wsData.Cells(FoundRow, 16).Value + 1
Case "60"
wsData.Cells(FoundRow, 17).Value = wsData.Cells(FoundRow, 17).Value + 1
Case "70"
wsData.Cells(FoundRow, 18).Value = wsData.Cells(FoundRow, 18).Value + 1
Case "80"
wsData.Cells(FoundRow, 19).Value = wsData.Cells(FoundRow, 19).Value + 1
Case "90"
wsData.Cells(FoundRow, 20).Value = wsData.Cells(FoundRow, 20).Value + 1
Case "97"
wsData.Cells(FoundRow, 21).Value = wsData.Cells(FoundRow, 21).Value + 1
Case "100"
wsData.Cells(FoundRow, 22).Value = wsData.Cells(FoundRow, 22).Value + 1
End Select
End If
Else 'error if identifier was not found
MsgBox "Identifier '" & wsRaw.Cells(i, 2) & "' could not be found in sheet 'Data'.", vbExclamation + vbOKOnly
End If
Next i
End Sub

IF statements not outputting proper value

So I have been trying to automate a process in excel with vba and I seem to be having trouble. I haven't done a lot with vba before and I appreciate any help. I have asked a couple questions before and looked up info on google and here, but my program still isn't running properly.
If I run this code just once then it works perfectly fine, but I want to run it 30 times with results continually updating and putting the new answer into a different cell then it doesn't work. Basically returns the correct answer for the first run but every time after that it generates the wrong response.
So below is my code, any help with this would be greatly appreciated!!!
For i = 19 To 19
Range("AA3:AA7").Select
Selection.ClearContents
If Cells(i, 26) > 499999 Then
Cells(3, 27) = 499999
ElseIf Cells(i, 26) < 499999 Then
Cells(3, 27) = Cells(i, 26)
End If
If Cells(3, 27) < 499999 Then
Cells(5, 27) = 0
ElseIf Cells(i, 26) > 999999 Then
Cells(4, 27) = 500000
ElseIf Cells(i, 26) < 999999 Then
Cells(4, 27) = Cells(i, 26) - 499999
End If
If Cells(4, 27) = 0 Then
Cells(5, 27) = 0
ElseIf Cells(i, 26) > 1999999 Then
Cells(5, 27) = 1000000
ElseIf Cells(i, 26) < 1999999 Then
Cells(5, 27) = Cells(i, 26) - 999999
End If
If Cells(5, 27) = 0 Then
Cells(6, 27) = 0
ElseIf Cells(i, 26) > 4999999 Then
Cells(6, 27) = 3000000
ElseIf Cells(i, 26) < 4999999 Then
Cells(6, 27) = Cells(i, 26) - 1999999
End If
If Cells(6, 27) = 0 Then
Cells(7, 27) = 0
ElseIf Cells(6, 27) = 3000000 Then
Cells(7, 27) = Cells(3, i) - 4999999
End If
Cells(i, 30).Value = (Cells(3, 28) + Cells(4, 28) + Cells(5, 28) + Cells(6, 28) + Cells(7, 28)) / Cells(i, 26)
Next i
You haven't got anything in place for when the value is exactly the figure eg <499999 and >499999 doesn't account for =499999.
You also don't need to state next i - just a next will do.
I'm not sure why you're updating a cell then adding them together, you'd be better off assigning the cell values to variables as with the below. If you'd like to see where it is going wrong I may be an idea to add msgbox calls to show the values as it iterates:
Dim I as integer
Dim value1, value2, value3, value4, value5 as integer
For i = 19 To 49
If Cells(i, 26) > 499999 Then
Value1=499999
Else
Value1=Cells(i, 26)
End If
' and so on...
Cells(i, 30).Value = value1+value2 etc...
Next

VBA Script working and now is not

I wrote a very quick script for VBA that takes a spread sheet and organizes it appropriately. For some reason I have just tried using it and I get a user defined error at this loop:
Dim qu As Long
For j = i To 10 Step -1
If Cells(j, 10) = "" Then
qu = j - 1
Do While Cells(qu, 10) = Cells(qu - 1, 10)
Cells(qu, 11) = 10
Cells(qu - 1, 11) = 10
qu = qu - 1
Loop
Cells(j - 1, 11) = 10
End If
Next j
The whole code looks like this:
Sub PopulateNF()
i = 10
Do While Cells(i, 2) <> ""
i = i + 1
Loop
For k = 10 To i Step 1
If Cells(k, 1) <> "" Then
Cells(k, 10) = ""
Else
If InStr(1, Cells(k, 2), "Received") Then
Cells(k, 10) = -1
ElseIf InStr(1, Cells(k, 2), "Workflow") Then
Cells(k, 10) = 0
ElseIf InStr(1, Cells(k, 2), "Forwarded") Then
Cells(k, 10) = 1
ElseIf InStr(1, Cells(k, 2), "Review Response") Then
Cells(k, 10) = 2
ElseIf InStr(1, Cells(k, 2), "Responded and Closed") Then
Cells(k, 10) = 4
ElseIf InStr(1, Cells(k, 2), "Sent") Then
Cells(k, 10) = 3
ElseIf InStr(1, Cells(k, 2), "Sent and Closed") Then
Cells(k, 10) = 3
End If
End If
Next k
Dim qu As Long
For j = i To 10 Step -1
If Cells(j, 10) = "" Then
qu = j - 1
Do While Cells(qu, 10) = Cells(qu - 1, 10)
Cells(qu, 11) = 10
Cells(qu - 1, 11) = 10
qu = qu - 1
Loop
Cells(j - 1, 11) = 10
End If
Next j
For a = i To 10 Step -1
If Cells(a, 1) <> "" Then
Cells(a, 11) = 10
End If
Next a
Const colA As Long = 11
Dim lngRow As Long
Dim lngLastRow As Long
lngLastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
lngRow = 10
Do While lngRow <= lngLastRow
If Cells(lngRow, colA) = "" Then
Cells(lngRow, 1).EntireRow.Delete
lngLastRow = lngLastRow - 1
Else: lngRow = lngRow + 1
End If
Loop
d = 10
Do While Cells(d, 2) <> ""
d = d + 1
Loop
For k = 6 To d Step 1
If Cells(k, 1) = "" Then
Cells(k, 1) = Cells(k, 6)
Cells(k, 6) = ""
Cells(k, 2) = Cells(k, 7)
Cells(k, 7) = ""
Cells(k, 3) = Cells(k - 1, 3)
Cells(k, 4) = Cells(k - 1, 4)
Cells(k, 5) = Cells(k - 1, 5)
Cells(k, 6) = Cells(k - 1, 6)
Cells(k, 7) = Cells(k - 1, 7)
Cells(k, 8) = Cells(k - 1, 8)
Cells(k, 9) = Cells(k - 1, 9)
End If
Next k
Const colAN As Long = 1
Dim lngRowN As Long
Dim lngLastRowN As Long
lngLastRowN = Cells.SpecialCells(xlCellTypeLastCell).Row
lngRowN = 9
Do While lngRowN <= lngLastRowN
If Cells(lngRowN, colAN) = "" Then
Cells(lngRowN, 1).EntireRow.Delete
lngLastRowN = lngLastRowN - 1
ElseIf InStr(1, Cells(lngRowN, colAN), "_") Then
Cells(lngRowN, 1).EntireRow.Delete
lngLastRowN = lngLastRowN - 1
Else: lngRowN = lngRowN + 1
End If
Loop
Range("a9").CurrentRegion.Sort key1:=Range("a9"), order1:=xlAscending, Header:=xlGuess
Range("D:D").NumberFormat = "mm/dd/yyyy"
Range("F:I").NumberFormat = "mm/dd/yyyy"
Range("C:I").HorizontalAlignment = xlCenter
Range("a:a").VerticalAlignment = xlTop
Range("J:K").EntireColumn.Delete
Range("A:J").Font.Color = vbBlack
MsgBox ("Reformatting Complete")
End Sub
Any help would be greatly appreciated!
Stepping through the code looks like your loop doesn't know where to end.
Basically the line:
Do While Cells(qu, 10) = Cells(qu - 1, 10)
is never false and "qu-1" eventually becomes negative.
I'm not sure if this works with the greater bulk of your code, but you could try:
Do While qu <> 1
That should close your loop while still allowing you to modify cell values within the loop.

Resources