Excel VBA Insert copied cells and shift cells down ERROR - excel

Complete amateur here. I've been puzzling over this for hours, and I can't find anything to help me in any other thread. At my wit's end so sorry if this has been asked elsewhere.
I'm trying to create a ridiculously simple macro to do the following:
Go to Sheet2,
Select C6:C10
Copy
Go to Sheet3
Insert copied cells in B2 and shift the other cells down.
I did this just by recording the macro, but each time I do it, I get different errors. The error I currently have is 'Insert Method of Range Class Failed', but sometimes the error pops up at 'Selection.Copy'. This is the code I have:
Sub InsertCellsShitDown()
'
' InsertCellsShitDown Macro
'
'
Sheets("Booking Sheet").Select
Range("C6:C10").Select
Selection.Copy
Sheets("Sheet1").Select
Range("B2").Select
Selection.Insert Shift:=xlDown
End Sub
Any help would be hugely appreciated.

Sub InsertCellsShiftDown()
'
' InsertCellsShitDown Macro
Dim bookingWS As Worksheet, mainWS As Worksheet
Dim copyRng As Range
Set bookingWS = Sheets("Booking Sheet")
Set mainWS = Sheets("Sheet1")
Set copyRng = bookingWS.Range("C6:C10")
mainWS.Range("B2:B" & copyRng.Rows.Count + 1).Insert Shift:=xlDown
copyRng.Copy mainWS.Range("B2")
End Sub
How does this work? I assume you wanted to insert 5 rows, so from B2:B7, then put the data.

Related

Creating a new column and pasting data into that new column with VBA

I am attempting to create a new column on a different sheet and then copy data into that column.
Below is the code I have written. The first sub is a new column to the left and the second sub is the column to the right.
The insert column part is working. I hid a column and have a cell in there as a named range which I used to select in my macro. The data I want to copy is on the Input sheet and is named InputData.
Dim sourceSheet As Worksheet
Set sourceSheet = ActiveSheet
Sheets("Data").Activate
Sheets("Data").Range("DividerColumn").Select
Selection.EntireColumn.Offset(0, 0).Insert Shift:=xlToLeft
'Sheets("Input").Activate
'Range("InputData").Copy
'Sheets("Data").Activate
'ActiveCell offset maybe?
'Range().PasteSpecial xlPasteValues
Call sourceSheet.Activate
End Sub
Dim sourceSheet As Worksheet
Set sourceSheet = ActiveSheet
Sheets("Data").Activate
Sheets("Data").Range("DividerColumn").Select
Selection.EntireColumn.Offset(0, 1).Insert Shift:=xlToRight
Call sourceSheet.Activate
End Sub
Oh I didn't see your copy range. In that case this could probably work. I see you just got the answer, but this would be a good way to avoid select.
Sub copyToLeft()
Call doTheCopy(False)
End Sub
Sub CopyToRight()
Call doTheCopy(True)
End Sub
Private Sub doTheCopy(goRightIsTrue As Boolean)
With Sheets("Data").Range("DividerColumn").EntireColumn.Offset(0, IIf(goRightIsTrue, 1, 0))
.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
.Offset(0, -1).Value = Sheets("Input").Range("InputData").EntireColumn.Value
End With
End Sub
I found the solution by using an offset function. Below is my code. Hope this helps someone with a similar situation.
Dim sourceSheet As Worksheet
Set sourceSheet = ActiveSheet
Sheets("Data").Activate
Sheets("Data").Range("DividerColumn").Select
Selection.EntireColumn.Offset(0, 0).Insert
Shift:=xlToLeft
Sheets("Input").Activate
Range("InputData").Copy
Sheets("Data").Activate
Range("DividerColumn").Select
ActiveCell.Offset(0, -1).PasteSpecial
xlPasteValues
Call sourceSheet.Activate
End Sub
-1 in the offset function moves your active cell to the left one cell and then 1 moves it to the right. So once the column is created, either right or left, my macro goes and copies the information and then goes back to the sheet I want it to and selects my named range again and then it gets moved to the left and pastes my data.

Populate specific cells to the last row in another Sheet in the same workbook

it paste the value but not to the last rows in the sheet (List) the END(xlDown).offset(1) stop the
macros and make it crash, I get error 1004 object undefined.
Can someone help me better up this code>
Private Sub CommandButton2_Click()
Dim ws As Worksheet
Set ws = ActiveSheet
Worksheets("FORM").Range("B7").Copy
Worksheets("List").Range("B7").End(xlDown).Offset(1).PasteSpecial xlPasteValues
Worksheets("FORM").Range("D7").Copy
Worksheets("List").Range("C7").PasteSpecial xlPasteValues
Worksheets("FORM").Range("G7").Copy
Worksheets("List").Range("D7").PasteSpecial xlPasteValues
'second line in the same row'
Worksheets("FORM").Range("A8").Copy
Worksheets("List").Range("F7").PasteSpecial xlPasteValues
Worksheets("FORM").Range("E8").Copy
Worksheets("List").Range("G7").PasteSpecial xlPasteValues
Worksheets("FORM").Range("H8").Copy
Worksheets("List").Range("H7").PasteSpecial xlPasteValues
Worksheets("FORM").Range("K8").Copy
Worksheets("List").Range("I7").PasteSpecial xlPasteValues
End Sub
This is because you're using Worksheets("FORM") when you should be using Sheets("FORM")
Also, when you're using .Offset() you need to specify the row AND column numbers you want to ofset by - so .Offset(1,0) in your case I imagine.
If you make those changes, your code should work.

VBA Copy and Pasting 2 Rows Down

I've only just come across VBA so I'm a complete novice. Essentially I'm currently automating a form that requires writing out the questions, potential answers, instructions, etc. for developers on a spreadsheet. I've created a basic template table so all the questions are structured the same. I want to copy and paste this table (clearing the contents and taking off the number of the question) and paste it 2 rows down from the bottom of the last table.
The code works fine if I just wanted to copy and paste the table directly below the first but I can't go any further than that. I'm not sure how to write that I want it to find the last filled in row and paste the table 2 rows below.
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+g
'
Range("C2:G6").Select
Selection.ClearContents
Range("A2").Select
ActiveCell.FormulaR1C1 = "C"
Range("A2:G6").Select
Selection.Copy
Range("A8").Select
ActiveSheet.Paste
End Sub
You can completely avoid using select in order to achieve your goal. In the following code, Source is the range of your table, LastRow finds the last row of your table and DestRng is the destination where you want a copy of your table. Hope this helps!
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+g
'
Dim TblRng As Range
Set TblRng = Range("C2:G6")
TblRng.ClearContents
Dim Source As Range
Set Source = Worksheets("Sheet2").Range(("A2"), Range("G2").End(xlDown))
Source.Copy
Dim LastRow As Long
LastRow = Cells(Rows.Count, "G").End(xlUp).Row
Dim DestRng As Range
Set DestRng = Source(LastRow + 1, "A")
DestRng.PasteSpecial xlPasteAll
End Sub

I want to copy a range of cells and paste them to another sheet dependent on a drop down selection and activated using a button

I have an Export to sheet button but I can't get it working correctly.
I selects the correct cells to copy but can't then transpose them on to selected sheet that appears in the drop down box in cell A1, I then also need it to paste on the next available row in that specific sheet. The problem is that I can't just list the sheets in VBA as the list in the drop down box changes. I have tried several ways to with no success. If someone could help it would be great
Sub Button2_Click()
Worksheets("Sheet1").Range("a2:x2").Copy
ActiveSheet.Paste Destination:=Worksheets("Sheet1!A1").Range("a:x")
End Sub
Here is some more code I have tried for the issue but still does not seem to work.
Sub ExportButton1()
'
' ExportButton1 Macro
' Exports Data to staff sheet from drop down box
'
' Keyboard Shortcut: Ctrl+e
'
ActiveWorkbook.Save
End Sub
Private Sub CommandButton1_Click(ByVal Target As Range)
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Data")
'On Error Resume Next
'If Not (Application.Intersect(Range("H2"), Target) Is Nothing) Then _
Set pasteSheet = Worksheets(ActiveSheet.Range("H2"))
copySheet.Range("G5:AA5").Copy
pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
you need to change the sheet name in your worksheet function.
This might work.
Sub Button2_Click()
'Copying the Data
Worksheets("Sheet1").Range("a2:x2").Copy
'Pasting the data
' What we were missing was to pass the name of the tab dynamically. Now this code will pick up the name that appears in the Cell A1.
ActiveSheet.Paste Destination:=Worksheets(Worksheets("Sheet1").Range("A1").value).Range("A1")
End Sub
Also in the paste range you only need to put the first cell range to paste values.
To paste the Transposed Values check the function PasteSpecial with Transpose property set to True.
'I have found another way around the problem to copy paste cells to certain sheet then on 'staff sheet a formula in a table to tests column A value on data sheet for name and only 'transpose those rows
Sub Macro1()
Range("A2:J2").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
End Sub
'Formula on staff sheet is -=IF(Sheet3!A:A="Column1",Sheet3!$B:$B,"")

Select range cells with blank rows

I've been having difficulties with figuring out how to code this select range macro to include blank rows. The worksheet is a chart with variable number of columns and rows. This is what I have so far:
Sub Macro1()
Range("A5").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
End Sub
The selection step in the macro will only go so far as that blank row and won't go any further (obviously because it's blank hehe). For that reason, I tried adapting this discontiguous type of code but with no luck:
Sub SelectRangeDown_Discontiguous()
Range("A5", Range("A65536").End(xlUp)).Select
End Sub
I was hoping someone could help me figure out the best way of writing this code? Am I on the right path?
If you are using .End(xlToRight) or .End(xlDown) you are going to stop at blank cells. If you are using Range("A65536").End(xlUp) then you are only selecting a single column but you are getting everything from A5 down to the last populated cell and bypassing interim blank cells. Extend this latter method laterally.
Sub Macro1()
with Range("A5")
.resize(cells(rows.count, "A").end(xlup).row - (.row - 1), _
cells(5, columns.count).end(xltoleft).column - (.column - 1)).Copy
end with
End Sub
This would be better with a .Parent Worksheet Object.
You do not need to .Select method something in order to .Copy it¹.
¹ See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.
Consider:
Sub whatever()
Dim r1 As Range, r2 As Range
ActiveSheet.UsedRange
Set r1 = Range(Cells(5, 1), Cells(Rows.Count, Columns.Count))
Set r2 = Intersect(r1, ActiveSheet.UsedRange)
End Sub

Resources