Thanks to this site I adapted a code for my own needs , this macro allows me to compare specific cells of an X rows with another cells of Y row and there is a match between all cells not only 1 or 2 cells but all cells of that row then it give 1 and 0 if no match is found
the problem that the macro is comparing row X with Y which what I want but i doesn't compare X with Y+1 or Y+2 , it passes directly to X+1 and compare it with Y+1
to better understand my problem see below :
in this image two line with yellow color are identical and in column AO there is 1 infront of the first line and 1 infront of the second line
so what is to compare every row with the other rows(X with Y,Y+1,Y+2) and passe on to the next Row and when there is match that is highlighted or returns 1 in column "AO"
Code :
Sub check()
Dim check1, check2 As Variant
Dim l As Long
Dim ElementsSame As Boolean
For i = 2 To 74
check1 = Array(Range("F" & i), Range("G" & i), Range("H" & i), Range("I"
& i), Range("J" & i), Range("K" & i), Range("L" & i), Range("M" & i),
Range("N" & i), Range("O" & i), Range("P" & i), Range("Q" & i),
Range("R" & i), Range("S" & i), Range("T" & i), Range("U" & i),
Range("V" & i), Range("W" & i), Range("X" & i), Range("Y" & i),
Range("Z" & i), Range("AA" & i), Range("AB" & i), Range("AC" & i),
Range("AD" & i), Range("AE" & i), Range("AF" & i), Range("AG" & i),
Range("AH" & i), Range("AI" & i), Range("AJ" & i), Range("AL" & i),
Range("AM" & i))
For D = 3 To 74
check2 = Array(Range("F" & D), Range("G" & D), Range("H" & D), Range("I"
& D), Range("J" & D), Range("K" & D), Range("L" & D), Range("M" & D),
Range("N" & D), Range("O" & D), Range("P" & D), Range("Q" & D),
Range("R" & D), Range("S" & D), Range("T" & D), Range("U" & D),
Range("V" & D), Range("W" & D), Range("X" & D), Range("Y" & D),
Range("Z" & D), Range("AA" & D), Range("AB" & D), Range("AC" & D),
Range("AD" & D), Range("AE" & D), Range("AF" & D), Range("AG" & D),
Range("AH" & D), Range("AI" & D), Range("AJ" & D), Range("AL" & D),
Range("AM" & D))
ElementsSame = True
For l = 0 To 32
If check1(l) <> check2(l) Then
ElementsSame = False
Exit For
End If
Next l
If ElementsSame = False Then
Range("AO" & i) = 1
Range("AO" & D) = 1
Else
Range("AN" & D) = 0
End If
Next D
Next i
End Sub
Anyone can light me about solving this and If anything is not clear about my problem , please feel free to ask
B.R
Polos
Can you try this (not tested on my end)?
Option Explicit
Sub check()
Dim check1, check2 As Variant
Dim i, d As Long
For i = 2 To 74
check1 = Array(Range("F" & i), Range("G" & i), Range("H" & i), Range("I" _
& i), Range("J" & i), Range("K" & i), Range("L" & i), Range("M" & i), _
Range("N" & i), Range("O" & i), Range("P" & i), Range("Q" & i), _
Range("R" & i), Range("S" & i), Range("T" & i), Range("U" & i), _
Range("V" & i), Range("W" & i), Range("X" & i), Range("Y" & i), _
Range("Z" & i), Range("AA" & i), Range("AB" & i), Range("AC" & i), _
Range("AD" & i), Range("AE" & i), Range("AF" & i), Range("AG" & i), _
Range("AH" & i), Range("AI" & i), Range("AJ" & i), Range("AL" & i), _
Range("AM" & i))
For d = 3 To 74
check2 = Array(Range("F" & d), Range("G" & d), Range("H" & d), Range("I" _
& d), Range("J" & d), Range("K" & d), Range("L" & d), Range("M" & d), _
Range("N" & d), Range("O" & d), Range("P" & d), Range("Q" & d), _
Range("R" & d), Range("S" & d), Range("T" & d), Range("U" & d), _
Range("V" & d), Range("W" & d), Range("X" & d), Range("Y" & d), _
Range("Z" & d), Range("AA" & d), Range("AB" & d), Range("AC" & d), _
Range("AD" & d), Range("AE" & d), Range("AF" & d), Range("AG" & d), _
Range("AH" & d), Range("AI" & d), Range("AJ" & d), Range("AL" & d), _
Range("AM" & d))
If Join(check1, Chr(0)) = Join(check2, Chr(0)) Then
Range("AO" & i) = 1
Range("AO" & d) = 1
Else
Range("AN" & d) = 0
End If
Next d
Next i
End Sub
Related
Hi I am newbies about vba. I want to compare and count the cell number that continuously lowers the previous cell however the code just successfully counts 1-2 days and n/a only.
Thanks
data set
Sub Mars()
Dim i As Integer
Sheets("working").Select
For i = 2 To 111
If Range("U" & i).Value > Range("T" & i).Value Then
Range("W" & i).Value = "n/a"
ElseIf Range("U" & i).Value < Range("T" & i).Value Then
Range("W" & i).Value = "1"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value Then
Range("W" & i).Value = "2"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value Then
Range("W" & i).Value = "3"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value Then
Range("W" & i).Value = "4"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value Then
Range("W" & i).Value = "5"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value Then
Range("W" & i).Value = "6"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value Then
Range("W" & i).Value = "7"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) Then
Range("W" & i).Value = "8"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value Then
Range("W" & i).Value = "9"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value < Range("K" & i).Value Then
Range("W" & i).Value = "10"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value < Range("K" & i).Value < Range("J" & i).Value Then
Range("W" & i).Value = "11"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value < Range("K" & i).Value < Range("J" & i).Value < Range("I" & i).Value Then
Range("W" & i).Value = "12"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value < Range("K" & i).Value < Range("J" & i).Value < Range("I" & i).Value < Range("H" & i).Value Then
Range("W" & i).Value = "13"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value < Range("K" & i).Value < Range("J" & i).Value < Range("I" & i).Value < Range("H" & i).Value < Range("G" & i).Value Then
Range("W" & i).Value = "14"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value < Range("K" & i).Value < Range("J" & i).Value < Range("I" & i).Value < Range("H" & i).Value < Range("G" & i).Value < Range("F" & i).Value Then
Range("W" & i).Value = "15"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value < Range("K" & i).Value < Range("J" & i).Value < Range("I" & i).Value < Range("H" & i).Value < Range("G" & i).Value < Range("F" & i).Value < Range("E" & i).Value Then
Range("W" & i).Value = "16"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value < Range("K" & i).Value < Range("J" & i).Value < Range("I" & i).Value < Range("H" & i).Value < Range("G" & i).Value < Range("F" & i).Value < Range("E" & i).Value < Range("D" & i).Value Then
Range("W" & i).Value = "17"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value < Range("K" & i).Value < Range("J" & i).Value < Range("I" & i).Value < Range("H" & i).Value < Range("G" & i).Value < Range("F" & i).Value < Range("E" & i).Value < Range("D" & i).Value < Range("C" & i).Value Then
Range("W" & i).Value = "18"
ElseIf Range("U" & i).Value < Range("T" & i).Value < Range("S" & i).Value < Range("R" & i).Value < Range("Q" & i).Value < Range("P" & i).Value < Range("O" & i).Value < Range("N" & i).Value < Range("M" & i) < Range("L" & i).Value < Range("K" & i).Value < Range("J" & i).Value < Range("I" & i).Value < Range("H" & i).Value < Range("G" & i).Value < Range("F" & i).Value < Range("E" & i).Value < Range("D" & i).Value < Range("C" & i).Value < Range("B" & i).Value Then
Range("W" & i).Value = "19"
End If
Next I
End Sub
In your code, the're a lot of this construction:
IF A < B < C < ...
This is not correct, it should be:
IF (A < B) AND (B < C) AND (C < ...) ...
For readability reasons, I would advise you the following formatting:
IF (A < B) AND_
(B < C) AND_
(C < ...) ...
(The underscore means that the "source code line" is not finished and continues on the next line.)
I have written multiple scripts in VBA for multiple Buttons in an excel sheet to automate a process for the same. All I want is for someone to review my code and critic its cleanliness. It would be very helpful to have suggestions on how to make the code cleaner and optimize the process.
Private Sub CommandButton1_Click()
Dim last_row As Double
Call ModelwithEach
last_row = Range("F" & Rows.Count).End(xlUp).Row
Range("AM1").Value = "WithEach"
Range("AM3:AM" & last_row).Formula = "=F3&R3"
Range("L3:L" & last_row).Formula = "=VLOOKUP(AM3,'[Indemed Datafeed Latest.xlsm]Sheet1'!$A:$B,2,0)"
Range("M3:M" & last_row).Formula = "=VLOOKUP(AM3,'[Indemed Datafeed Latest.xlsm]Sheet1'!$A:$K,11,0)"
Range("N3:N" & last_row).Formula = "=VLOOKUP(AM3,'[Indemed Datafeed Latest.xlsm]Sheet1'!$A:$H,8,0)"
Cells.SpecialCells(xlCellTypeFormulas, xlErrors).Clear
End Sub
Private Sub CommandButton2_Click()
Dim last_row As Double
last_row = Range("U" & Rows.Count).End(xlUp).Row
Range("V3:V" & last_row).Formula = "=VLOOKUP(U3,'[current pricing sheet july 2019 - Copy.xlsx]Sheet1'!$A:$B,2,0)"
Range("W3:W" & last_row).Formula = "=VLOOKUP(U3,'[current pricing sheet july 2019 - Copy.xlsx]Sheet1'!$A:$B,2,0)"
Dim Shipping As Double
Dim i As Double
Shipping = Range("W" & Rows.Count).End(xlUp).Row
For i = 3 To Shipping
If Range("W" & i).Value >= 70 Then
Range("X" & i).Value = "Free Shipping"
Range("Y" & i).Value = 0
Range("Z" & i).Value = "Yes"
ElseIf Range("W" & i).Value <= 69.99 Then
Range("X" & i).Value = "Really Flat"
Range("Y" & i).Value = 6.99
Range("Z" & i).Value = "No"
Else
End If
Next i
End Sub
Private Sub CommandButton3_Click()
Dim last_row As Double
Dim i As Double
last_row = Range("F" & Rows.Count).End(xlUp).Row
For i = 3 To last_row
If Range("N" & i).Value > Range("K" & i).Value Then
Range("G" & i).Value = "McKesson"
ElseIf Range("K" & i).Value > Range("N" & i).Value Then
Range("G" & i).Value = "Independence Medical"
End If
Next i
Range("XFD2:XFD" & last_row).Formula = "=UPPER(LEFT(Q2,2))"
Range("S3:S" & last_row).Formula = "=PROPER(VLOOKUP(F3,'MediUSA wound Care Feed.csv'!$A:$G,7,0))"
For i = 2 To last_row
Range("T" & i).Value = Range("R" & i) & "/" & Range("S" & i)
Next i
'If Range("M2:M" & last_row).Value = "Case" Then
' Range("B2:B" & last_row).Formula = "=UPPER(LEFT(Q2,2))&F2"
' Range("B2:B" & last_row).Value = Range("B" & i).Value & "-CS"
'ElseIf Range("M" & i) = "Box" Then
' Range("B2:B" & last_row).Formula = "=UPPER(LEFT(Q2,2))&F2"
' Range("B" & i).Value = Range("B" & i).Value & "-BX"
' End If
' Next i
For i = 2 To last_row
If Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Case" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-CS"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Each" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & ""
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Box" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-BX"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Pair" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-PR"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Package" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-PK"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Carton" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-CT"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Dozen" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-DZ"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Vial" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-VL"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Roll" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-RL"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Tray" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-TR"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Can" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-CN"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Jar" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-JR"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Bag" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-BG"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Gallon" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-GL"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Set" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-ST"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Kit" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-KT"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Gross" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-GR"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Pad" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-PD"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Tube" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-TU"
ElseIf Range("F" & i).Value = Range("F" & i).Value And Range("S" & i) = "Sleeve" Then
Range("B" & i).Value = Range("XFD" & i) & Range("F" & i) & "-SL"
Else
Range("B" & i).Value = ""
End If
Next i
End Sub
Private Sub CommandButton4_Click()
Dim last_row As Double
Dim i As Double
last_row = Range("F" & Rows.Count).End(xlUp).Row
For i = 2 To last_row
If InStr(1, UCase(Range("B" & i)), "-MI") <> 0 Then
Range("F" & i).Value = Range("F" & i + 1)
Range("G" & i).Value = Range("G" & i + 1)
Range("U" & i).Value = Range("U" & i + 1)
Range("V" & i).Value = Range("V" & i + 1)
Range("W" & i).Value = Range("W" & i + 1)
Range("X" & i).Value = Range("X" & i + 1)
Range("Y" & i).Value = Range("Y" & i + 1)
Range("Z" & i).Value = Range("Z" & i + 1)
Range("D" & i).Value = "Parent Matrix Item"
Range("A" & i & ":AL" & i).Interior.Color = vbYellow
Range("A" & i - 1 & ":AL" & i - 1).Value = Range("A1:AL1").Value
Range("A" & i - 1 & ":AL" & i - 1).Interior.Color = vbGreen
Range("AE" & i).Value = "<p>Warranty and stuff</p>"
Range("AF" & i).Value = "<p>Return Policy</p>"
Range("AA" & i).Value = Range("C" & i) & "|" & Range("F" & i)
Range("Q" & i).Value = Range("Q" & i + 1)
Range("E" & i + 1 & ":E" & last_row).Value = Range("A" & i)
Range("E" & i).Value = " "
Else
Range("D" & i).Value = "Child Matrix Item"
End If
Next i
Range("AG2:AG" & last_row).Formula = "Supply Item"
End Sub
Private Sub CommandButton5_Click()
Cells.SpecialCells(xlCellTypeFormulas, xlErrors).Clear
End Sub
Private Sub CommandButton6_Click()
Dim last_row As Double
last_row = Range("F" & Rows.Count).End(xlUp).Row
Range("H3:H" & last_row).Formula = "=VLOOKUP(AM3,'[Mck Merge Sheet.xlsx]Sheet1'!$A:$D,4,0)"
Range("J3:J" & last_row).Formula = "=VLOOKUP(AM3,'[Mck Merge Sheet.xlsx]Sheet1'!$A:$H,8,0)"
Range("K3:K" & last_row).Formula = "=VLOOKUP(AM3,'[Mck Merge Sheet.xlsx]Sheet1'!$A:$J,10,0)"
Cells.SpecialCells(xlCellTypeFormulas, xlErrors).Clear
End Sub
Any word of advice will be much appreciated and thanked for.
My final goal is to print my cells pipe delimited so in order to do so I am trying to print everything on each row into cell AB on each row. I am trying to loop through each row to do so however I am currently getting the top row of code repeated in all my rows instead of each row individually being printed.
Sub print_misc()
Dim cell As Range
Dim lastRow As Long
Sheets("1099-Misc_Form_Template").Select
lastRow = Range("B" & Rows.Count).End(xlUp).row
For Each cell In Range("AB2:" & "AB" & lastRow)
cell.Value = Range("B2") & "|" & Range("C2") & "|" & Range("D2") & "|" & Range("E2") & "|" & Range("F2") & "|" & Range("G2") & "|" & Range("H2") & "|" & Range("I2") & "|" & Range("J2") & "|" & Range("L2") & "|" & Range("M2") & "|" & Range("N2") & "|" & Range("O2") & "|" & Range("P2") & "|" & Range("Q2") & "|" & Range("R2") & "|" & Range("S2") & "|" & Range("U2") & "|" & Range("V2") & "|" & Range("W2") & "|" & Range("X2") & "|" & Range("Y2") & "|" & Range("Z2") & "|" & Range("AA2")
Next
End Sub
Each cell in AB shows the result of the combined cells in that row (pipe delimited).
Current output:
Expected output:
You aren't incrementing the value of the row for each iteration of cell. You are point at row 2 for each one.
You also shouldn't use Select it is unnecessary just directly reference the sheet object.
Sub print_misc()
Dim cell As Range
Dim lastRow As Long
dim iter as long
with Sheets("1099-Misc_Form_Template")
lastRow = .Range("B" & Rows.Count).End(xlUp).row
iter = 2
For Each cell In .Range("AB2:" & "AB" & lastRow)
cell.Value = .Range("B" & iter) & "|" & .Range("C" & iter) & "|" & _
.Range("D" & iter) & "|" & .Range("E" & iter) & "|" & _
.Range("F" & iter) & "|" & .Range("G" & iter) & "|" & _
.Range("H" & iter) & "|" & .Range("I" & iter) & "|" & _
.Range("J" & iter) & "|" & .Range("L" & iter) & "|" & _
.Range("M" & iter) & "|" & .Range("N" & iter) & "|" & _
.Range("O" & iter) & "|" & .Range("P" & iter) & "|" & _
.Range("Q" & iter) & "|" & .Range("R" & iter) & "|" & _
.Range("S" & iter) & "|" & .Range("U" & iter) & "|" & _
.Range("V" & iter) & "|" & .Range("W" & iter) & "|" & _
.Range("X" & iter) & "|" & .Range("Y" & iter) & "|" & _
.Range("Z" & iter) & "|" & .Range("AA" & iter)
iter = iter + 1
Next
end with
End Sub
Writing macros to copy cells in a different workbook in a specific format.Getting error at different lines everytime I run the code
I tried with unhide cells, selection
For i = 1 To lastrow
If IsEmpty(ThisWorkbook.Sheets("Summary").Range("A" & i).Value) = False Then
If ThisWorkbook.Sheets("Summary").Range("A" & i).Font.Bold = True Then
'Range("A" & i).Copy Range("B" & i)
Set BoldTitle = ThisWorkbook.Sheets("Summary").Range("A" & i)
x = i
Else
ws.Range("A" & i).Value = "Winter I"
BoldTitle.Copy
ws.Range("B" & i).PasteSpecial xlPasteValues
ThisWorkbook.Sheets("Summary").Range("A" & i).Copy
ws.Range("C" & i).PasteSpecial xlPasteValues
ThisWorkbook.Sheets("Summary").Range("B" & i).Copy
ws.Range("D" & i).PasteSpecial xlPasteValues
ThisWorkbook.Sheets("Summary").Range("C" & i).Copy
ws.Range("E" & i).PasteSpecial xlPasteValues
ThisWorkbook.Sheets("Summary").Range("D" & i).Copy
ws.Range("F" & i).PasteSpecial xlPasteValues
End If
Else
End If
Next i
I'm pretty new to VBA and I've been trying to use a macro to paste a few formulas in cells. Problem is, everytime I do so, I get a 1004 error on the very first formula.
I strongly suspect it's because of quotes but I can't for the life of me understand what goes wrong. Would you guys have any idea ?
Sub PREF()
Dim lastrow As Long
lastrow = Range("'Extract WIN'!A" & Rows.Count).End(xlUp).Row
Dim P2 As Worksheet
Set P2 = Sheets("PRO")
Select Case MsgBox("Do you want to proceed with" & P2.[C2].Value & " ?", vbYesNo, "as datepref")
Case vbYes
Sheets("Extract WIN").Select
Range("W2" & ":" & "W" & lastrow).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10];7;2);""/"";MID(RC[-10];5;2);""/"";MID(RC[-10];1;4)));TEXT(,))"
Range("Y2" & ":" & "Y" & lastrow).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10];7;2);""/"";MID(RC[-10];5;2);""/"";MID(RC[-10];1;4)));TEXT(,))"
Range("AA2" & ":" & "AA" & lastrow).FormulaR1C1 = "=IFERROR(IF(AND(Provision!R2C3-RC[-4]<366;RC[-18]>0);RC[-18];0);TEXT(,))"
Range("AB2" & ":" & "AB" & lastrow).FormulaR1C1 = "=IFERROR(RC[-1]*RC[-18];TEXT(,))"
Range("AC2" & ":" & "AC" & lastrow).FormulaR1C1 = "=IF(AND(RC[-2]=0;RC[-20]>0;RC[-4]>Provision!R2C6;ISNA(VLOOKUP(RIGHT(TEXT(RC[-25];""000#####"");4);Provision!R7C17:R101C18;1;FAUX))=FAUX);1;0)"
Range("AD2" & ":" & "AD" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-20]"
Range("AE2" & ":" & "AE" & lastrow).FormulaR1C1 = "=IFERROR(RC[-22]-RC[-4]-RC[-2];TEXT(,))"
Range("AF2" & ":" & "AF" & lastrow).FormulaR1C1 = "=IF(AND(RC[-20]>0;RC[-1]>0);ROUND(MIN(RC[-20]*12;RC[-1]);0);0)"
Range("AG2" & ":" & "AG" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-23]"
Range("AH2" & ":" & "AH" & lastrow).FormulaR1C1 = "=RC[-2]-RC[-18]"
Range("AI2" & ":" & "AI" & lastrow).FormulaR1C1 = "=IFERROR(RC[-4]-RC[-3];TEXT(,))"
Range("AJ2" & ":" & "AJ" & lastrow).FormulaR1C1 = "=IF(RC[-24]>0;ROUND(MIN(RC[-24]*12;RC[-1]);0);0)"
Range("AK2" & ":" & "AK" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-27]"
Range("AL2" & ":" & "AL" & lastrow).FormulaR1C1 = "=RC[-2]-RC[-21]"
Range("AM2" & ":" & "AM" & lastrow).FormulaR1C1 = "=IFERROR(RC[-4]-RC[-3];TEXT(,))"
Range("AN2" & ":" & "AN" & lastrow).FormulaR1C1 = "=IF(AND(RC[-16]>Provision!R2C7;RC[-28]>=0);RC[-1];0)"
Range("AO2" & ":" & "AO" & lastrow).FormulaR1C1 = "=IFERROR(RC[-1]*RC[-31];TEXT(,))"
Range("AP2" & ":" & "AP" & lastrow).FormulaR1C1 = "=IF(RC[-18]=TEXT(,);0;IF(AND(X2<Provision!R2C7;RC[-3]>0);RC[-3];0))"
Range("AQ2" & ":" & "AQ" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-33]"
Range("AR2" & ":" & "AR" & lastrow).FormulaR1C1 = "=IF(RC[-20]="""";RC[-5];0)"
Range("AS2" & ":" & "AS" & lastrow).FormulaR1C1 = "=IFERROR(RC[-1]*RC[-35];TEXT(,))"
Range("AT2" & ":" & "AT" & lastrow).FormulaR1C1 = "=IFERROR(RC[-6]+RC[-4]+RC[-2];TEXT(,))"
Range("AU2" & ":" & "AU" & lastrow).FormulaR1C1 = "=IFERROR(RC[-6]+RC[-4]+RC[-2];TEXT(,))"
Range("AV2" & ":" & "AV" & lastrow).FormulaR1C1 = "=IFERROR(RC[-2]-RC[-30];TEXT(,))"
Range("AX2" & ":" & "AX" & lastrow).FormulaR1C1 = "=IFERROR(RC[-13]*0,5;TEXT(,))"
Range("AY2" & ":" & "AY" & lastrow).FormulaR1C1 = "=IFERROR(RC[-4]*0,9;TEXT(,))"
Range("AZ2" & ":" & "AZ" & lastrow).FormulaR1C1 = "=IFERROR(RC[-2]+RC[-1];TEXT(,))"
Range("BA2" & ":" & "BA" & lastrow).FormulaR1C1 = "=IFERROR(RANK(RC[-1];RC:RC;0);TEXT(,))"
Columns("AA:AZ").NumberFormat = "#,##0"
Columns("W:BA").EntireColumn.AutoFit
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
Range("AI2").Select
Sheets("PRO").Select
Case vbNo
P2.[C2].Select
End Select
End Sub
I'm sorry for the wall of code but I have no idea where I did something wrong, and was advised to give you guys full context
You need to replace the ";" with regular ",".
For example, take this line of code:
Range("W2:" & "W" & LastRow).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10];7;2);""/"";MID(RC[-10];5;2);""/"";MID(RC[-10];1;4)));TEXT(,))"
And change it to this:
Range(Cells(2, 1), Cells(2, LastRow)).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10],7,2),""/"",MID(RC[-10],5,2),""/"",MID(RC[-10],1,4))),TEXT(,))"
Also, keep in mind I made a few changes to this line. I changed it so that it is using Cells instead of range. I find Cells are easier to work with and provide better readability because we don't have to join any strings.
Either way is fine, it just boils down to preference.