VB with Excel About Range Use - vba

I'm trying to get the value from each cell from column A of the table compared to a value entered by the user, the problem is that in the line:
If TamañoCmb.Value = Range("A" & Trim(fila)).Value Then
The range value doesn't return any value when it should get the cell a2 in the first loop and enter the if. I debugged the code and isn't returning any value.
Private Sub CalcularBtn_Click()
If NombreTxt.Value = vbNullString Then
MsgBox "Debe ingresar todos los datos"
ElseIf TamañoCmb.Value = vbNullString Then
MsgBox "Debe ingresar todos los datos"
ElseIf CantidadTxt.Value = vbNullString Then
MsgBox "Debe ingresar todos los datos"
End If
Worksheets("Información").Activate
Dim fila As Single
Dim Produccion_Principal As String
Dim Precio_Principal As String
For fila = 2 To 8
If TamañoCmb.Value = Range("A" & Trim(fila)).Value Then
Produccion_Principal = Range("D" & Trim(fila)).Value
Exit For
End If
Next
ProduccionTxt.Value = Val(Produccion_Principal)
For fila = 2 To 8
If TamañoCmb.Value = Range("A" & Trim(fila)).Value Then
Precio_Principal = Range("C" & Trim(fila)).Value
Exit For
End If
Next
PrecioUTxt.Text = Val(Precio_Principal) * CantidadTxt
If Produccion_Principal < CantidadTxt.Value Then
MsgBox "Producto primario mayor a producción, Cambie la cantidad"
End If
If Producto2Marco.Enabled Then
Dim Produccion_Secundaria As String
Dim Precio_Secundario As String
For fila = 2 To 8
If Producto2Cmb.Value = Range("A" & Trim(fila)).Value Then
Produccion_Secundaria = Range("D" & Trim(fila)).Value
Exit For
End If
Next
If Producto2Cmb.Value = Range("A" & Trim(fila)).Value Then
Precio_Secundario = Range("C" & Trim(fila)).Value
End If
PrecioSecTxt.Value = Val(Precio_Secundario)
If Produccion_Secundaria < CantidadSecTxt.Value Then
MsgBox "Producto secundario mayor a Producción, Cambie la cantidad"
End If
End If

I fixed the problem using a variable to store the value of the range like this:
Dim celda As String
celda = Range("A" & Trim(fila)).Value
Thanks to everyone for helpimg solve the issue!

Related

For loop with formulas

I have this code below which I want to be used in a loop. However, instead of C5 and D5, I would want this loop to be run on all the cells in column C and column D and not only for C5 and D5.
To summarize, I would want C5 and D5 to be replaced by every cell in Column C and D. Please assist.
For i = 1 To 5
Valuex = Evaluate("=IsNumber(Value(Mid(C5, 2, 1)))")
MsgBox (Valuex)
Valuex1 = Evaluate("=Left(Trim(C5), 1) = ""R""")
MsgBox (Valuex1)
If ((Evaluate("=Left(Trim(C5), 1) = ""R""") = "True") And (Evaluate("=IsNumber(Value(Mid(C5, 2, 1)))") = "True")) Then
Range("D5").Formula = "=VLOOKUP(C5,[old.xls]Sheet1!$D:$V,19,0)"
MsgBox ("if")
Else
Range("D5").Formula = "=VLOOKUP(C5,[old.xls]Sheet1!$E:$V,18,0)"
MsgBox ("else")
End If
Next i
Think this does what you want. It will run from row 1 to the last row in C. Note that you could do all this without VBA.
Sub x()
Dim i As Long, Valuex As Boolean, Valuex1 As Boolean
For i = 1 To Range("C" & Rows.Count).End(xlUp).Row
Valuex = Evaluate("=IsNumber(Value(Mid(C" & i & ", 2, 1)))")
MsgBox (Valuex)
Valuex1 = Evaluate("=Left(Trim(C" & i & "), 1) = ""R""")
MsgBox (Valuex1)
If Valuex1 And Valuex Then
Range("D" & i).Formula = "=VLOOKUP(C" & i & ",[old.xls]Sheet1!$D:$V,19,0)"
MsgBox ("if")
Else
Range("D" & i).Formula = "=VLOOKUP(C" & i & ",[old.xls]Sheet1!$E:$V,18,0)"
MsgBox ("else")
End If
Next i
End Sub
I think you can avoid the loop altogether thus
Sub xx()
Dim i As Long
i = Range("C" & Rows.Count).End(xlUp).Row
With Range("D1:D" & i)
.Formula = "=IF(AND(ISNUMBER(VALUE(MID(C1, 2, 1))),LEFT(TRIM(C1), 1) = ""R""),VLOOKUP(C1,Sheet1!$D:$V,19,0),VLOOKUP(C1,Sheet1!$E:$V,18,0))"
.Value = .Value
End With
End Sub

VBA Code for Multiple if conditions

I need to categorize my data into different buckets. my worksheet has column V & Column Y (actually a name match & address match respectively) has values that are either "ok" or "check". Column O has IDs, of which some are only numeric and some are alpha numeric.i need to fill my column A based on these 3 columns.
category 1 - Column A to be filled with "Verify name & Address" - logic for this is - If Column A is blank, Column V value = "check", Column Y value = "check" and column O = all alphanumeric IDs (except that starts with CWT) and numeric IDs = 2 & 9612
Category 2 - Column A to be filled with "Verify Address" - logic for this is - If Column A is blank, Column V value = "ok", Column Y value = "check" and column O = all alphanumeric IDs (except that starts with CWT) and numeric IDs = 2 & 9612.
Sub Rules()
'
'Autofill based on Rules
Worksheets("ORD_CS").Activate
Dim sht As Worksheet
Dim LR As Long
Dim i As Long
Set sht = ActiveWorkbook.Worksheets("ORD_CS")
LR = sht.UsedRange.Rows.Count
With sht
For i = 8 To LR
If .Range("A" & i).Value = "" And Range("V" & i).Value = "check" And Range("Y" & i).Value = "check" And Range("O" & i).Value = "2" And Range("O" & i).Value = "9612" Then
.Range("D" & i).Value = "Verify Name & Address"
End If
Next i
End With
End Sub
I have not completed my code. Can someone help? Thanks
The below should work, I changed your O column to be an OR
Edit: for function
Public Function IsAlpha(strValue As String) As Boolean
Dim intPos As Integer
For intPos = 1 To Len(strValue)
Select Case Asc(Mid(strValue, intPos, 1))
Case 65 To 90, 97 To 122
IsAlpha = True
Case Else
IsAlpha = False
Exit For
End Select
Next
End Function
With sht
For i = 8 To LR
If .Range("A" & i).Value = "" And Range("V" & i).Value = "check" And Range("Y" & i).Value = "check" And Range("O" & i).Value = "2" Or Range("O" & i).Value = "9612" Or IsAlpha(Range("O" & i).Value) = True Then
.Range("D" & i).Value = "Verify Name & Address"
Else
If .Range("A" & i).Value = "" AND .Range("V" & i).Value = "ok" AND .Range("O" & i).Value = "2" Or .Range("O" & i).Value = "9612" Then
Do Stuff
End If
End If
Next i
End With

objReminder.Start = Range("h" & i) not working

I have this code:
Sub StoreReminders()
Dim LastRow As Long
Dim i As Integer
LastRow = ActiveSheet.UsedRange.Rows.Count
i = 2
Debug.Print LastRow
Dim appOL As Object
Dim objReminder As Object
Set appOL = GetObject(, "Outlook.application")
Set objReminder = appOL.CreateItem(1) ' olAppointmentItem
For i = 2 To LastRow
Debug.Print i
Debug.Print Range("h" & i)
objReminder.Start = Range("h" & i)
objReminder.Duration = Range("I" & i)
objReminder.Subject = "Renew " & Range("a" & i)
objReminder.ReminderSet = True
objReminder.Save
Next i
End Sub
It breaks at objReminder.Start = Range("h" & i) because according to the Debug.Print code, it is returning the entire column of dates instead of just the date in H2
Does the Range("a" & i) code not work in conjunction with objReminder.Start?
Debug.print prints it out correctly.
If you check the MSDN entry for the Start property then it says:
Returns or sets a Date indicating the starting date and time for the appointment or Journal entry. Read/write.
(their emphasis)
So, in your VBA code, I would change this line:
objReminder.Start = Range("h" & i)
To this:
objReminder.Start = CDate(Range("h" & i).Value)
Using the CDate function which converts a value to a date.

VBA in Excel returning Type mismatch

I'm trying to create a Macro that will modify contents in columns S, W and AH based on the content in AB
e.g. if AB1 = No, then S1=C-MEM, AH = N/A and W is cleared.
For some reason, I get a 'Type mismatch' error on the first line of my if statement and can't figure out why or how to fix it - even after reading other posts about similar issue.
Sub test()
Dim lastrow As Long
Dim i As Long
lastrow = Range("AB" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
**-> If Range("AB" & i).Value = "No" Then
Range("S" & i).Value = "C-MEM"
Range("W" & i).Value = ""
Range("AH" & i).Value = "N/A"
End If
Next i
End Sub
Thanks
You are trying to test if an error is = No.
Test for the error and skip the logic in that loop:
Sub test()
Dim lastrow As Long
Dim i As Long
lastrow = Range("AB" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If Not IsError(Range("AB" & i).Value) Then
If Range("AB" & i).Value = "No" Then
Range("S" & i).Value = "C-MEM"
Range("W" & i).Value = ""
Range("AH" & i).Value = "N/A"
End If
End If
Next i
End Sub

Do while loop code wont play

I am trying to loop through a list of data then and copy the rows that contain "WIP" into a second tab. yet the code will nor do anything when I hit execute. Can someone explain why?
Thank you.
Sub Update_LvL1_WIP()
Dim BrowFi As Integer
Dim BrowWIP1 As Integer
Dim dblSKU As Double
Dim strDescription As String
Dim strType As String
BrowFi = (ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row) + 1
Do While BrowFi > 4
If Range("G" & BrowFi).Value = "WIP" Then
strType = Range("G" & BrowFi).Value
strDescription = Range("F" & BrowFi).Value
dblSKU = Range("E" & BrowFi).Value
Worksheets("WIP 1").Activate
BrowWIP1 = (ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row) +1
Range("A" & BrowWIP1).Value = dblSKU
Range("B" & BrowWIP1).Value = strDescription
Range("B" & BrowWIP1).Value = strType
Else
BrowFi = BrowiF - 1
End If
Loop
End Sub
You have a typo in your decrement variable
BrowFi = BrowiF - 1
should be
BrowFi = BrowFi - 1
Since BrowiF was not defined it was treated as 0 and so you were setting BrowFi to -1 on the first run through the loop.
It may be worth using Option Explicit to help catch these errors.