[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 113
  • Last Modified:

Find first blank column and paste copied data

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
Jagwarman
Asked:
Jagwarman
  • 2
  • 2
1 Solution
 
Hakan YılmazTechnical Office MEP EngineerCommented:
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
 
JagwarmanAuthor Commented:
Hi Hakan Yılmaz I am getting an error at PasteToBlankColumns ThisWorkbook.Sheets("Sheet1"), 1

Subscript out of range (Error 9)

Regards
0
 
Hakan YılmazTechnical Office MEP EngineerCommented:
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
 
JagwarmanAuthor Commented:
great code thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now