Solved

VBA delete 2 columns before export

Posted on 2014-10-02
8
216 Views
Last Modified: 2014-10-02
hi,

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
    
    Worksheets("IMPORT").Activate
    lastColumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
    lastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
    

ActiveSheet.Columns("P:Q").Delete
   


    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.
            Else
                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

0
Comment
Question by:Mandy_
8 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 400 total points
Comment Utility
It works on my spreadsheet.

Are those columns Protected?

Alternatively, try:

Worksheets("IMPORT").Columns("P:Q").Delete

Open in new window

0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 100 total points
Comment Utility
Try using Range instead of columns.
Sheets("Import").Range("P:Q").Delete

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
what is the error number/message you are seeing?
0
 
LVL 2

Author Comment

by:Mandy_
Comment Utility
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;;;
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Can you post your spreadsheet please.
0
 
LVL 2

Author Comment

by:Mandy_
Comment Utility
another problem occurs. Runtime error 13  "Typ incompatible" and the string is marked see pic

vba-export.png
0
 
LVL 24

Expert Comment

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

Author Closing Comment

by:Mandy_
Comment Utility
i found the error .  one cell has #nv. everything fine now. thank you so much
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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 will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

762 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

10 Experts available now in Live!

Get 1:1 Help Now