I have a workbook with a column that contains date strings - itself being copied from another source as "paste as values". I need to transform that date number into an abbreviated month - say Jan, Feb, etc.
I tried recording it but the macro doesn't understand autofill for formulas, so if the length of the data changes, more or fewer rows, then it doesn't fill out all the spaces or overfills them.
I tried amending the formula to include whole range with End(xlDown) instead of the fixed range it was giving, but then all the rows down to the very bottom are filled.
Here's the code, but I'm open to any other solution.
Sub ConvertDateStringToMonth()
'
' ConvertDateStringToMonth Macro
'
'
Columns("I:I").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("I2").Select
ActiveCell.FormulaR1C1 = "=TEXT(RC[-1], ""mmm"")"
Range("I2").Select
Selection.AutoFill Destination:=Range([I2], [I2].End(xlDown))
Range([I2], [I2].End(xlDown)).Select
Range("H1").Select
Selection.AutoFill Destination:=Range("H1:I1"), Type:=xlFillDefault
Range("H1:I1").Select
Columns("I:I").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("H:H").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("H1").Select
End Sub
Nevermind, with some googling and common sense I used this and it works:
Sub ChangeDate()
Set DateRange = Range([H2], [H2].End(xlDown))
DateRange.NumberFormat = "mmm"
End Sub
Posting here in case anyone else has such a simple question.
Related
I got little project in VBA and stuck on below topic
I need to Sum selected range in first empty cell in B column. I tried a small macro in which it sums the same row which mentioned in the vba.
This is what I've found and try to use
Sub Macro9()
'
' Macro9 Macro
'
'
Range("A3").Select
Selection.Copy
Range("A2").Select
ActiveSheet.Paste
Range("B2").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=+SUM(R[1]C:R[3]C)"
Range("B2").Select
Selection.Copy
Range("B3").Select
Selection.End(xlToRight).Select
Range("S2").Select
Range(Selection, Selection.End(xlToLeft)).Select
ActiveSheet.Paste
Selection.End(xlToLeft).Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Final.xlsx").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False
Range("A6").Select
Windows("copy.xlsm").Activate
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Selection.End(xlToLeft).Select
End Sub
I tried to searched for last non-empty cell in selected range so it won't search the whole column
To get the sum of all the non empty cells in Column "B" and return it to Column "B2" as I can understand with the image, following is the code which help you:
Range("B2").FormulaR1C1 = "=Sum(R3C:R" & ActiveSheet.UsedRange.Rows.Count + 1 & "C)"
You may use following code to get the sum of till first empty cell:
Range("B2").FormulaR1C1 = "=Sum(R3C:R" & Range("B3").End(xlDown).Row & "C)"
use SpecialCells method of Range object to get not empty cells in a given range
then use Areas property of Range object to loop through each group of consecutive not empty cells
Option Explicit
Sub SumThem()
Dim c As Range
For Each c In Intersect(ActiveSheet.UsedRange, Columns(2)).Offset(1).SpecialCells(xlCellTypeConstants).Areas ' loop through each "area" (i.e.: group of consecutive cells with "constant" values) of column B from row 2 downwards
c.Resize(1).Offset(-1, 0).Formula = "=+SUM(" & c.Address & ")" ' place the sum of current "area" in the cell right above it
Next
End Sub
I am setting up a attendance spreadsheet where we count the total Late and sick days using the CountIf function. Employees are listed one per column. I am trying to write a macro that will count the number of late or sick days per employee, enter that value to a cell and copy that value to a summary sheet. Then using a For Loop to iterate through each employee using the same range (number of rows) but always moving over one column.
This is my first time really using VBA so I am stuck as my code gives me an error when trying to iterate up the columns.
Thank you!
Sub Counting()
ActiveCell.Select
ActiveCell.Offset(0, -1).Range("A1").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(R[-10]C[2]:RC[2],""L"")"
Selection.Copy
Sheets("Sheet3").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
Application.CutCopyMode = False
Selection.ClearContents
Dim LastCol As Integer
With ActiveSheet
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
Dim i As Integer
For i = 1 To LastCol
ActiveCell.FormulaR1C1 = "=COUNTIF(R[-10]C[i]:RC[i],""L"")"
ActiveCell.Select
Selection.Copy
Sheets("Sheet3").Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
Application.CutCopyMode = False
Selection.ClearContents
Next i
End Sub
The problem is with this line:
ActiveCell.FormulaR1C1 = "=COUNTIF(R[-10]C[i]:RC[i],""L"")"
It looks like you want the value of i in here, not the letter "i", which you can do like this:
ActiveCell.FormulaR1C1 = "=COUNTIF(R[-10]C[" & i & "]:RC[" & i & "],""L"")"
This will fix your immediate error message, however the macro as whole is very fragile, you should read article PEH referenced and replace all those .Select / .Copy / .PasteSpecial as much as possible.
Alternatively, you can probably do what you need without a macro, and use formulas instead. If you can it would be preferable.
I believe I'm having an issue with either appropriately identifying a fixed cell or order of operations. I've spent an hour an a half researching and can't find the answer. The issue is only with the Concatenation row: I can't get VBA to recognize the insertion of a fixed cell into the text of the formula (I can only get it R the cell). It's for a daily exported excel report from a database that inserts the date into C2. I'm concatenating the file names in column B with the folder location they'll be in at the end of the day, the day's date and the unique file group identifier in each matching cell in column C. I've replaced the text of the folder name with FOLDER for confidentiality purposes. I can concatenate and autofill it manually, but I'd rather just type the formula in once! Any assistance would be helpful.
Thanks! - John
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C2").Select
ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],""yyyy mm dd"")"
Range("C2").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A5").Select
ActiveCell.FormulaR1C1 =
"=CONCATENATE(""FOLDER,("$C$2"), FOLDER"",RC[1])"
Dim lastRow As Long
lastRow = Range("B" & Rows.Count).End(xlUp).Row
Range("A5").Select
Selection.AutoFill Destination:=Range("A5:A" & lastRow), Type:=xlFillDefault
Range("A5:A" & lastRow).Select
Selection.Copy
Range("B5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Rows("1:2").Select
Selection.Delete Shift:=xlUp
Range("B3").Select
I don't know if this will work since the formula is expecting references via the RC notation, but you could try:
ActiveCell.FormulaR1C1 =
"=CONCATENATE(""FOLDER,(" & "$C$2" & "), FOLDER"",RC[1])"
As an alternative to inserting a fixed cell, an option would be to assign that cell's value to a string variable and than insert that.
Dim dateVal as string
dateVal = Range("C2").Value2
Range("A5").FormulaR1C1 =
"=CONCATENATE(""FOLDER,(" & dateVal & "), FOLDER"",RC[1])"
I need a macro to append a comma onto the beginning of a column of text strings. I recorded the action myself, but it limited itself to Column C (often, the text strings I need to do this with appear in a different column), and also limited the application of the range to the specific number of rows in the worksheet I recorded it on (in this case, 114).
Here is the original Record Macro output:
Sub AddCommaToESIID()
'
' AddCommaToESIID Macro
'
'
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C2").Select
ActiveCell.FormulaR1C1 = ","
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C114")
Range("C2:C114").Select
Range("E2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]&RC[-1]"
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E114")
Range("E2:E114").Select
Selection.Copy
Range("D2").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Columns("E:E").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
End Sub
I would like to modify this to achieve the following:
Make the macro apply to whatever Column I have selected, as opposed to Column C
Once I have selected a Column, make the macro apply to however many rows there are in the particular worksheet I'm working on.
Thanks in advance for your help!
This will change all non-formulas in whatever range you select
Sub AddCommaToESIID()
Dim rCell As Range
If TypeName(Selection) = "Range" Then
For Each rCell In Selection.Cells
If Not rCell.HasFormula Then
rCell.Value = "," & rCell.Value
End If
Next rCell
End If
End Sub
I would like to ask for the help with one simple macro. It does the job for my team mates to compare the data and apply countif function and bring the proper data. However, macro has the autofill set up to D108. I would like to change it slightly to autofill it until last populated row. Could anyone help me to amend it to work as it should?
Sub Countif_function()
Sheets("Account Campaign Member").Select
Range("D1").Select
ActiveCell.FormulaR1C1 = "How Many Contacts do we have?"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF('Campaign Member'!C[-2],RC[-1])"
Selection.AutoFill Destination:=Range("D2:D108")
Range("D2:D108").Select
Columns("D:D").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("B:D").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Comparison").Select
Range("A1").Select
ActiveSheet.Paste
Columns("A:B").EntireColumn.AutoFit
Columns("A:B").EntireColumn.AutoFit
Columns("B:B").Select
End Sub
Try this update
Sub Countif_function()
Sheets("Account Campaign Member").Range("D1")= "How Many Contacts do we have?"
with Sheets("Account Campaign Member").Range("D2:D" & .cells(rows.count,3).end(xlup).row)
.FormulaR1C1 = "=COUNTIF('Campaign Member'!C[-2],RC[-1])"
.value=.value
end With
Sheets("Account Campaign Member").Columns("B:D").copy Sheets("Comparison").Range("A1")
Sheets("Comparison").Columns("A:B").EntireColumn.AutoFit
Sheets("Comparison").Columns("B:B").Select
End Sub
use this code
LastRow = Range("A2").End(xlDown).Row
Range("D2").AutoFill Destination:=Range(Range("D2"), Range("D" & LastRow))
instead of
Selection.AutoFill Destination:=Range("D2:D108")