vba code to create a csv file - how to choose rows with data in column A

anteween

New Member
Joined
Nov 18, 2016
Messages
3
Aliquot NumberAnalyteMeasured ValueAnalysis DateAnalysis TimeAnalystDilution factor
12345NH3 as N2.211/17/201613:00Sarah1
12346NH3 as N3.211/17/201613:00Sarah1
12347NH3 as N4.211/17/201613:00Sarah1
12348NH3 as N5.211/17/201613:00Sarah1
12349NH3 as N6.211/17/201613:00Sarah1
12350NH3 as N7.211/17/201613:00Sarah1
12351NH3 as N8.211/17/201613:00Sarah1
12352NH3 as N9.211/17/201613:00Sarah1
12353NH3 as N10.211/17/201613:00Sarah1
12354NH3 as N11.211/17/201613:00Sarah1
12355NH3 as N12.211/17/201613:00Sarah1
12356NH3 as N13.211/17/201613:00Sarah1
12357NH3 as N14.211/17/201613:00Sarah1
12358NH3 as N15.211/17/201613:00Sarah1
12359NH3 as N16.211/17/201613:00Sarah1
NH3 as N 11/17/201613:00Sarah1
NH3 as N 11/17/201613:00Sarah1
NH3 as N 11/17/201613:00Sarah1
NH3 as N 11/17/201613:00Sarah1
NH3 as N 11/17/201613:00Sarah1
NH3 as N 11/17/201613:00Sarah1
NH3 as N 11/17/201613:00Sarah1
NH3 as N 11/17/201613:00Sarah1
<colgroup><col width="257" style="width: 193pt; mso-width-source: userset; mso-width-alt: 9398;"> <col width="60" style="width: 45pt; mso-width-source: userset; mso-width-alt: 2194;"> <col width="98" style="width: 74pt; mso-width-source: userset; mso-width-alt: 3584;"> <col width="84" style="width: 63pt; mso-width-source: userset; mso-width-alt: 3072;"> <col width="86" style="width: 65pt; mso-width-source: userset; mso-width-alt: 3145;"> <col width="111" style="width: 83pt; mso-width-source: userset; mso-width-alt: 4059;"> <col width="84" style="width: 63pt; mso-width-source: userset; mso-width-alt: 3072;"> <tbody> </tbody>

<tbody>
</tbody>
I have a worksheet above(Upload), which gets data from another worksheet (Batch) in the same workbook.
My vba code creates a csv file (code below) However, when the file is created, it creates a line for the rows that have nothing in column A.
How do I change my code to only select data if there is a 'character' in column A? For example, looking at the above table, I would only want the first 15 rows of data.


Sub WriteCSVTitanUpload()

Dim FilePath As String
Dim CellData As String
Dim LastCol As Long
Dim LastRow As Long

LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
LastRow = ActiveSheet.Range("A1").SpecialCells(xlCellTypeLastCell).Row
'LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row (I was trying different codes here)
CellData = ""

FilePath = "H:\Redirection\Documents\ATL Implementation\NH3Upload.csv"

Open FilePath For Output As #2

For i = 1 To LastRow

For J = 1 To LastCol

If J = LastCol Then
CellData = CellData + Trim(ActiveCell(i, J).Value)
Else
CellData = CellData + Trim(ActiveCell(i, J).Value) + ","
End If


Next J

Write #2, CellData
CellData = ""

Next i

Close #2

MsgBox ("Done")


End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this:

Replace your code
Code:
[COLOR=#333333]LastRow = ActiveSheet.Range("A1").SpecialCells(xlCellTypeLastCell).Row[/COLOR]
with
Code:
LastRow = [COLOR=#333333]ActiveSheet[/COLOR].Cells([COLOR=#333333]ActiveSheet[/COLOR].Rows.Count, "A").End(xlUp).Row

The function you have finds the furthest column right and returns that column letter and the furthest column down and returns that row, even if there is nothing in that actual cell.

The code I added goes to the very bottom of the worksheet column A, then do an "End" "Up", which will find the last cell in A that actually has a value in it.
 
Upvote 0

Forum statistics

Threads
1,215,417
Messages
6,124,777
Members
449,187
Latest member
hermansoa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top