Solved

VBA delete 2 columns before export

Posted on 2014-10-02
8
232 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 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 34

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

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

717 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