Solved

Find first blank column and paste copied data

Posted on 2015-01-26
4
97 Views
Last Modified: 2015-01-27
Can an expert provide me with VBA that will find the first blank column and then paste the data I have copied

i.e. My Macro will copy data from an existing file [3columns] it will then open another file on a Sheet called Archive Daily

in this sheet I need to find the first blank column and paste the data I have copied.

Many thanks
0
Comment
Question by:Jagwarman
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
Hakan Yılmaz earned 500 total points
ID: 40570703
please try this.
it can look for a fixed number of adjacent blank columns, and paste your data to first cell of found range.
Sub StartCode()
    PasteToBlankColumns ThisWorkbook.Sheets("Sheet1"), 1
End Sub

Sub PasteToBlankColumns(ByRef refsheet As Worksheet, ByVal colcnt As Long)
    Dim ic As Long
    Dim checkrange As Range
    refsheet.Activate
    ic = 0
    Do
        ic = ic + 1
        Set checkrange = refsheet.Range(refsheet.Columns(ic), refsheet.Columns(ic + colcnt - 1))
        If Application.WorksheetFunction.CountA(checkrange) = 0 Then
            refsheet.Cells(1, ic).Select
            refsheet.paste
            Exit Do
        End If
    Loop
End Sub

Open in new window

0
 

Author Comment

by:Jagwarman
ID: 40572128
Hi Hakan Yılmaz I am getting an error at PasteToBlankColumns ThisWorkbook.Sheets("Sheet1"), 1

Subscript out of range (Error 9)

Regards
0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40572164
Do you have a sheet named "Sheet1"?
You can change this line with code below.
This will search blank columns in active sheet of workbook which has this code.
PasteToBlankColumns ThisWorkbook.ActiveSheet, 1

Open in new window

Or this code.
This will search blank columns in application level active sheet.
PasteToBlankColumns Application.ActiveSheet, 1

Open in new window

0
 

Author Closing Comment

by:Jagwarman
ID: 40572277
great code thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now