?
Solved

VBA delete 2 columns before export

Posted on 2014-10-02
8
Medium Priority
?
234 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_
[X]
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
8 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 1600 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 34

Assisted Solution

by:Norie
Norie earned 400 total points
ID: 40356522
Try using Range instead of columns.
Sheets("Import").Range("P:Q").Delete

Open in new window

0
 
LVL 46

Expert Comment

by:aikimark
ID: 40356586
what is the error number/message you are seeing?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

801 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