Need Macro to add comma to the beginning of text string - excel

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

Related

Convert a column of date numbers into abbreviated month

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.

I need help truncating characters beyond 40 in the one column in VBA

My code fails to truncate anything beyond 40 characters when i run it. Any suggestions on what line I can use. The code is an xlam. I am trying to truncate anything in column G I tried to put in formula left(F2, 40). Maybe i am using a wrong formula? or there is another way to fix it. Please let me know. Here is the Code I have so far:
Option Explicit
Private Sub ProcessReport()
Dim oWB As Excel.Workbook
Dim oXLAM As Excel.Workbook
Dim oWS As Excel.Worksheet
Set oWB = ActiveWorkbook
Set oWS = ActiveSheet
Set oXLAM = Workbooks("NRPPosPay.xlam")
Call formatcols
End Sub
Sub formatcols()
Dim oWS As Excel.Worksheet
Dim LastPopulatedRow As Long
LastPopulatedRow = Range("G" & Rows.Count).End(xlUp).Row
'Delete Colums
Columns("F:H").Select
Selection.Delete Shift:=xlToLeft
'Move Colums
Columns("E:E").Select
Selection.Cut
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Columns("G:G").Select
Selection.Cut
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Columns("G:G").Select
Selection.Cut
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Columns("E:E").Select
Selection.Cut
Columns("D:D").Select
Selection.Insert Shift:=xlToRight
Columns("G:G").Select
Selection.Cut
Columns("E:E").Select
Selection.Insert Shift:=xlToRight
Columns("G:G").Select
Selection.Cut
Columns("F:F").Select
Selection.Insert Shift:=xlToRight
'Replace in Colums
Range("G:G").Replace What:=",", Replacement:=" "
Range("C:C").Replace What:="-", Replacement:=" "
Range("E:E").Replace What:="OCK", Replacement:="IS"
Range("E:E").Replace What:="VCK", Replacement:="CN"
'Formula in Columns
Range("G2").Formula = "=left(G2, 40)"
Range("G2: " & "G" & LastPopulatedRow).FillDown
'Copy and Paste
Range("G:G").Copy
Range("F:F").PasteSpecial _
Operation:=xlPasteSpecialOperationDivide
'Delete Column
Columns("G:G").Select
Selection.Delete Shift:=xlToLeft
'Replace
Range("F:F").Replace What:=",", Replacement:=" "
'Format columns
Columns("A:A").NumberFormat = "0"
Columns("B:B").NumberFormat = "0"
Columns("C:C").NumberFormat = "#.00"
Columns("D:D").NumberFormat = "mddyyyy"
' Delete Header row
Rows(1).EntireRow.Delete
End Sub
Sub ProcessPos(control As IRibbonControl)
Call ProcessReport
End Sub
Leaving all other changes that you can do to improve the code you should replace this line
Range("G2").Formula = "=left(G2, 40)"
with this one:
Range("G2").Value = Left(Range("G2").Value, 40)
This would make your code do what you desire for cell G2 now you can use loop to do this for all cells.

Select range instead of specific cell value with VBA

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

Transpose rows to columns on irregular and inconsistent data

I have to transpose rows to columns in excel using vba and the data is of around 500000.
The problem is that the data is not regular and is consistent.
Like there will be 4 rows then a blank then it can be three rows or one as well.
I want to transpose the group of data separated by a blank cell to be transposed to the the respective column in-front of the first entry.
Sub Transpose()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Loop
End Sub
I used this code but the problem is that it is skipping the data which is present in the single row.
Then this should do it, beware that I'm assuming where your data is and where is going to get paste, don't forget to change that:
Option Explicit
Sub Transpose()
Dim LastRow As Long 'last row on the sheet
Dim TransposeRow As Long 'row where we transpose
Dim x As Long 'columns
Dim C As Range 'faster looping through cells with For Each C in range
With ThisWorkbook.Sheets("MySheet") 'change this to your sheet
'To assign the last row im gonna assume your data is in column A or 1(B would be 2 and so...)
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Last row with data
TransposeRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1 'on column B will be pasting the data
x = 2 'initialize x being 2 as for B column
For Each C In .Range("A2:A" & LastRow)
If C = vbNullString Then 'in case the cell is blank we jump a row
TransposeRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1 ' recalculate row for transposing data
x = 2 'reinitialize column counter
Else
.Cells(TransposeRow, x) = C 'we copy the value to the row and column empty
x = x + 1 'add 1 column
End If
Next C
End With
End Sub
I have edited your code to show an approach that can work for you. You need to add a condition for one cell data.
Sub Transpose2()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
If IsEmpty(ActiveCell.Offset(1, 0).Value) Then
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Else
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.End(xlDown).Select
End If
Application.CutCopyMode = False
Selection.End(xlDown).Select
Loop
End Sub
Note: Using select is not generally a good idea. An example of cutting down select would be:
Sub Transpose3()
' Transpose Macro
' Keyboard Shortcut: Ctrl+Shift+T
Do Until IsEmpty(ActiveCell.Value)
If IsEmpty(ActiveCell.Offset(1, 0).Value) Then
ActiveCell.Copy ActiveCell.Offset(0, 1)
Else
Range(ActiveCell, ActiveCell.End(xlDown)).Copy
ActiveCell.Offset(0, 1).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(0, -1).Range("A1").End(xlDown).Select
End If
Application.CutCopyMode = False
Selection.End(xlDown).Select
Loop
End Sub

Use macro donw the rows

I'm having a problem with setting up a macro. Let me try to explain:
I've got two sheets, in sheet1 I've got pure data, in sheet2 I've got a macro that calculates how many pieces you can put into a certain container by using dimensions given in certain cells. As I said I've got a pure data in sheet1 which contains a box number, its width, length a container number its going to be packed and that container's width and length. Now what I want is I want the macro to copy the dims from sheet1, put them in the specific cells in sheet2, run the calculating macro, copy the cell with result, paste it in the sheet1 and carry on with the next row until there's no data in the column next to it.
This what I've got so far but I don't know how to loop it for the next rows. I think there might be something with selecting cells that will make this possible rather then specifying the cells by name. Any help will be much appreciated.
Sub AutoCalculating()
Sheets("Sheet1").Select
Range("A2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("B2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("B2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("B3").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("D2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("B4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("E2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("B5").Select
ActiveSheet.Paste
Call CalculateBoxes
Range("B6").Select
Selection.Copy
Sheets("Sheet1").Select
Range("F2").Select
ActiveSheet.Paste
End Sub
`
Found the solution, I did the code by offsets not by named cells and then looped it down until it finds an empty cell in the next row. Here's the is the code if somebody would struggle with such thing like I did. You just need to make sure before playing this macro we need to make active the first cell in the first empty column next to the data
Sub AutoCalc2()
Do
ActiveCell.Offset(0, -1).Select
If IsEmpty(ActiveCell) Then
Exit Do
Else
ActiveCell.Offset(0, 1).Select
ActiveCell.Offset(0, -5).Copy
Sheets("Sheet2").Select
Range("B2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy 1st cell which is +5 cells to the left next to the acive
ActiveCell.Offset(0, -4).Copy
Sheets("Sheet2").Select
Range("B3").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy 2nd cell which is +4 Cells to the left next to the acive
ActiveCell.Offset(0, -2).Copy
Sheets("Sheet2").Select
Range("B4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy 3rd cell which is +2 cells to the left next to the acive
ActiveCell.Offset(0, -1).Copy
Sheets("Sheet2").Select
Range("B5").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
'Copy 4th cell which is +1 cells to the left next to the acive
Call CalculateBoxes
'Call your macro
Sheets("Sheet2").Select
Range("B6").Select
ActiveCell.Copy
Sheets("Sheet1").Select
ActiveCell.PasteSpecial
ActiveCell.Offset(1, 0).Select 'selects next cell to be active so it won't make it lasts forever block on first cell
End If
Loop
End Sub

Resources