Results 1 to 31 of 31

Compare column number of a cell with the last cell column number in a loop

Threaded View

Pavan Renjal Compare column number of a... 01-11-2014, 12:51 PM
Marc L Re: Compare column number of... 01-11-2014, 01:17 PM
Pavan Renjal Re: Compare column number of... 01-11-2014, 01:24 PM
Marc L Re: Compare column number of... 01-11-2014, 01:52 PM
Pavan Renjal Re: Compare column number of... 01-11-2014, 02:07 PM
Marc L Re: Compare column number of... 01-11-2014, 02:43 PM
Pavan Renjal Re: Compare column number of... 01-11-2014, 02:59 PM
Marc L Try this ! 01-11-2014, 03:11 PM
Pavan Renjal Re: Try this ! 01-11-2014, 03:44 PM
Marc L Thanks ! 01-11-2014, 05:37 PM
Pavan Renjal Re: Thanks ! 01-12-2014, 02:16 AM
Pavan Renjal Re: Thanks ! 02-27-2014, 02:51 AM
Pavan Renjal Re: Compare column number of... 02-27-2014, 02:53 AM
Pavan Renjal Re: Compare column number of... 02-27-2014, 05:28 AM
Marc L Re: Compare column number of... 02-27-2014, 06:45 AM
Pavan Renjal Re: Compare column number of... 02-27-2014, 07:40 AM
Marc L Re: Compare column number of... 02-27-2014, 09:18 AM
Pavan Renjal Re: Compare column number of... 02-27-2014, 09:23 AM
Pavan Renjal Re: Compare column number of... 02-27-2014, 09:31 AM
Marc L Re: Compare column number of... 02-27-2014, 09:54 AM
Pavan Renjal Re: Compare column number of... 02-27-2014, 10:34 AM
Pavan Renjal Re: Compare column number of... 02-27-2014, 10:41 AM
Pavan Renjal Re: Compare column number of... 02-27-2014, 10:44 AM
Marc L Re: Compare column number of... 02-27-2014, 10:40 AM
Marc L Re: Compare column number of... 02-27-2014, 10:47 AM
Marc L Try this ‼ 02-27-2014, 05:43 PM
Pavan Renjal Re: Try this ‼ 02-28-2014, 01:30 AM
Marc L Re: Try this ‼ 02-28-2014, 05:15 AM
Pavan Renjal Re: Try this ‼ 02-28-2014, 02:55 PM
Marc L Re: Compare column number of... 02-28-2014, 04:29 PM
Pavan Renjal Re: Compare column number of... 03-28-2014, 06:56 AM
  1. #1
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Exclamation Compare column number of a cell with the last cell column number in a loop

    I am trying to save an excel sheet to .csv format with the following macro:

    Public Sub WriteCSV()
    Set wkb = ActiveSheet
    Dim fileName As String
    Dim MaxCols As Integer
    fileName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
     
    If fileName = "False" Then
    End
    End If
     
    On Error GoTo eh
    Const adTypeText = 2
    Const adSaveCreateOverWrite = 2
     
    Dim BinaryStream
    Set BinaryStream = CreateObject("ADODB.Stream")
    BinaryStream.Charset = "UTF-8"
    BinaryStream.Type = adTypeText
    BinaryStream.Open
     
    For r = 1 To Range("A" & Rows.Count).End(xlUp).Row
    s = ""
    c = 1
    i = 1
    
    For j = 1 To wkb.Cells(1, wkb.Columns.Count).End(xlToLeft).Column
    i = i + 1
    If Not IsEmpty(wkb.Cells(r, i).Value) Then
    s = s & wkb.Cells(r, c).Value & ","
    c = c + 1
    ElseIf IsEmpty(wkb.Cells(r, i).Value) Then
    If wkb.Cells(r, i).Column <> wkb.Cells(1, wkb.Columns.Count).End(xlToLeft).Column Then
    s = s & ","
    c = c + 1
    Else
    s = s & wkb.Cells(r, c).Value
    c = c + 1
    End If
    End If
    Next j
    
    BinaryStream.WriteText s, 1
    Next r
     
    BinaryStream.SaveToFile fileName, adSaveCreateOverWrite
    BinaryStream.Close
     
    MsgBox "CSV generated successfully"
     
    eh:
     
    End Sub
    The following part of the code needs to be modified so that the commas appear correctly in .csv file even for null values for any given column.

    For j = 1 To wkb.Cells(1, wkb.Columns.Count).End(xlToLeft).Column
    i = i + 1
    If Not IsEmpty(wkb.Cells(r, i).Value) Then
    s = s & wkb.Cells(r, c).Value & ","
    c = c + 1
    ElseIf IsEmpty(wkb.Cells(r, i).Value) Then
    If wkb.Cells(r, i).Column <> wkb.Cells(1, wkb.Columns.Count).End(xlToLeft).Column Then
    s = s & ","
    c = c + 1
    Else
    s = s & wkb.Cells(r, c).Value
    c = c + 1
    End If
    End If
    Next j
    I have attached the Sample Sheets. Source sheet and the Result sheet. The Result sheet doesn't provide the required result. Some data are missing and unwanted commas have been added. Please help me to correct the logic in the above piece of code. Thanks!
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 8
    Last Post: 11-18-2013, 04:24 PM
  2. [SOLVED] How to use the value of a cell as a column number of another cell in a for next loop?
    By George59 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-29-2012, 10:31 AM
  3. Replies: 1
    Last Post: 11-05-2010, 12:44 PM
  4. Replies: 10
    Last Post: 07-12-2010, 08:55 AM
  5. Replies: 7
    Last Post: 12-18-2008, 07:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1