Solved

Find first blank column and paste copied data

Posted on 2015-01-26
4
100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

896 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

15 Experts available now in Live!

Get 1:1 Help Now