VBA: Adding a condition to an ElseIf loop - vba

I'd like to add one more condition to this Loop that would put "Discrepancy" into column J if there is a value in column F that is neither 0 or #VALUE. Any and all suggestions would be appreciated. Thank you, the current code is below:
Sub ERS_Vlookup()
Dim Lastrow As Long
Dim h As Long
For h = 5 To Lastrow
If IsError(ActiveSheet.Range("H" & h).Value) Or
IsError(ActiveSheet.Range("F" & h).Value) Then
ActiveSheet.Range("J" & h).Value = " "
ElseIf ActiveSheet.Range("H" & h).Value <> " " And _
ActiveSheet.Range("F" & h).Value = 0 Then
ActiveSheet.Range("J" & h).Value = "Paid"
Else
ActiveSheet.Range("J" & h).Value = "Processed Not Yet Paid"
End If
Next h

On the face of it, this should work, but I think you need to think it through and check whether these conditions are all mutually exclusive.
Sub ERS_Vlookup()
Dim Lastrow As Long
Dim h As Long
For h = 5 To Lastrow
If IsError(Range("H" & h).Value) Or IsError(Range("F" & h).Value) Then
Range("J" & h).Value = vbNullString
ElseIf Range("H" & h).Value <> vbNullString And Range("F" & h).Value = 0 Then
Range("J" & h).Value = "Paid"
ElseIf Not IsError(Range("F" & h).Value) And Range("F" & h).Value <> 0 Then
Range("J" & h).Value = "Discrepancy"
Else
Range("J" & h).Value = "Processed Not Yet Paid"
End If
Next h
End Sub

You can the use the And function to add booleans to loops
i recomend you visit this website http://www.excel-easy.com/vba/loop.html
for more information

Related

Double concatenation in different TextBox depending on category

I don't know if somebody already asked a question about it or not, i'm new to VBA but i've already done some C# programming.
Code
in Text :
For i = 1 To 5 'Category
For j = 1 To 7 'Each entry of the category
If UserForm1.Controls("Cat" & j & "Entry" & i).Value <> "" Then
Range("A" & i).Value = UserForm1.Controls("Cat" & i & "Entry" & j).Value
End If
Next j
Next i
So this is basicaly what i'm trying to do, i have 2 categories, each one have 7 TextBox, depending on how many of them are filled, I wanted to put their values on a cell... But seems like the concatenation isn't working, also tried the For Each methods but no results..
The interface
Thank you guys
You aren't concatenating anything you are overwriting cells A1-5 every time.
For i = 1 To 5 'Category
For j = 1 To 7 'Each entry of the category
If UserForm1.Controls("Cat" & j & "Entry" & i).Value <> "" Then
if range("A" & i).value <> "" then
Range("A" & i).Value = Range("A" & i).Value & " " & UserForm1.Controls("Cat" & i & "Entry" & j).Value
else 'Avoiding the space
range("A" & i).value = Userform1.controls("Cat" & i & "Entry" & j).value
end if
End If
Next j
Next i

For without next in Excel

I'm trying to get all possible combinations with a kind of VBA macro presented in https://stackoverflow.com/a/10693789/1992004, but get an error For without Next. I compared the source from another thread with mine, but don't found such difference, which could cause this error.
Do you see, what causes this error? - please point me to. My Code follows.
Option Explicit
Sub Sample()
Dim l As Long, m As Long, n As Long, o As Long, p As Long, q As Long, r As Long, s As Long, t As Long, u As Long
Dim CountComb As Long, lastrow As Long
Range("L2").Value = Now
Application.ScreenUpdating = False
CountComb = 0: lastrow = 18
For l = 1 To 1: For m = 1 To 2
For n = 1 To 2: For o = 1 To 18
For p = 1 To 15: For q = 1 To 10
For r = 1 To 10: For s = 1 To 17
For t = 1 To 3: For u = 1 To 3
Range("L" & lastrow).Value = Range("A" & l).Value & "/" & _
Range("B" & m).Value & "/" & _
Range("C" & n).Value & "/" & _
Range("D" & o).Value & "/" & _
Range("E" & p).Value & "/" & _
Range("F" & q).Value & "/" & _
Range("G" & r).Value & "/" & _
Range("H" & s).Value & "/" & _
Range("I" & t).Value & "/" & _
Range("J" & u).Value
lastrow = lastrow + 1
CountComb = CountComb + 1
Next: Next
Next: Next
Range("L1").Value = CountComb
Range("L3").Value = Now
Application.ScreenUpdating = True
End Sub
All the comments above explain your problem, but this is what your code would look like with proper indenting AND the missing "next" statements:
For l = 1 To 1
For m = 1 To 2
For n = 1 To 2
For o = 1 To 18
For p = 1 To 15
For q = 1 To 10
For r = 1 To 10
For s = 1 To 17
For t = 1 To 3
For u = 1 To 3
Range("L" & lastrow).Value = Range("A" & l).Value & "/" & _
Range("B" & m).Value & "/" & _
Range("C" & n).Value & "/" & _
Range("D" & o).Value & "/" & _
Range("E" & p).Value & "/" & _
Range("F" & q).Value & "/" & _
Range("G" & r).Value & "/" & _
Range("H" & s).Value & "/" & _
Range("I" & t).Value & "/" & _
Range("J" & u).Value
lastrow = lastrow + 1
CountComb = CountComb + 1
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
At the very least, it would have made it immediately obvious where your code was failing.

Outputting to two worksheets

I have the below function that outputs records to either a worksheet called CI or one called Error. I added an additional IF statement where if my source 'col' column contains the word "TITER" then I want it to output to the "Error" worksheet. This seems to be working and outputting the appropriate records to the Error tab. However I noticed that it is also outputting these same records to the "CI" worksheet as well. I have the IF code nested in the main Else statement, but I'm thinking it doesn't belong there. Any help is appreciated!
Public lstrow As Long, strDate As Variant, stredate As Variant
Sub importbuild()
lstrow = Worksheets("Data").Range("G" & Rows.Count).End(xlUp).Row
Function DateOnlyLoad(col As String, col2 As String, colcode As String)
Dim i As Long, j As Long, k As Long
j = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
strDate = spacedate(Worksheets("Data").Range(col & i).Value)
stredate = spacedate(Worksheets("Data").Range(col2 & i).Value)
If (Len(strDate) = 0 And (col2 = "NA" Or Len(stredate) = 0)) Or InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "EXP") > 0 Then
GoTo EmptyRange
Else
If InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "TITER") > 0 Then
Worksheets("Error").Range("A" & k & ":C" & k).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("Error").Range("D" & k).Value = "REVIEW MMR1 DATES"
k = k + 1
End If
Worksheets("CI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("CI").Range("D" & j).Value = colcode
Worksheets("CI").Range("E" & j).Value = datecleanup(strDate)
Worksheets("CI").Range("L" & j).Value = dateclean(strDate)
Worksheets("CI").Range("M" & j).Value = strDate
If col2 <> "NA" Then
If IsEmpty(stredate) = False Then
Worksheets("CI").Range("F" & j).Value = datecleanup(stredate)
End If
End If
j = j + 1
End If
EmptyRange:
Next i
End Function
Please review and compare to original code. You can see the quick change made. Indentation helps so much to spot errors and/or opportunities to improve the code.
Function DateOnlyLoad(col As String, col2 As String, colcode As String)
Dim i As Long, j As Long, k As Long
j = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
strDate = spacedate(Worksheets("Data").Range(col & i).Value)
stredate = spacedate(Worksheets("Data").Range(col2 & i).Value)
If (Len(strDate) = 0 And (col2 = "NA" Or Len(stredate) = 0)) Or InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "EXP") > 0 Then
GoTo EmptyRange
Else
If InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "TITER") > 0 Then
Worksheets("Error").Range("A" & k & ":C" & k).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("Error").Range("D" & k).Value = "REVIEW MMR1 DATES"
k = k + 1
Else
Worksheets("CI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("CI").Range("D" & j).Value = colcode
Worksheets("CI").Range("E" & j).Value = datecleanup(strDate)
Worksheets("CI").Range("L" & j).Value = dateclean(strDate)
Worksheets("CI").Range("M" & j).Value = strDate
If col2 <> "NA" Then
If IsEmpty(stredate) = False Then
Worksheets("CI").Range("F" & j).Value = datecleanup(stredate)
End If
End If
j = j + 1
End If
End If
EmptyRange:
Next i
End Function

how to initialise my counter in vba excel

I have a problem with my vba project.
My workbook has 4 sheets (Draft, cky, coy and bey), in the sheet "draft i have all my data and i want to reorganise them. the columns "G" of the sheet "draft" contains the values (cky, coy and bey).
I want my macro to go through the colums and copy all the cells that have the same value and paste them in their corresponding sheet starting at the cell (A2), for exemple: i want the macro to copy all the data that have "cky" and paste it in the sheet "cky" starting at the cell A2 and so on/
Below you can see what i have done so far:
Sub MainPower()
Dim lmid As String
Dim srange, SelData, ExtBbFor As String
Dim lastrow As Long
Dim i, j, k As Integer
lastrow = ActiveSheet.Range("B30000").End(xlUp).Row
srange = "G1:G" & lastrow
SelData = "A1:G" & lastrow
For i = 1 To lastrow
If InStr(1, LCase(Range("E" & i)), "bb") <> 0 Then
Range("G" & i).Value = Mid(Range("E" & i), 4, 3)
ElseIf Left(Range("E" & i), 1) = "H" Then
Range("G" & i).Value = Mid(Range("E" & i), 7, 3)
Else
Range("G" & i).Value = Mid(Range("E" & i), 1, 3)
End If
Next i
'Sorting data
Range("A1").AutoFilter
Range(SelData).Sort key1:=Range(srange), order1:=xlAscending, Header:=xlYes
'Spreading to the appropriate sheets
j = 1
For i = 1 To lastrow
If Range("G" & i).Value = "CKY" Then
Sheets("CKY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
ElseIf Range("G" & i).Value = "BEY" Then
Sheets("BEY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
ElseIf Range("G" & i).Value = "COY" Then
Sheets("COY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
End If
j = j + 1
Next i
End Sub
Thank you to help
best regards
Use this refactored code in the For Loop and it should work for better for you:
For i = 1 To lastrow
Select Case Sheets("Draft").Range("G" & i).Value
Case is = "CKY","COY","BEY"
Dim wsPaste as Worksheet
Set wsPaste = Sheets(Range("G"& i).Value)
Dim lRowPaste as Long
lRowPaste = wsPaste.Range("A" & .Rows.COunt).End(xlup).Offset(1).Row
wsPaste.Range("A" & lRowPaste & ":E" & lRowPaste).Value = _
Sheets("Draft").Range("C" & i & ":G" & i).Value
End Select
Next i

VBA Macro Search Difference in Range

I need to check the range from A1 to AY1, B1 to BY1.... from sheet 1 and compare them with the range from A1 to AY1, B1 to BY1.... from sheet 2 and highlight in yellow the difference in sheet 1.
Sub copyWorkingFileToConsolidated()
Dim i As Integer
i = 5
Do While Sheet2.Range("A" & i).Value <> ""
Sheet3.Range("A" & i & ":D" & i).Value = Sheet2.Range("A" & i & ":D" & i).Value
Sheet3.Range("F" & i & ":Y" & i).Value = Sheet2.Range("E" & i & ":X" & i).Value
Sheet3.Range("A" & i & ":Y" & i).Interior.ColorIndex = 0
i = i + 1
Loop
getResourceLevel
End Sub
Please help
Thanks