Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

VBA delete 2 columns before export

Posted on 2014-10-02
Last Modified: 2014-10-02

before i export i like to delete column p and q in sheet import
this gives me an error  ActiveSheet.Columns("P:Q").Delete
Any idea!?

Sub csv_Export()
    Dim lastColumn As Integer
    Dim lastRow As Integer
    Dim strString As String
    Dim i As Integer, j As Integer
    Dim UD As String
    Dim Bereich As Range
    Dim filename As Variant
   'user will be prompted to choose file name and location while the extension is fixed
    filename = Application.GetSaveAsFilename(FileFilter:="CSV files (*.csv), *.csv")
    UD = filename
    lastColumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
    lastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row


    Open UD For Output As #1

    For i = 1 To lastRow
        Cells(i, 1).Select
        strString = ""
        For j = 1 To lastColumn
            If j <> lastColumn Then
                strString = strString & Cells(i, j).Value & ";" ' Use semicolon instead of pipe.
                strString = strString & Cells(i, j).Value
            End If
        Next j
        If Len(Trim$(Replace(strString, ";", ""))) > 0 Then
        Print #1, strString
        End If
     Next i

    Close #1
End Sub

Open in new window

Question by:Mandy_
LVL 24

Accepted Solution

Phillip Burton earned 400 total points
ID: 40356497
It works on my spreadsheet.

Are those columns Protected?

Alternatively, try:


Open in new window

LVL 33

Assisted Solution

Norie earned 100 total points
ID: 40356522
Try using Range instead of columns.

Open in new window

LVL 45

Expert Comment

ID: 40356586
what is the error number/message you are seeing?
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


Author Comment

ID: 40356694
Dear phillip,

you solved it but only one thing the output.csv has right now a blank row between like this
how can I prevent it?

NONBM;22.09.2014 11:45:00;New;;;

NONBM;22.09.2014 11:45:01;New;;;

NONBM;22.09.2014 11:45:01;New;;;
LVL 24

Expert Comment

by:Phillip Burton
ID: 40356697
Can you post your spreadsheet please.

Author Comment

ID: 40356828
another problem occurs. Runtime error 13  "Typ incompatible" and the string is marked see pic

LVL 24

Expert Comment

by:Phillip Burton
ID: 40356835
And what is the value of Cells(i,j).Value for this specific instance? (Hover over it to find out.)

Author Closing Comment

ID: 40357406
i found the error .  one cell has #nv. everything fine now. thank you so much

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

808 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