Conditional Concatenate and Autofill with Value from Adjacent Column - excel

Sub Concatenate ()
Dim LastRow As Long
Dim i As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.Range("K2").Formula = "= TODAY() - I2"
Range("K2").Select
Selection.AutoFill Destination:=Range("K2:K" & LastRow)
For i = 2 To LastRow
If Range("K" & i).Value < 5 Then Range("J2:J" & i).Value = "Week of" & "" & ("I2:I" & i)
Next i
End Sub
I have a spreadsheet that lists item numbers in Column A, and corresponding dates in column I. Not every item will have a date, so I'm basing the LastRow on Column A to work around the gaps. I want dates in the past to return 0 in column J. I want future dates to return "Week of __" there the __ is the date in column I.
I'm not the most familiar with VBA, and I've run into a bit of a snag. With the above, everything returns "Week of9". I know it's a simple answer, but I have been Googling for an hour. I just need to know the syntax to make the above return the value of "I" at the end of the concatenate as it loops down the rows. If this is a duplicate question, I apologize.
Thanks in advance.

Maybe your condition should be:
If Range("K" & i).Value < 5 Then Range("J" & i).Value = "Week of " & Range("I" & i)

Related

Excel VBA to copy rows with a specific criteria [duplicate]

any help with this would be appreciated.
I am trying to copy a cell from column (G) into another worksheet, if cells from the same row in column (R) = "Y" and column (B) = "Month"
I had it working for just "Y" criteria but as soon as I added the "Month" this button has stopped working?
this is the code I have;
Private Sub CommandButton1_Click()
Dim LR As Long, i As Long
With Sheets("Savings Q4")
LR = .Range("R" & Rows.Count).End(xlUp).Row
For i = 1 To LR
With .Range("R" & i)
If .Value = "Y" Then
With .Range("B" & i)
If .Value = "January" Then
Sheets("Savings Q4").Range("G:G").Copy Destination:=Sheets("Cifas Loadings").Range("A:A")
End If
End With
Next i
End With
End Sub
Any help would be appreciated.
Thanks
Too Much 'Withing' and 'Ifing'
Rules
The number of With statements must be equal to the number of End With statements.
The number of If statements must be equal to the number of End If statements, except when there is a 'one-line' If Statement.
Insights
Alexey C's solution gets those With-s and If-s in line, but problems still remain.
When you write With .Range("R" & i) it refers to Sheets("Savings Q4") from the previous With statement and you are actually shortening the line
With Sheets("Savings Q4").Range("R" & i)
which is OK.
When you later write With .Range("B" & i) it doesn't refer to Sheets("Savings Q4"), it refers to .Range("R" & i) i.e. your not shortening the line With Sheets("Savings Q4").Range("B" & i) but
With Sheets("Savings Q4").Range("R" & i).Range("B" & i)
which is WRONG.
Matteo NNZ correctly commented that the If statements could be shortened to one If statement using the And operator.
When you write
Sheets("Savings Q4").Range("G:G").Copy_
Destination:=Sheets("Cifas Loadings").Range("A:A")
you are copying the entire G column to the entire A column. Since this is in a loop (For i = 1 to LR), each time the code 'encounters' "January" and "Yes", it copies the entire column. It is highly unlikely that you are trying to achieve that functionality.
Plan of Changes
Get rid of the 'inner' With's.
Reduce the If's using And.
Change the COLUMN ranges G:G and A:A to the corresponding CELL ranges.
Get rid of Destination:=.
The Code
Sub WithIf()
Dim LR As Long, i As Long
With Sheets("Savings Q4")
LR = .Range("R" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If .Range("R" & i).Value = "Y" And _
.Range("B" & i).Value = "January" Then
.Range("G" & i).Copy Sheets("Cifas Loadings").Range("A" & i)
End If
Next i
End With
End Sub
Remaining Issues
A further problem might be that you didn't want the A column data copied in the same row as the G column data. If this is the case you should apply the same LR = .Range("R" & Rows.Count).End(xlUp).Row logic and integrate it into the code referring to the A column of sheet "Cifas Loadings".
At least, you forgot one End With and one End If. But anyway it's not correct with logical part.
Dim LR As Long, i As Long
With Sheets("Savings Q4")
LR = .Range("R" & Rows.count).End(xlUp).Row
For i = 1 To LR
With .Range("R" & i)
If .Value = "Y" Then
With .Range("B" & i)
If .Value = "January" Then
Sheets("Savings Q4").Range("G:G").Copy Destination:=Sheets("Cifas Loadings").Range("A:A")
End If
End With
End If
End With
Next i
End With
It's easier to make it w/o With, like
if Range("R"& i).value="Y" and Range("B" & i).value = "Month"
COPE CELLS
end if

Autofill not populating until last row

I am trying to populate the date and have it populate the column until the last row of data. However it doesn't seem to populate to the last line of data and I don't understand why. What is wrong with my code?
Sub updatedate()
lastRow = Range("F" & Rows.Count).End(xlUp).Row
Range("F2:F" & lastRow) = Date
Range("F2:F" & lastRow).Style = "Normal"
Range("F2:F" & lastRow).Value = Format(Date, "yyyymmdd")
End Sub
the date in format yyyymmdd to be updated and populated on each row until last in column F
The problem is on the line
lastRow = Range("F" & Rows.Count).End(xlUp).Row
If you change "F" to "A" (or any other suitable column) it will work better.
The last used cell in the F column is not yet used.

Autofill Destination from Active Dynamic Selection to certain cell

I have used the following codes to select the last 3 rows (dynamic position) in fixed columns of so that I can extrapolate till cell Q37.
selection of last 3 rows in column Q R and S
Sheets("Data").Select
LastRow = Range("S" & Rows.Count).End(xlUp).Row
Set Last3Rows = Range("Q" & LastRow - 2, "S" & LastRow)
Last3Rows.Select
I am unable to proceed to autofill these values till row 37 since the last 3 cells' position vary for different worksheets.
The code I used to autofill from the dynamic ending was:
Last3Rows.Select
Range ("Q:S" & Range("Y" & Rows.Count).End(xlUp).Row), Type:=xlFillDefault
Range("Q18:S37").Select
Y is a column with values will row 37.
How can I proceed to autofill?
Notice you din't use .AutoFill method in your example.
I hope I've understood what you asked:
LastRow = Range("S" & Rows.Count).End(xlUp).Row
Set Last3Rows = Range("Q" & LastRow - 2, "S" & LastRow)
Last3Rows.AutoFill Destination:=Last3Rows.Resize(37 - LastRow + Last3Rows.Rows.Count, 3)
This is a line of code that does what I think you are asking. The first part select the range you want to paste and the second part the range you want to copy from.
Sheets("Data").Range("Y37:AA39").Value = Range("Q" & Cells.Rows.Count).End(xlUp).Offset(-2).Resize(3, 3).Value

VBA code to select a cell when specific letters appears and sort

I need some help to understand and modify some code I found in this answer:
Sub Main()
Dim cell As Range
Dim nextRow As Long
For Each cell In Range("K50:K200")
If StrComp(cell, "Late", vbTextCompare) = 0 Then
nextRow = Range("J" & Rows.Count).End(xlUp).Row + 1
Range("J" & nextRow) = Range("B" & cell.Row)
Range("K" & nextRow) = Range("C" & cell.Row)
Range("N" & nextRow) = Range("G" & cell.Row)
Range("O" & nextRow) = Range("H" & cell.Row)
End If
Next
End Sub
What I'd like is:
If late is along with some other words, say late submit, copy values in B and paste in J
if row K has progress along with say in progress, copy values in C to K
If sick is with say on sick leave, copy values in G to N
I tried so hard, but I still didn't succeed in modifying this code to suit my needs.
If late is along with some other words, say late submit
In that case, instead of StrComp, use Instr
If Instr(1, cell, "Late", vbTextCompare) > 0 Then
Similarly for the rest.
If you check Excel's help you will notice that Instr returns a Variant (Long) specifying the position of the first occurrence of one string within another.
Syntax
InStr([start, ]string1, string2[, compare])

VBA script when dividing by zero

I am trying to do a formula in VBA, but I am coming across errors because of a zero in the denominator. If I have a zero in the denominator, I'd like the active cell to be set to zero. Whatever I am doing is incorrect, and I am not a programmer. I have no idea what I'm doing. Any help would be greatly appreciated. Here is what I
Range("H" & Row).Activate
If Range("F" & Row) = 0 Then ActiveCell.Formula = 0
ActiveCell.Formula = Range("G" & Row) / Range("F" & Row)
try using an else
something like
Range("H" & Row).Activate
If Range("F" & Row) = 0 Then
ActiveCell.Formula = 0
Else
ActiveCell.Formula = Range("G" & Row) / Range("F" & Row)
End If
You could just build the statement into the formula:
For Row = 1 To 3
Cells(Row, "H").Formula = "=IF(F" & Row & "=0,0,G" & Row & "/F" & Row & ")"
Next Row
And instead of using a loop...
With Range("H2:H" & Cells(Rows.Count, "G").End(xlUp).Row)
.Formula = "=IF(F" & .Row & "=0,0,G" & .Row & "/F" & .Row & ")"
.Value = .Value
End With
I do not recommend Cor_Blimey or tigeravatar recommend - both make the code less readable. Cor_Blimey's suggestion to use the line continuation character " _" and the end of statement character ":" do not add to the succinctness of the code and is logically the same as the code above. Tigeravatar is sortof on the right track with suggesting that the cell handle the error case itself, but his code suggestion makes everything more difficult to read.
My suggested change is to drop the VBA entirely and use the following formula in the cell(s) in column H that would be doing the calculations:
=IfError(G1/F1,0)
This would work because you are not using any values in your calculations other than what is already on the worksheet.

Resources