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
Related
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
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'm really new to VBA and I only get by with copying codes from solutions on this site but for this one I can't find a similar problem.
I want to copy only certain cells (not whole rows) when column A and B aligns as "apple" with "cat". I also want to delete that row I copied from.
the plan illustrated
My code so far:
Sub animals()
Dim rcnt As Long
rcnt = Worksheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To rcnt
If Range("B" & i).Value = "cat" And ("A" & i) = "apple" Then
Range("C" & i).Offset(1, 0).Copy
Range("D" & i).Offset(1, 0).Copy
End If
Next i
End Sub
For i = 1 To rcnt
If Range("B" & i).value = "cat" And ("A" & i) = "apple" Then
Range("C" & i).value = Range("C" & i).Offset(1, 0).value
Range("D" & i).value = Range("D" & i).Offset(1, 0).value
End If
Next i
You were copying the data, just not pasting it anywhere. This method will skip the .Copy (skipping the clipboard), and just sets the values equal.
I have a formula which concatenates strings in different columns. It works great when there is data in each of the columns but if one column is blank I get an error "invalid procedure call or argument" for the string formed by the empty column. Is there a clause i can add into my code to ignore the string if it is empty?
Sub Concatenation_for_the_nation()
'Range("H2").End(xlDown).Select
Cells(rows.Count, "H").End(xlUp).Select
For i = 1 To ActiveCell.Row
Range("H" & i).Select
StrStrONE = StrStrONE & "" & Selection
Next i
Cells(1, 1).Select
'Range("I2").End(xlDown).Select
Cells(rows.Count, "I").End(xlUp).Select
For j = 1 To ActiveCell.Row
Range("I" & j).Select
StrStrTWO = StrStrTWO & "" & Selection
Next j
Cells(1, 1).Select
'Range("J2").End(xlDown).Select
Cells(rows.Count, "J").End(xlUp).Select
For k = 1 To ActiveCell.Row
Range("J" & k).Select
StrStrTHREE = StrStrTHREE & "" & Selection
Next k
Cells(1, 1).Select
'Range("K2").End(xlDown).Select
Cells(rows.Count, "K").End(xlUp).Select
For l = 1 To ActiveCell.Row
Range("K" & l).Select
StrStrFOUR = StrStrFOUR & "" & Selection
Next l
Cells(1, 1).Select
StrStrONE = Trim(StrStrONE)
StrStrTWO = Trim(StrStrTWO)
StrStrTHREE = Trim(StrStrTHREE)
StrStrTHREE = Left(StrStrTHREE, Len(StrStrTHREE) - 3)
StrStrFOUR = Trim(StrStrFOUR)
StrStrFOUR = Left(StrStrFOUR, Len(StrStrFOUR) - 3)
Cells(14, 7) = "(ISAV(" & StrStrONE & " " & StrStrTWO & " " & StrStrTHREE & ")=1 OR (" & StrStrFOUR & ")=1)=1"
Cells(14, 7).Select
End Sub
You can check if the columns are not empty by using ISBLANK() function
As user2471313 said used ISBLANK() function or I would add something like this for checking the string:
If StrStrONE<>"" and StrStrTWO<>"" and StrStrTHREE<>"" and StrStrFOUR<>"" then
StrStrONE = Trim(StrStrONE)
''''your code until end
End if
I have a problem with values i try to copy...
Let's say the value of A33 is 1655, formula for the cell is IFERROR('L-Logic'!G14;"") and when copied it displays 0 and if i cklick on that copied cell it displays the formula. why is that? Should i have to paste special values? Any suggestions?
Am I on the right path? And if I wan't to check cells values, I was thinking to use this..
If ws.Cells(i, 1) <> "blabla" Then ws.Range("A1:A50" & lastrow).Copy Destination:=Work
Below is my sample code. Regards
For Each Ws In Sheets(Array("List 1", "list 2", "List3"))
lastrow = Ws.Range("A" & Rows.Count).End(xlUp).row
For i = 1 To lastRow
If ws.cells(i, 1)<> "testtest" Then
Ws.Range("C1:C50" & lastrow).Copy Destination:=Worksheets("Master list").Range("D" & lastRowMaster)
Ws.Range("A1:A50" & lastrow).Copy Destination:=Worksheets("Master list").Range("A" & lastRowMaster)
Ws.Range("L1:L50" & lastrow).Copy Destination:=Worksheets("Master list").Range("B" & lastRowMaster)
Ws.Range("L1:L50" & lastrow).Copy Destination:=Worksheets("Master list").Range("C" & lastRowMaster)
lastRowMaster = lastRowMaster + Range("C1:C" & lastrow).Rows.Count
Next i
Next
I like what you did with lastRowMaster.
For direct value transfer, you would reverse them.
lrm = 1
With Worksheets("MasterList")
For Each ws In Sheets(Array("List1", "List2", "List3"))
lr = ws.Range("A" & Rows.Count).End(xlUp).Row
.Range("D" & lrm).Resize(lr, 1) = ws.Range("C1:C" & lr).Value2
.Range("A" & lrm).Resize(lr, 1) = ws.Range("A1:A" & lr).Value2
.Range("B" & lrm).Resize(lr, 1) = ws.Range("L1:L" & lr).Value2
.Range("C" & lrm).Resize(lr, 1) = ws.Range("L1:L" & lr).Value2
lrm = lrm + Range("A1:A" & lr).Rows.Count
Next
'...
End With
The destination needs to be resized to accommodate the value transfer. It is not sufficient to just reference the top cell like copy and paste. Be careful with the ones that start at row 7. You will have to do a little maths to get the Range.Resize property correct.