I use the above equations in my code to provide a value in Column M.
Now i need to use an "IF-ELSE" statement to pull the value from column M and return the correct corresponding value in Column N.
However, my code keeps taking the value only from Column "N2" and is returning it to all the values in column N.
How do i get the 'IF-ELSE" statement to return adjacent values i.e
(M2-->N2)
(M3-->N3) and so on and so forth
Option Explicit
Sub STADPROJ()
Dim lastrow As Long
lastrow = Range("E" & Rows.Count).End(xlUp).Row
Range("H2:H" & lastrow).Formula = ("=G2 * 1.2")
Range("I2:I" & lastrow).Formula = ("=H2 * 1.5")
Range("J2:J" & lastrow).Formula = ("=I2 * 0.8")
Range("K2:K" & lastrow).Formula = ("=J2 * 1")
Range("L2:L" & lastrow).Formula = "=SUM(G2:K2)"
Range("M2:M" & lastrow).Formula = "=(F2 - L2)"
If Range("M2") < 200 Then
Range("N2:N" & lastrow) = 1008
Else
Range("N2:N" & lastrow) = 0
End If
End Sub
Instead of a VBA If ... Else construct, use a formula. So replace:
If Range("M2") < 200 Then
Range("N2:N" & lastrow) = 1008
Else
Range("N2:N" & lastrow) = 0
End If
with:
Range("N2:N" & lastrow) = "=IF(M2 < 200, 1008, 0)"
You can use a formula again:
Range("N2:N" & lastrow).FormulaR1C1 = "=IF(RC[-1] <200, 1008, 0 )"
Or, if you want to have only resulting values in column N:
With Range("N2:N" & lastrow)
.FormulaR1C1 = "=IF(RC[-1] <200, 1008, 0 )"
.Value = .Value
End With
Related
I am trying to do a VLookUp through an IF/THEN statement.
My Excel formula would be:
=If(F2=G2,Today(),Vlookup(A2,[Range],15,FALSE))
When that would give me an "n/a" error, I would the use the following formula:
=IF(VALUE(LEFT(C2,1))>8,N2+28,N2+14))"
I am trying to combine these in a macro and cannot get the syntax worked out.
Here is what I am 'trying' so far:
Set WS2 = ThisWorkbook.Sheets("Sheet2")
Set WS4 = ThisWorkbook.Sheets("Sheet1 (2)")
For RowNumber = Range("M" & Rows.Count).End(xlUp).Row To 2 Step -1
With WS2
If Range("F" & RowNumber).Value = Range("G" & RowNumber).Value Then Range("O" & RowNumber) = (Now) Else Range("O" & RowNumber) = MyStringVar1 = Application.WorksheetFunction.VLookup(Range("A" & RowNumber), WS4.Range("A2:V5000").Value, 15, False)"
On Error GoTo 0
If MyStringVar1 = "" Then Range("O" & RowNumber).Value = Range("O" & RowNumber).Value = "=IF(VALUE(LEFT(Range("C" & RowNumber).Value,1))>8,Range("N" & RowNumber).Value+28,Range("N" & RowNumber).Value+14))" Else Range("O" & RowNumber).Value = MyStringVar1
End With
Next RowNumber
I suspect you don't need to resort to VBA. If you wrap your VLOOKUP inside an IFERROR formula, something like below, you may be able to manage just with cell formulas.
=IF(F2=G2,Today(),IFERROR(VLOOKUP(A2,$A$2:$V$5000,15,FALSE),IF(VALUE(LEFT(C2,1))>8,N2+28,N2+14)))
I want to concatenate two ranges into one WIHTOUT using a loop.
Below is the code. The lines with comment's is basically the solution I want to avoid.
With ws_AUoM
lCountEntriesInAUoMFile = .Cells(Rows.Count, "B").End(xlUp).Row
.Range("O2:O" & lCountEntriesInAUoMFile).Value = .Range("B2:B" & lCountEntriesInAUoMFile).Value & .Range("F2:F" & lCountEntriesInAUoMFile).Value
' For lLoopCounterAUoM = 2 To lCountEntriesInAUoMFile
'
' .Cells(lLoopCounterAUoM, "O").Value = .Cells(lLoopCounterAUoM, "B").Value & .Cells(lLoopCounterAUoM, "F").Value
'
' Next lLoopCounterAUoM
End With
This line:
.Range("O2:O" & lCountEntriesInAUoMFile).Value = .Range("B2:B" & lCountEntriesInAUoMFile).Value & .Range("F2:F" & lCountEntriesInAUoMFile).Value
returns the error "Type Mismatch". I have double checked the sizes and location of each range. Yet it does not work. What am I missing here?
You can do this:
Dim r As Long
With ws_AUoM
r = .Cells(.Rows.Count, "B").End(xlUp).Row
.Range("O2:O" & r).Value = .Evaluate("B2:B" & r & " & F2:F" & r)
End With
Evaluate knows you're giving it an array formula, and will return the resulting array, which you can assign directly to the sheet.
I want to change dates from text format to date format (custom) dd/mm/yyyy hh:mm . I have been reading all types of similar questions inside the website but nothing seems to work for me. Even if I apply changes, the date stays in a text format. Is there a way to use the Date function in VBA. Or generally, any ideas about how I can finally make it work. My dates are vlookups from an excel sheet named "TMS", where they are in a text format. The destination sheet is "Tracker". The dates are imported from a website to the "TMS" sheet so I have to perform the change in format automatically inside the excel. My code is provided below. Much appreciated!!
The code below is the fixed code, for which the date format worked, but it does not run the loop for every row, instead it just copy paste the value of the first row to the other rows. In other words, it works perfectly for the first row, but not for the other!
Sub Tracker()
Sheets("TMS").Select
lastrow = Range("B" & Rows.Count).End(xlUp).Row
With Range("G2:G" & lastrow)
If Not IsEmpty(Range("G2:G" & lastrow)) Then
.value = .Parent.Evaluate("DATE(MID(" & .Address & ",7,4),MID(" & .Address & ",4,2),LEFT(" & .Address & ",2))+RIGHT(" & .Address & ",4)")
End If
End With
Sheets("Tracker").Select
lastrow = Range("B" & Rows.Count).End(xlUp).Row
With Range("AG2:AG" & lastrow)
.Formula = "=VLOOKUP(B2,TMS!B:G,6,FALSE)"
.value = .value
End With
End Sub
simply add to your With Range("G2:G" & lastrow) part:
.Value = .Parent.Evaluate("DATE(MID(" & .Address & ",7,4),MID(" & .Address & ",4,2),LEFT(" & .Address & ",2))+RIGHT(" & .Address & ",4)")
this should change all strings to numerical values in one step :)
EDIT
As Evaluate does not want to return an array this way, we simply force it via INDEX:
.Value = .Parent.Evaluate("INDEX(DATE(MID(" & .Address & ",7,4),MID(" & .Address & ",4,2),LEFT(" & .Address & ",2))+RIGHT(" & .Address & ",4),)")
in the image above, I have illustrated the formulas used to convert from text to their various components then back to a date serial including time. The format for F2 was set as a custom format to display correctly.
I am not sure how your worksheet is organised but considering the dates are imported to Sheets("TMS").Range("G2:G" & lastrow), and you are not able to change their format by using only .NumberFormat = "mm/dd/yyyy hh:mm" then you need to get rid of the complete text and paste them as dates.
You should also avoid selecting sheets. Your code should look something similar to this. Please correct the parts if I guess them incorrectly.
Sub Tracker()
Dim lastrow As Long
Dim arr() As Date
With Sheets("TMS")
lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
ReDim arr(lastrow) As Date
For i = 2 To lastrow
arr(i) = .Range("G" & i).Value
Next i
.Range("G2:G" & lastrow).Delete
For i = 2 To lastrow
.Range("G" & i) = arr(i)
Next i
.Range("G2:G" & lastrow).NumberFormat = "mm/dd/yyyy hh:mm"
End With
With Sheets("Tracker")
lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
With .Range("AG2:AG" & lastrow)
.Formula = "=VLOOKUP(B2,TMS!B:G,6,FALSE)"
.NumberFormat = "mm/dd/yyyy hh:mm"
End With
End With
End Sub
Try this code.
Sub Tracker()
Dim vDB, vT, vD
Dim i As Long
With Sheets("TMS")
lastrow = .Range("B" & Rows.Count).End(xlUp).Row
With .Range("G2:G" & lastrow)
vDB = .Value
If IsDate(vDB(1, 1)) Then
Else
For i = 1 To UBound(vDB, 1)
vT = Split(vDB(i, 1), " ")
vD = Split(vT(0), "/")
vDB(i, 1) = DateSerial(vD(2), vD(1), vD(0)) + Val(Trim(vT(1)))
Next i
End If
.Value = vDB
.NumberFormat = "mm/dd/yyyy hh:mm"
End With
End With
With Sheets("Tracker")
lastrow = .Range("B" & Rows.Count).End(xlUp).Row
With .Range("AG2:AG" & lastrow)
.Formula = "=VLOOKUP(B2,TMS!B:G,6,FALSE)"
.NumberFormat = "mm/dd/yyyy hh:mm"
.Value = .Value
End With
End With
End Sub
I created a simple form in Excel for inserting data to the spreadsheet. I have a table in my sheet with headlines in row 2 and blank in row 3. When I enter the data form into the first empty row, it started from row 4 (fist row under the table). I want to insert data into existing table. How can I do this?
This is Add button code in my form:
Private Sub Add_Click()
TrackingDate = UserForm1.TrackingDate.Value
INS = UserForm1.INS.Value
COY = UserForm1.COY.Value
Amount = UserForm1.Amount.Value
InvoiceDate = UserForm1.InvoiceDate.Value
InvoiceNumber = UserForm1.InvoiceNumber.Value
PolicyNumber = UserForm1.PolicyNumber.Value
Reminder = UserForm1.Reminder.Value
Cheque = UserForm1.Cheque.Value
Status = UserForm1.Status.Value
Range("A" & Rows.Count).End(xlUp).Offset(1).Value = TrackingDate
Range("B" & Rows.Count).End(xlUp).Offset(1).Value = INS
Range("C" & Rows.Count).End(xlUp).Offset(1).Value = COY
Range("D" & Rows.Count).End(xlUp).Offset(1).Value = Amount
Range("E" & Rows.Count).End(xlUp).Offset(1).Value = InvoiceDate
Range("F" & Rows.Count).End(xlUp).Offset(1).Value = InvoiceNumber
Range("G" & Rows.Count).End(xlUp).Offset(1).Value = PolicyNumber
Range("H" & Rows.Count).End(xlUp).Offset(1).Value = Reminder
Range("I" & Rows.Count).End(xlUp).Offset(1).Value = Cheque
Range("J" & Rows.Count).End(xlUp).Offset(1).Value = Status
End Sub
your code can be written like this:
Private Sub Add_Click()
Dim i&: i = [A:J].Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1 'find the last empty row range("A:J")
With UserForm1
Range("A" & i).Value = .TrackingDate.Value
Range("B" & i).Value = .INS.Value
Range("C" & i).Value = .COY.Value
Range("D" & i).Value = .Amount.Value
Range("E" & i).Value = .InvoiceDate.Value
Range("F" & i).Value = .InvoiceNumber.Value
Range("G" & i).Value = .PolicyNumber.Value
Range("H" & i).Value = .Reminder.Value
Range("I" & i).Value = .Cheque.Value
Range("J" & i).Value = .Status.Value
End With
End Sub
When you use Range("A" & Rows.Count).End(xlUp) approaching an Excel "table", the first cell that registers will be the last cell of the "table" even if that cell is blank. I haven't tested it but suspect if your "table" is all values then it won't auto-extend itself if something is entered below a blank row.
You could try adding an innocuous formula like =Row() in the last column of your table and hiding that column - then the table should auto-extend.
I'm trying to get a script to find the appropriate column and create a formula with that cells "name" in it. This is the script:
'Search for value
Dim i As Integer
i = 4
Do Until Cells(9, i).Value = ddLeveranciers.Value Or Cells(9, i).Value = ""
i = i + 1
Loop
'Add formulas
Range("D5").Formula = "=IF(" & Cells(15, i) & "<>"""",D4*" & Cells(15, i) & ","""")"
This now returns the formula "=IF(1.23<>"",D4*1.23,"")", 1.23 being the value of cells(15,i). I would like the script to return (for example) "=IF(D15<>"",D4*D15,"")". How do I do that?
You can use the .Address property.
Range("D5").Formula = "=IF(" & Cells(15, i).Address & "<>"""",D4*" & Cells(15, i).Address & ","""")"
For example:
MsgBox(Cells(1,1).Address)
Would return $A$1