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.
Related
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
I am trying to update the date in the format of 20190715 (yyyymmdd) to column F under the header. My code seems to return hashes and I cannot understand why.
lastRow = Range("F" & Rows.Count).End(xlUp).Row
Range("F2:F" & lastRow).Value = Format(Date, "yyyymmdd")
End Sub
All the cells populated with the current date in yyyymmdd
Does this do the job?
Range("F2:F" & lastRow).NumberFormat = "yyyymmdd;#"
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)
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
I am trying to create an allocation system, and whilst I've programmed VBA before I'm stuck this time!
I have data across five columns, the contents of which determine which staff member I allocate to them.
What I need to do is this:
If column E = "a", then put name "xx" in column A (of same row)
If column E = "b", then put name "yy" in column A (of same row)
If column E is blank, then move to next criteria....
If column D is "c" then put name "zz" in column A (of same row)
If column D is "d" then move to next criteria....
If column A is blank then put name "ww" in column A.
Thanks in advance!!
Sub Allocate()
Dim i As Long
For i = 1 To Range("E" & Rows.Count).End(xlUp).Row
If Range("E" & i) = "a" Then
Range("A" & i) = "xx"
ElseIf Range("E" & i) = "b" Then
Range("A" & i) = "yy"
End If
If Range("D" & i) = "c" Then
Range("A" & i) = "zz"
End If
If IsEmpty(Range("A" & i)) Then
Range("A" & i) = "ww"
End If
Next i
End Sub
Note that
If column E is blank, then move to next criteria....
isn't included anywhere in the code, there is no point to implement this in the code because it literally does nothing :)
Like I mentioned in the link under your question, you don't need loop for this.
If you were to use an Excel formula then you would use something like this
=IF(E1="a","xx",IF(E1="b","yy",IF(D1="c","zz","ww")))
Simply use that in the vba code
Option Explicit
Sub Sample()
Dim lRow As Long
'~~> Change this to the relevant sheet
With ThisWorkbook.Sheets("Sheet1")
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
.Range("A1:A" & lRow).Formula = "=IF(E1=""a"",""xx"",IF(E1=""b"",""yy"",IF(D1=""c"",""zz"",""ww"")))"
.Range("A1:A" & lRow).Value = .Range("A1:A" & lRow).Value
End With
End Sub
ScreenShot: