Solved

VBA delete 2 columns before export

Posted on 2014-10-02
8
222 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
ID: 40356497
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
ID: 40356522
Try using Range instead of columns.
Sheets("Import").Range("P:Q").Delete

Open in new window

0
 
LVL 45

Expert Comment

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

Author Comment

by:Mandy_
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;;;
0
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.

 
LVL 24

Expert Comment

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

Author Comment

by:Mandy_
ID: 40356828
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
ID: 40356835
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_
ID: 40357406
i found the error .  one cell has #nv. everything fine now. thank you so much
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

Title # Comments Views Activity
macro for same location path 2 19
Excel 2016 Not Responding Issues 6 28
Excel Save As Status Box will not go away 6 17
Excel printing page management 2 20
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

910 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

24 Experts available now in Live!

Get 1:1 Help Now