Use macro donw the rows - excel

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

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.

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

How to have my recorded macro move to the next person in my list

I have a list that has names and I have recorded a macro to help me copy, paste, format and move to next names in list. It is working, however something crucial is missing because the macro does not keep the previously recorded data, and it does not move to next names in list.
I ran the macro of different rows and tried recording the macro again but it is not working as expected
Sub Accrual()
'
' Accrual Macro
'
'
ActiveSheet.Range("$A$1:$G$1007").AutoFilter Field:=1, Criteria1:= _
"doe, john"
Range("A2").Select
Selection.Copy
Sheets("Sheet4").Select
Range("A2").Select
ActiveSheet.Paste
Sheets("Sheet3").Select
Range("B2:C2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("C2").Select
ActiveSheet.Paste
Range("E2").Select
Sheets("Sheet3").Select
Range("F2:F6").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("A3").Select
End Sub
I would expect my recorded macro to go down my list of names and copy, paste, and format all the names in the list and keep all the data

VBA to look for information in between two dates that are written manually in a specific cell

I'm looking for a VBA code that will look in the 2nd row for specific dates.
Basically, I'm writing a "Begin" date (ex: 2018-07-01) and an "End" date (ex: 2018-07-31) and I want my code to look for everything in between, including those dates, and copy paste all the information in another excel sheet. What the function will do at the end is it'll look into many sheets for those dates and copy every bit of information that are below those dates, and paste them all into one main sheet.
What I'm attaching below is what I've done so far:
Sub Copie()
Sheets("1").Select
Range("B1:H1").Select
Selection.Copy
Sheets("Per Employe").Select
Range("A4").Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Range("J1:P1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Cells(1, 1).Select
ActiveCell.Offset(1, 0).Select
Selection.Copy
Sheets("Per Employe").Select
Cells(4, 1).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Sheets("1").Select
Range("B2:H2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Sheets("1").Select
Range("J2:P2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Per Employe").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
End Sub
What I'm trying to add is the function to look up the dates and only copy/paste the dates that were asked for in the main excel sheet, which would be "Per Employe". Would anyone have any solution to this? The cells that contain the begin and end date would be D1 & F1.

Need Macro to add comma to the beginning of text string

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

Resources