Macro to autofill to the last column with data in Excel? - excel

I have a spreadsheet and have a macro that resets the data each month by moving some around and clearing some out which also deletes my autosums in those certain columns. I need to put these sums back in and I believe the autofill feature is the best way to do this. However, I have a lot of different worksheets and the autosum is never on the same row. The below is the macro I have so far. The line with arrows next to it is where it debugs. The macro just above that gets me to the correct cell that I need to autofill from I'm just not sure how to make that autofill macro to not be for a certain row and to be relative. Any help is much appreciated.
Sub Reset_Each_Spare()
Range("E16:F5000").Select
Selection.Copy
Range("P16").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("I16:J5000").Select
Selection.Copy
Range("R16").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("L16:M5000").Select
Selection.Copy
Range("T16").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("D16:D5000").Select
Selection.ClearContents
Range("H16:H5000").Select
Selection.ClearContents
Range("K16:K5000").Select
Selection.ClearContents
Cells.Find(What:="Total", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Offset(0, 1).Select
>>>Selection.AutoFill Destination:=Range("C:U"), Type:=xlFillDefault
Range("A1").Select
End Sub

I answered my own question guys, thanks for anybody who took the time to look at my question. I just replaced the line above marked by >>> with the following line of code.
Selection.AutoFill Destination:=ActiveCell.Range("A1:S1"), Type:=xlFillDefault

Related

Pivotitems(Cell reference)

I recently started using Macros in my workbooks, and I ran into a problem with my record.
I recorded a macro that performs the following actions:
go to my Summary sheet (called "PIVOT.EXM"), expend the first Cell in the second column on my pivot table.
copy the data from there
paste my copied data to my cus.1 sheet
return to my PIVOT.EXM sheet, close the expansion of my first cell and expend the second cell and then repeat all the actions from above.
My problem is that I want to set pivotitems dynamically, in my code its recognized as value and I want it will be as my cell location.
example for my code (2 first cells):
Range("A5").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("cus_number").PivotItems("1") _
.ShowDetail = True
Range("D5:E7").Select
Selection.Copy
Sheets("CUS.1").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("PIVOT.EXM").Select
Range("A5").Select
Application.CutCopyMode = False
ActiveSheet.PivotTables("PivotTable1").PivotFields("cus_number").PivotItems("1") _
.ShowDetail = False
Range("A6").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("cus_number").PivotItems("2") _
.ShowDetail = True
Range("D6:E8").Select
Selection.Copy
Sheets("CUS.2").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("PIVOT.EXM").Select
Range("A6").Select
Application.CutCopyMode = False
ActiveSheet.PivotTables("PivotTable1").PivotFields("cus_number").PivotItems("2") _
.ShowDetail = False
I want that the cell location will be written in pivotitems("") instead of the value 1
Thank you very much!
I tried to write the Cell location inside the brackets with quotation marks and without.

Adding If/End If to macro

I'm trying to add a condition to a copy and paste macro where it copies a row from table1 and pastes it onto table2 if the row in table1 is red.
I've tried:
Sub ColdLake1()
If Range("B55").Interior.ColorIndex = 3 Then
Range("B55:H55").Select
Selection.Copy
Range("C140").Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
End Sub
It returns nothing. Any ideas?
Thanks (I'm quite new to all this).
This worked for me:
Sub ColdLake1()
With ActiveSheet
Debug.Print "ColorIndex", .Range("B55").Interior.ColorIndex
If .Range("B55").Interior.ColorIndex = 3 Then
.Range("B55:H55").Copy
With .Range("C140")
.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End With
End If
End With
End Sub
If your code isn't doing anything it may be because the color isn't what you think it is, or for some reason your code is not entering the If block. Put some breakpoints and see what is going on. https://www.excel-easy.com/vba/examples/debugging.html

How to save and transpose a range in Excel VBA

I have a macro where I open a textfile to copy and transpose several columns into my worksheet. As it is now this actually works. However, as I am right now opening the textfile, copying a column, going back to my original worksheet pasting and transposing the data, then switching back and forward between the two sheets until all relevant columns have been copied I figured it could probably be optimized if I understood VBA a little better.
So my question is if I can save the column data as ranges instead and then copying it all at once and pasting and transposing it all at once as well?
Or will this not have any impact on the speed of my macro?
Also, as I am actually opening many textfiles (open one, close it, open the next, close it, etc) is it possible to overwrite a range when a new textfile has been opened?
I have shown an example of my code below hope you can make sense of it:
'First Selection to be copied
ActiveSheet.Cells(RowTemp + 1, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Go back to original sheet
Windows(Left(f, Len(f))).Activate
'Paste and Transpose Data
If IsEmpty(ActiveSheet.Cells(4, 2).Value) = True Then
ActiveSheet.Cells(4, 1).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
ActiveSheet.Cells(4 + 5 + Range("A2").Value, 1).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End If
'Copy 2nd set of Data
Windows(Left(z, Len(z))).Activate
Range("B1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows(Left(f, Len(f))).Activate
'Paste and Transpose 2. set of data
If IsEmpty(ActiveSheet.Cells(5, 2).Value) = True Then
ActiveSheet.Cells(5, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
ActiveSheet.Cells(5 + 5 + Range("A2").Value, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End If

copy and paste a cell into a filter box vba

I'm extremely new to this and most off my codes have come from recording.
I'm trying to complete a basic search using a filter box and then copy the cells onto a different work sheet ready to start again.
This is the macro I recorded, the problem I keep having is rather than "copy what is in Cell B7 in places the contents of what I record in the macro....="=meal", I need this to be Cell B7 from the concur expenses tab?
Sub MySub()
Range("A13:G36").Select
Selection.ClearContents
Range("B7").Select
Selection.Copy
Sheets("Detail for selection").Select
ActiveSheet.Range("$A$1:$F$55").AutoFilter Field:=6, Criteria1:="=*meal*", _
Operator:=xlAnd
Application.CutCopyMode = False
Selection.Copy
Sheets("Concur Expenses").Select
Range("A12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Detail for selection").Select
ActiveSheet.Range("$A$1:$F$55").AutoFilter Field:=6
Sheets("Concur Expenses").Select
Cells.Select
Cells.EntireColumn.AutoFit
Range("B7").Select
End Sub
Any helps greatly appreciated,
Thanks

Excel VBA Copying and Pasting into Next Blank Row in Another Sheet

I'm trying to record a macro using keystrokes only and am coming across an issue on my final keystroke as it is not doing what I expect it to be. A little background in what I am doing - I am working with growing data and a rolling graph to graph the last 4 rows of data. I am trying to copy data from one sheet and paste it onto the next empty row on my table in another sheet. Because I'm using keystrokes, I expected the macro to record the number of clicks but this is not what it's doing. Instead it pastes the data right over the exact cell that the macro was recorded in and not the empty cell below it. For example, I have data in C19 already. When I run the macro, I expect it to paste the new data into C20 but instead it pastes over C19. I think I need to add/edit my VBA so that it will paste the new data into the next empty row in another sheet. I hope this made sense. Any help will be much appreciated!
Thank you!!
Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
Range("C3").Select
ActiveCell.FormulaR1C1 = "=DATE(YEAR(RC[-1]), MONTH(RC[-1])+1, DAY(RC[-1]))"
Range("C3").Select
Selection.Copy
Range("B3").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("C3").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Range("B85:K146").Select
Selection.Copy
Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
Range("B9").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
ActiveSheet.Next.Select
Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
Range("E71:G71").Select
Application.CutCopyMode = False
Selection.Copy
Range("A1").Select
ActiveSheet.Next.Select
Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
Selection.End(xlToLeft).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Selection.End(xlToLeft).Select
Range("C5").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Range("C19").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
End Sub
As others have commented it's best if you read up on how to code directly rather than record a macro. As a quick fix though this should help:
Replace this:
Range("C19").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
with this:
Cells(Range("C1000000").End(xlUp).Row + 1, 3).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
What the .End(xlUp) does is to go from row 1,000,000 upwards looking for the first cell that contains something. The .Row gets the row it's on and the + 1 means we want the next row. Cells is another way of expressing a Range, look up Range and Cell on-line, it will help you on your way.

Resources