MS VB Copy from 1 worksheet to another with empty cells - excel

I have a MS Visual Basic macro for my excel sheet, I have a child workbook and a parent workbook. I want to copy the cells from the child worksheet "account" into the parent worksheet "account". the cells in the child sheet have some blank cells, currently with this code, it stops at the blank cell, I want it to miss the blank cell and go to the next cell with values and then keep copying.
Sub Button1_Click()
'Field Name
Windows("childsheet.xlsm").Activate
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("A3").Select
ActiveSheet.Paste
'API Name
Windows("childsheet.xlsm").Activate
Range("B3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("B3").Select
ActiveSheet.Paste
'Type
Windows("childsheet.xlsm").Activate
Range("C3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("C3").Select
ActiveSheet.Paste
'Length
Windows("childsheet.xlsm").Activate
Range("D3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("D3").Select
ActiveSheet.Paste
'Required
Windows("childsheet.xlsm").Activate
Range("E3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("E3").Select
ActiveSheet.Paste
'Read Only?
Windows("childsheet.xlsm").Activate
Range("F3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("F3").Select
ActiveSheet.Paste
End Sub
it works. it copies each column that i specify but when it gets to a column that has empty cells it copy any info. in that cell from top to bottom but if it encounters a blank it stops there and then moves onto the next column. I want it to copy all info.

Instead of
Windows("childsheet.xlsm").Activate
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("A3").Select
ActiveSheet.Paste
Use something like
With Workbooks("childsheet.xlsm").ActiveSheet
.Range("A3", .Cells(.Rows.Count, "A").End(xlUp)).Copy Destination:=Workbooks("parentsheet.xlsm").ActiveSheet.Range("A3")
.Range("B3", .Cells(.Rows.Count, "B").End(xlUp)).Copy Destination:=Workbooks("parentsheet.xlsm").ActiveSheet.Range("B3")
'… and so on …
End With
A further improvement is to replace .ActiveSheet with the sheet name like .Worksheets("YourSheetName") so your code is more reliable.

Related

Macro Button for Filter

I recorded a macro to create a button that would filter my contracts to find only the ones with a - in them and copy and paste them into another sheet and got this:
Sub Contracts_Hyphen()
'
' Contracts_Hyphen Macro
'
'
Sheets("Transactions").Select
ActiveSheet.Range("$A$1:$AA$31579").AutoFilter Field:=5, Criteria1:=Array( _
"17030-89", "39975-41468-43641-45775-48215-49324", "40011-41747-46077", _
"43642-45773", "43773-46237", "46078-46771", "46238-46409"), Operator:= _
xlFilterValues
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Contracts").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.AutoFilter
Range("A1").Select
End Sub
The problem with this code is that if I added another contract with the number for example "18936-87645" this macro would not find it. So what I changed it to was:
Sub Contracts_Hyphen()
'
' Contracts_Hyphen Macro
'
'
Sheets("Transactions").Select
ActiveSheet.Range("$A$1:$AA$31579").AutoFilter Field:=5, Criteria1:="-"
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Contracts").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.AutoFilter
Range("A1").Select
End Sub
But now with that change it is not finding anything. So how can I setup my macro to include numbers with hyphens even potentially newly added numbers?

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

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

VBA script to copy Data from specfic cell range and paste to a specific cell in a new sheet

I have the following script and am trying to work out how to paste the copied data to cell AA1 in sheet TEST, But before it pastes the selected data i need to clear all the data that is in columns AA:AK in sheet TEST. The script copies the data ok but i can't get Selection.PasteSpecial to work with "offset", and i cant work out how to clear the contents of AA:AK in advance.
Any help would be much appreciated.
Sub CopyDATA()
' Set Auto Filter
Selection.AutoFilter
Sheets("SBC_Month").Select
Range("$AA$1:$AK$5000").AutoFilter Field:=9, Criteria1:="Rep Name" ' Filters by Rep Name
' copy filtered data
Range("AA1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
' Paste to sheet Test
Sheets("TEST").Select
Selection.PasteSpecial , Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
You should be able to use
Sub CopyDATA()
Sheets("TEST").Range("AA:AK").ClearContents
Range("B1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
' Paste to sheet Test
Sheets("TEST").Range("AA1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub

Error copying data to new Excel sheet using a Macro

This is my first post here.
I am trying to copy data from one sheet to a new sheet in the same workbook after filtering in the main sheet.
I also have to copy the sheet template from a template sheet onto this new sheet where I am copying the data to before I copy the data.
This is the VBA code shown in my macro:
Sub Macro7()
'
' Macro7 Macro
'
'
Sheets("Template").Select
Rows("1:3").Select
Application.CutCopyMode = False
Selection.Copy
Dim sSheetName As String
Sheets.Add After:=Sheets(Sheets.Count)
sSheetName = ActiveSheet.Name
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("N13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("D4").Select
ActiveSheet.Paste
Columns("D:D").EntireColumn.AutoFit
Sheets("Sheet1").Select
Range("A13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("C4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("D13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("E4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("B13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("F4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("H13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("G4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("F13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("I4").Select
ActiveSheet.Paste
Range("A4").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Fives Cinetic Corp"
Columns("B:B").Select
Columns("A:A").ColumnWidth = 17.57
Range("A4").Select
Selection.AutoFill Destination:=Range("A4:A5")
Range("A4:A5").Select
Range("D10").Select
End Sub
I get an error: Run-time Error 9: Subscript out of range
Guess its something to do with sheet numbers but not able to figure it out exactly what it is.
Just incase you haven't solved this already, take a look at the code I've adapted for you. When making new VBA projects, play around with some of these methods, you'll find they're a lot faster and more reliable than your previous version. Compare them side by side.
This took a lot of assumption on my part so make a backup before running this to test it and make sure everything is going to the right place.
Sub Macro7()
Sheets.Add After:=Sheets(Sheets.Count)
NewSheet = ActiveSheet.Name
Sheets("Template").Rows("1:3").Copy Destination:=ActiveSheet.Range("A1")
Sheets("Sheet1").Activate
Range("N13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("D4")
With Sheets(NewSheet)
Columns("D:D").EntireColumn.AutoFit
End With
Range("A13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("C4")
Range("D13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("E4")
Range("B13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("F4")
Range("H13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("G4")
Range("F13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("I4")
Sheets(NewSheet).Activate
Range("A4").Value = "Fives Cinetic Corp"
Columns("A:A").AutoFit
Range("A4").AutoFill Destination:=Range("A4:A5")
Range("D10").Select
End Sub

Resources