Formatting a cell value to "MMMM" using excel vba - vba

I have the below code that I need some assistance modifying.
Sub CopyDataBasedOnTimeRangeMonth()
Dim i, LastRow
Dim Cell As Range
LastRow = Sheets("OPA").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A3:U500").ClearContents
For i = 2 To LastRow
If Sheets("OPA").Cells(i, "G").Value >= Range("U1") And Sheets("OPA").Cells(i, "G").Value < Range("AC1") Then
Sheets("OPA").Cells(i, "R").EntireRow.Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
End Sub
For the calculation of "G" I want to use the format of "MMMM" of the value being calculated. In an excel formula I can use something like Text("G12","MMMM") and then continue with the formula but I don't know how to modify the about code to just to use the Month only value of "G".
Thanks in advance for any help you can provide.

It would be best to have [U1] as a date as well, then formatted as month
If Month(Sheets("OPA").Cells(i, "G")) >= Month(Range("U1"))

you can see below the variant of your code, which has been updated using variant provided by Davesexcel (+1), and also some correction from my side, just to simplify readability:
1) absolute reference to Range() replaced to [] shorthand method;
2) removed Destination:= as excessive, also destination range replaced by row, because when you copy the row then destination shall be the row;
3) applied with (object) method;
4) added type of the variables, e.g. i replaced by i& (means i as long)
Sub CopyDataBasedOnTimeRangeMonth()
Dim i&, LastRow&, Cl As Range
LastRow = Sheets("OPA").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").[A3:U500].ClearContents
With Sheets("OPA")
For i = 2 To LastRow
If Month(.Cells(i, "G")) >= Month(.[U1]) And _
Month(.Cells(i, "G")) < Month(.[AC1]) Then
.Rows(i).Copy Sheets("Sheet2").Rows(Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1)
End If
Next i
End With
End Sub
tested, works fine.
source:
destination:

Related

Copy - paste values while creating growing List

I want to have a macro which runs everytime I open the excel-file, then compares the date (I5) with the last entry in a list (column L), and if the date is older, copy some values (I5 and I11) and paste them in the next empty row of the list (columns L and M). I have written the code bellow but it does not work, I get runtime error 424 and every other syntax I found online and tried to adapt isn't working either. Can anyone help ?
Private Sub Workbook_Open()
If Worksheets("overdue").Range("I5").Value > Worksheets("overdue").Range("L2").End(xlDown).Value Then
Worksheets("overdue").Range("I5").Copy
Worksheets("overdue").Range("L1").End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues
Worksheets("overdue").Range("I11").Copy
Worksheets("overdue").Range("M1").End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues
End If
End Sub
Try the code below, in case you have only 1 row in column "L" (I guess header):
Private Sub Workbook_Open()
Dim LastRow As Long
With Worksheets("overdue")
LastRow = .Range("L1").End(xlDown).Row
If LastRow >= 2 Then
If .Range("I5").Value > .Range("L" & LastRow).Value Then
.Range("L" & LastRow + 1).Value = .Range("I5").Value
.Range("M" & LastRow + 1).Value = .Range("I11").Value
End If
End If
End With
End Sub

Counting the occurrences of names using VBA

Sorry I am new to VBA. The Vlookup calls for Column X & Y work fine. However, for column Z, I am trying to count the number of names in Column B, but an error kept popping up. I wonder why?
Dim lastrow As Long
lastrow = Range("A2").End(xlDown).Row
Range("X2:X" & lastrow).FormulaR1C1 = "=VLOOKUP(RC[-23],'Sheet2'!R1C1:R25000C10,7,0)"
Range("Y2:Y" & lastrow).FormulaR1C1 = "=VLOOKUP(RC[-24],'Sheet2'!R1C1:R25000C10,2,0)"
Range("Z2:Z" & lastrow).FormulaR1C1 = "=LEN(RC[-22]-LEN(SUBSTITUTE(RC[-22], "";"", ""))+1"
Columns("X:Z").EntireColumn.AutoFit
Basically, what I am try to achieve is like the following. Column Z will autofill the occurrences of names in column B
I think you are missing a parenthese in your function string, as well as escape quotes for the empty string, so it should be:
Range("Z2:Z" & lastrow).FormulaR1C1 = "=LEN(RC[-22])-LEN(SUBSTITUTE(RC[-22], "";"", """"))+1"
I realize that this answer doesn't answer your question but at least the following code serve the same purpose. And to fix the problem if there are blanks or merged cells in the range as pointed out by Mr. RGA, I change the way to formulate variable Last_Row
Sub Count_Name()
Dim i As Long, Last_Row As Long
With Sheets("Sheet1")
'It won't be a problem anymore if there are blanks or merged cells in the range using this line
Last_Row = Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To Last_Row
If .Cells(i, "A") = "" Then
.Cells(i, "B") = 0
Else
.Cells(i, "B") = Len(.Cells(i, "A")) - Len(Replace(.Cells(i, "A"), ";", "")) + 1
End If
Next i
End With
End Sub
Here I'm assuming the string names are in column A, the number of names are in column B, and both of the data are in Sheet1. The equivalent Excel formula for the above code is
=LEN(A2) - LEN(SUBSTITUTE(A2, ";", "")) + 1

VBA: Placing a forumula down a column using a vlookup formula

Below I am attempting to place the formula just to the right of the last column, beginning at row 2. I know the For statement works, as well as the searching for last column/ row as i've used this in a previous macro when placing a formula down a column. The only question I have is how do I make the VLookup formula work properly?
End goal:
1) Forumla on column to the right of last one
2) Vlookup looksup the value in the last column on the given row within the For statement on a tab called "Lookup"
3) On this Lookup tab, column A is where the value will be found, but I need to return the second column value.
Please zero in on the forumula beginning with the "=iferror(...". I currently receive the error, "Application Defined or Object-Defined" error.
EThree = Cells(Rows.Count, 4).End(xlUp).Row
NumThree = Evaluate("=COUNTA(9:9)")
For r = 2 To EThree
Cells(r, NumThree + 2).Formula = "=IFERROR(((Vlookup(" & Cells(r, 14).Value & ",Lookup!$A:$B,2,0)""))))"
Next
You can place your formula in one go; no need to loop.
Try this:
With Sheets("NameOfWorksheet") '~~> change to suit
'~~> first get the last row and column
Dim lrow As Long, lcol As Long
lrow = .Range("D" & .Rows.Count).End(xlUp).Row
lcol = .Cells(9, .Columns.Count).End(xlToLeft).Column
Dim rngToFillFormula As Range, mylookup As String
'~~> get the lookup value address
mylookup = .Cells(2, lcol).Address(False, False, xlA1)
'~~> set the range you need to fill your formula
Set rngToFillFormula = .Range(.Cells(2, lcol), Cells(lrow, lcol)).Offset(0, 1)
rngToFillFormula.Formula = "=IFERROR(VLOOKUP(" & mylookup & _
",Lookup!A:B,2,0),"""")"
End With
What we did is explained in the comments. HTH.

Change a cell's format to boldface if the value is over 500

I am using Excel 2010 and trying to add a bunch of rows placing the sum of columns A and B in column C. If the sum is over 500 I would then like to boldface the number in column C. My code below works works mathematically but will not do the bold formatting. Can someone tell me what I am doing wrong? Thank you.
Public Sub addMyRows()
Dim row As Integer 'creates a variable called 'row'
row = 2 'sets row to 2 b/c first row is a title
Do
Cells(row, 3).Formula = "=A" & row & "+B" & row 'the 3 stands for column C.
If ActiveCell.Value > 500 Then Selection.Font.Bold = True
row = row + 1
'loops until it encounters an empty row
Loop Until Len(Cells(row, 1)) = 0
End Sub
Pure VBA approach:
Public Sub AddMyRows()
Dim LRow As Long
Dim Rng As Range, Cell As Range
LRow = Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Range("C2:C" & LRow)
Rng.Formula = "=A2+B2"
For Each Cell In Rng
Cell.Font.Bold = (Cell.Value > 500)
Next Cell
End Sub
Screenshot:
An alternative is conditional formatting.
Hope this helps.
Note: The formula in the block has been edited to reflect #simoco's comment regarding a re-run of the code. This makes the code safer for the times when you need to re-run it. :)

converting excel sumifs formula to vba code

I'm trying to do a SUMIFS calculation in VBA. It works fine when I enter it on the spreadsheet, but when I try to convert it to VBA, it doesn't seem to work.
Sheets("Master").Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row).Formula = _
"=SUMIFS(Input!C32,Input!C37,Master!C1,Input!C31,Master!R1C)"
This is the snippet of code (originally in a comment):
Dim LastRow As Long
Dim rw As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For rw = 2 To LastRow
Sheets("Master").Cells(rw, 2).Value = Application.WorksheetFunction.SumIfs(Sheets("Input").Range("AF:AF"), Sheets("Input").Range("AK:AK"), Sheets("Master").Range("A:A"), Sheets("Input").Range("AE:AE").Sheets("Master").Range("B2"))
Next
You aren't specifying the values you want to lookup in your criteria1. You have to specify a value, not a range.
Your sum range is fine.
Sheets("Input").Range("AF:AF")
Your criteria range1 is fine.
Sheets("Input").Range("AK:AK")
Your criteria1 needs to be a value, not a range.
Use this Sheets("Master").Range("A2").value
instead of Sheets("Master").Range("A:A")
Obviously you can replace the 2 in the criteria1 with a variable if you need to to get your loop to work.