Paste to the end of selected column

Dtmq

New Member
Joined
Jul 8, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hey everyone!
I'm new to VBA and have been attempting to achieve the following:

1. Message prompt let's the user select a cell from the column that is to be copied. Based on that cell, the entire column is copied.
2. The user then selects a cell from the column to which the copied column should be pasted. Here I don't want to overwrite any preexisting data and instead want the copied column to be pasted to the end of this column.

The code looks like this:

VBA Code:
Sub test2()
Set ws1 = Sheets("Test Sheet 1")
Set ws2 = Sheets("Test Sheet 2")

  Dim rng As Range
  On Error Resume Next
  Set rng = Application.InputBox(Prompt:="Select a cell from the column that is to be copied .", Title:="Select Column", Type:=8)
  On Error GoTo 0
  If rng Is Nothing Then Exit Sub
  
  Dim rng2 As Range
  On Error Resume Next
  Set rng2 = Application.InputBox(Prompt:="Select a cell from the column in which the data should be pasted", Title:="Select column", Type:=8)
  Columns(rng.Column).Copy
[COLOR=rgb(251, 160, 38)]  ws2.Range(rng2.Columns & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues[/COLOR]
  Application.CutCopyMode = False

End Sub

I highlighted the line of code that I'm not getting to work as planned (in this current version, nothing is pasted). Any ideas? Help would be very much appreciated.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
There are a number of issues with your code.
  1. You can't copy a "whole" column to any position other that row 1 or it won't fit
    So you need to just select down the rows being used before copying.
  2. Your On error resume next is masking errors
  3. If you are copying between sheets you need to be referencing the sheets in the code.
See if the below helps:
VBA Code:
Sub test2()

  Dim ws1 As Worksheet, ws2 As Worksheet

  Dim rng As Range
  On Error Resume Next
  Set rng = Application.InputBox(Prompt:="Select a cell from the column that is to be copied .", Title:="Select Column", Type:=8)
  Set ws1 = rng.Parent
  On Error GoTo 0
  If rng Is Nothing Then Exit Sub
  
  Dim rng2 As Range
  'On Error Resume Next
  Set rng2 = Application.InputBox(Prompt:="Select a cell from the column in which the data should be pasted", Title:="Select column", Type:=8)
  If rng2 Is Nothing Then Exit Sub
  Set ws2 = rng2.Parent
  'Columns(rng.Column).Copy
  With ws1
    .Range(.Cells(1, rng.Column), .Cells(Rows.Count, rng.Column).End(xlUp)).Copy
  End With
  ws2.Cells(Rows.Count, rng2.Column).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
  
  Application.CutCopyMode = False

End Sub
 
Upvote 0
Solution
Hi, thank you vey much for you reply and help. From what I can see, the code now works as I had intended.

That makes sense. I was incorrectly assuming that only cells with entries would be counted as part of the column. I will have to look into the functionality of "On Error Resume Next", i found that in connection with the Application.InputBox when trying to create the message/selection boxes and didn't think much of it.
 
Upvote 0
VBA does not always have the functionality to test if something exists and you have to use workarounds. One of the workarounds is to let it "error out" and then test for the error.
So you use On Error Resume Next to stop the code from crashing, then test for the error and perform the appropriate action. As soon as you have done that you want to turn error handling back on to a more appropriate setting.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top