So I have one excel document that has partially filled character array ("|" shows excel column line):
Current Result ("_" is a space):
1111GGH80100022190
1112QQH80100023201
1113GGH80100045201
1114AAH80100025190
So my current code outputs this above result. The problem is that characters 1-5 and 21-24 get skipped over. In general if there is no column number accounted for I should print " " (Space).
Desired Result ("_" is a space):
_____1111GGH80100022____190
_____1112QQH80100023____201
_____1113GGH80100045____201
_____1114AAH80100025____190
Is there are column way to detect if I am missing a range in the header column? I currently use this and only select the data:
Private Sub WriteFile_Click()
Dim myFile As String, rng As Range, cellValue As Variant, I As Integer, j As Integer
myFile = "C:\Reformatted.txt"
Set rng = Selection
Open myFile For Output As #1
For I = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = cellValue + CStr(rng.Cells(I, j).Value)
cellValue = Replace(cellValue, "NULL", " ")
If j = rng.Columns.Count Then
Print #1, cellValue
End If
Next j
cellValue = ""
Next I
Close #1
Shell "C:\Windows\Notepad.exe C:\Reformatted.txt", 1
End Sub
-----------------------------After TMh885 Answer--------------------------------
So the code works perfectly EXCEPT if you have a header thats a single number '63'
So I am trying this:
Private Sub CommandButton1_Click()
Dim myFile As String, rng As Range, cellValue As Variant, I As Integer, j As Integer
myFile = "C:\Reformatted.txt"
Set rng = Selection
Open myFile For Output As #1
Dim strArr(1 To 63) As String, intBeg As Integer, intEnd As Integer, intCount As Integer
For I = 2 To rng.Rows.Count
For j = 1 To rng.Columns.Count
If Len(Cells) = 1 Then
cellValue = cellValue + " "
Else
intBeg = Val(Left(Cells(1, j).Value, InStr(1, Cells(1, j).Value, "-") - 1))
intEnd = Val(Right(Cells(1, j).Value, Len(Cells(1, j).Value) - InStr(1, Cells(1, j).Value, "-")))
intCount = 1
For t = intBeg To intEnd
strArr(t) = Mid(Cells(I, j).Value, intCount, 1)
intCount = intCount + 1
Next t
End If
Next j
For t = 1 To UBound(strArr)
If strArr(t) = "" Then strArr(t) = " "
cellValue = cellValue + strArr(t)
Next t
Erase strArr
Print #1, cellValue
cellValue = ""
Next I
Close #1
Shell "C:\Windows\Notepad.exe C:\Reformatted.txt", 1
End Sub
--------------------------------Error Snapshot----------------------------------
Input:
Here's your code with the extra functionality built in. It essentially just builds an array, adds each character to the correct location then adds it all together, replacing blanks with spaces. You'll have to change the maximum (in this example it's hard-coded at 27) but you could use the same logic as I used to get "intEnd" to find your maximum by looping over the heading column. Notes, this will compensate for out of order columns and I assumed that the Selection includes headers (hence starting at I = 2):
Private Sub WriteFile_Click()
Dim myFile As String, rng As Range, cellValue As Variant, I As Integer, j As Integer
myFile = "C:\Reformatted.txt"
Set rng = Selection
Open myFile For Output As #1
Dim strArr(1 To 27) As String, intBeg As Integer, intEnd As Integer, intCount As Integer
For I = 2 To rng.Rows.Count
For j = 1 To rng.Columns.Count
If InStr(1, CStr(Cells(1, j).Value), "-") = 0 Then
strArr(Val(Cells(1, j).Value)) = Cells(I, j).Value
Else
intBeg = Val(Left(Cells(1, j).Value, InStr(1, Cells(1, j).Value, "-") - 1))
intEnd = Val(Right(Cells(1, j).Value, Len(Cells(1, j).Value) - InStr(1, Cells(1, j).Value, "-")))
intCount = 1
For t = intBeg To intEnd
strArr(t) = Mid(Cells(I, j).Value, intCount, 1)
intCount = intCount + 1
Next t
End If
Next j
For t = 1 To UBound(strArr)
If strArr(t) = "" Then strArr(t) = " "
cellValue = cellValue + strArr(t)
Next t
Erase strArr
Print #1, cellValue
cellValue = ""
Next I
Close #1
Shell "C:\Windows\Notepad.exe C:\Reformatted.txt", 1
End Sub
Related
Now I'm using MS Excel 2019. I desire to make function to get text at Summary Steps column and Sumary Values column from Steps and Values Column
It's described as .
I tried with this function. However, It doesn't work at all
Function Congdoan_Time(Congdoan As Range, Time As Range, gtri As Boolean) As String
Dim xValue, TimeValue As String
Dim xChar As String
Dim xOutValue, xTimeValue As String
xValue = Congdoan.Value
TimeValue = Time.Value
Dim arr, timearr As Variant
Dim text, texttime As String
Dim nextarr As Variant
arr = Split(xValue, ",")
timearr = Split(TimeValue, "-")
Dim i As Long
Dim vallue As Variant
vallue = timearr(0)
For i = LBound(arr) To UBound(arr) - 1
If arr(i) = arr(i + 1) And i < UBound(arr) - 1 Then
vallue = Val(vallue) + Val(timearr(i + 1))
End If
If arr(i) = arr(i + 1) And i = UBound(arr) - 1 Then
End If
If arr(i) <> arr(i + 1) Then
xOutValue = xOutValue & "," & arr(i)
xTimeValue = xTimeValue & "-" & vallue
vallue = Val(timearr(i + 1))
End If
Next i
If xOutValue = "" Then
xOutValue = Join(arr, ",")
xTimeValue = vallue
End If
text = Right(xOutValue, Len(xOutValue) - 1)
nextarr = Split(text, ",")
If arr(UBound(arr)) <> nextarr(UBound(nextarr)) Then
text = text & "," & arr(UBound(arr))
xTimeValue = xTimeValue & "-" & Val(vallue) + Val(timearr(UBound(arr)))
End If
If gtri = True Then
Congdoan_Time = text
Else
Congdoan_Time = xTimeValue
End If
End Function
Formula at Sumary Steps Column
at Sumary Values Column
Please help to make another funtion that's work for me
Thank you
My two cents using a dictionary:
Function Summary(steps As String, vals As String, pick As Boolean) As String
Dim arr_steps As Variant, arr_vals As Variant
Dim new_steps() As Variant, new_vals() As Variant
arr_steps = Split(steps, ",")
arr_vals = Split(vals, "-")
ReDim new_steps(UBound(arr_steps))
ReDim new_vals(UBound(arr_steps))
For x = 0 To UBound(arr_steps)
If x = 0 Then
new_steps(x) = arr_steps(x)
new_vals(x) = arr_vals(x)
ElseIf arr_steps(x) = arr_steps(x - 1) Then
new_vals(x) = CDbl(new_vals(x - 1)) + CDbl(arr_vals(x))
new_vals(x - 1) = ""
Else
new_steps(x) = arr_steps(x)
new_vals(x) = arr_vals(x)
End If
Next
If pick Then
Summary = Join(new_steps, ",")
Else
Summary = Join(new_vals, "-")
End If
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "(?:^-+|[-,]+([-,])|,+$)"
Summary = .Replace(Summary, "$1")
End With
End Function
Formula in C1:
=Summary(A1,B1,1)
Formula in D1:
=Summary(A1,B1,0)
Note: My locale uses decimal-comma instead of point. It should work out fine if yours is using dots. I just had to change these in the input.
I'm not sure I fully understand the question but looking at your examples it looks like you want to deduplicate a comma delimited string but only where duplicates are in series. Something like this does that quite optimally:
Function SerialDedupe(ByVal s As String) As String
Dim v: v = Split(s, ",")
Dim iLB As Long: iLB = LBound(v)
Dim index As Long: index = iLB
Dim i As Long
For i = iLB + 1 To UBound(v)
If v(i) <> v(index) Then
index = index + 1
v(index) = v(i)
End If
Next
ReDim Preserve v(iLB To index)
SerialDedupe = Join(v, ",")
End Function
Tests:
1,2,2,2,3,3,2 ==> 1,2,3,2
A,B,B,C,B,B,C,D,B,C,B,A,A,B ==> A,B,C,B,C,D,B,C,B,A,B
1,2,2 ==> 1,2
1,1,2 ==> 1,2
1,2 ==> 1,2
1 ==> 1
I have a generated list of part numbers (A2:A100), and their quantities (B2:B100), for a particular order number (C2:C100). I am writing a sub which will filter the list of part numbers for each unique part number and then create a new list with the total quantity of each part and every order where it will be used.
I have a sub that successfully creates a list of unique part numbers (F8:F100), then another sub auto-filters the main list (A2:A100) of part numbers for each unique part number and creates a range for the order numbers (C2:C100) for that particular part. I have tried to concatenate the range of order numbers, but my function is failing.
Sub WOSorter()
Dim rng As Range
Dim WOrng As Range
Dim i As Long
Dim Limit As Long
Dim seperator As String
seperator = ", "
Limit = Worksheets("Selector").Range("F8:F100").Cells.SpecialCells(xlCellTypeConstants).Count - 1
For i = 0 To Limit
Set rng = Worksheets("Selector").Cells(8 + i, 6)
With Worksheets("Selector").Range("A1")
.AutoFilter Field:=1, Criteria1:=rng
Set WOrng = Worksheets("Selector").Range("C2:C100").Cells.SpecialCells(xlCellTypeVisible)
Worksheets("Selector").Cells(8 + i, 9).Value = ConcatenateRange(WOrng, seperator)
End With
Next
If Worksheets("Selector").AutoFilterMode Then Worksheets("Selector").AutoFilter.ShowAllData
End Sub
-----------------------------------------------------------------------------
Function ConcatenateRange(ByVal WOrng As Range, Optional ByVal seperator As String) As String
Dim newString As String
Dim cellArray As Variant
Dim i As Long, j As Long
cellArray = WOrng.Value
For i = 1 To UBound(cellArray, 1)
For j = 1 To UBound(cellArray, 2)
If Len(cellArray(i, j)) <> 0 Then
newString = newString & (seperator & cellArray(i, j))
End If
Next
Next
If Len(newString) <> 0 Then
newString = Right$(newString, (Len(newString) - Len(seperator)))
End If
ConcatenateRange = newString
End Function
I am currently getting a type mismatch error on the line:
For i = 1 To UBound(cellArray, 1)
If the original list is in colA, B, C with unique part numbers in colF:
colA colB colC colF
123-4 1 01111 123-4
456-7 2 02222 456-7
123-4 1 03333 789-0
789-0 1 04444
456-7 3 05555
Then the result should be:
colA colB colC colF colI
123-4 1 01111 123-4 01111, 03333
456-7 2 02222 456-7 02222, 05555
123-4 1 03333 789-0 04444
789-0 1 04444
456-7 3 05555
Using the function on the link change your code to:
Sub WOSorter()
Dim seperator As String
seperator = ", "
With Worksheets("Selector")
Dim lstrow As Long
lstrow = .Cells(.Rows.Count, "F").End(xlUp).Row
Dim i As Long
For i = 2 To lstrow
.Range("I" & i).Value = TEXTJOINIFS(.Range("C:C"), seperator, .Range("A:A"), .Range("F" & i).Value)
Next i
End With
End Sub
This does not rely on filter which will not allow the bulk load of arrays.
Here is the textjoinifs function:
Function TEXTJOINIFS(rng As Range, delim As String, ParamArray arr() As Variant) As String
Dim rngarr As Variant
rngarr = Intersect(rng, rng.Parent.UsedRange).Value
Dim condArr() As Boolean
ReDim condArr(1 To Intersect(rng, rng.Parent.UsedRange).Rows.Count) As Boolean
TEXTJOINIFS = ""
Dim i As Long
For i = LBound(arr) To UBound(arr) Step 2
Dim colArr() As Variant
colArr = Intersect(arr(i), arr(i).Parent.UsedRange).Value
Dim j As Long
For j = LBound(colArr, 1) To UBound(colArr, 1)
If Not condArr(j) Then
Dim charind As Long
charind = Application.Max(InStr(arr(i + 1), ">"), InStr(arr(i + 1), "<"), InStr(arr(i + 1), "="))
Dim opprnd As String
If charind = 0 Then
opprnd = "="
Else
opprnd = Left(arr(i + 1), charind)
End If
Dim t As String
t = """" & colArr(j, 1) & """" & opprnd & """" & Mid(arr(i + 1), charind + 1) & """"
If Not Application.Evaluate(t) Then condArr(j) = True
End If
Next j
Next i
For i = LBound(rngarr, 1) To UBound(rngarr, 1)
If Not condArr(i) Then
TEXTJOINIFS = TEXTJOINIFS & rngarr(i, 1) & delim
End If
Next i
If TEXTJOINIFS <> "" Then
TEXTJOINIFS = Left(TEXTJOINIFS, Len(TEXTJOINIFS) - Len(delim))
End If
End Function
Here is the ouput:
Excel spreadsheet
I have a set of over 10,000 lines of text strings in column A (Input), and I need to get the number (in case there is only one) or a sum of both (in case there are two).
Code
Here is the VBA code I have:
Sub ExtractNumericStrings()
Dim rngTemp As Range
Dim strTemp As String
Dim currNumber1 As Currency
Dim currNumber2 As Currency
Dim lngTemp As Long
Dim lngPos As Long
Dim lngLastRow As Long
With ActiveSheet
lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For Each rngTemp In .Cells(1, "A").Resize(lngLastRow, 1) ' Set Range to look at
strTemp = rngTemp.Value2 ' Get string value of each cell
lngTemp = Len(strTemp) 'Get length of string
currNumber1 = 0 ' Reset value
currNumber2 = 0 ' Reset value
' Get first number
currNumber1 = fncGetNumericValue(strTemp, 1) ' Strip out first number
' Get second number if exists
' First strip out first number
strTemp = Replace(strTemp, currNumber1, "")
If Len(strTemp) <> 0 Then
currNumber2 = fncGetNumericValue(strTemp, 1)
End If
' now paste to sheet
If currNumber1 <> 0 And currNumber2 <> 0 Then
rngTemp.Offset(0, 1).Value = currNumber1 + currNumber2
rngTemp.Offset(0, 2).Value = "sum of the numbers"
ElseIf currNumber1 <> 0 Then
rngTemp.Offset(0, 1).Value = currNumber1
End If
Next rngTemp
End With
Call MsgBox("Procedure Complete!", vbOKOnly + vbInformation, "Procedure Complete")
End Sub
Private Function fncGetNumericValue(strTemp As String, lngStart As Long) As Currency
Dim varTemp As Variant
Dim lngCount As Long
Dim lngTemp As Long
' Reset
lngCount = 1
lngTemp = 1
varTemp = ""
On Error Resume Next
If IsNumeric(Left(strTemp, lngCount)) Then
Do While IsNumeric(Left(strTemp, lngCount)) = True
varTemp = Left(strTemp, lngCount)
lngCount = lngCount + 1
If lngCount > Len(strTemp) Then
Exit Do
End If
Loop
Else
' First clear non-numerics from string
lngTemp = 1
Do While IsNumeric(Left(strTemp, 1)) = False
lngTemp = lngTemp + 1
strTemp = Mid(strTemp, 2, Len(strTemp) - 1)
If lngTemp > Len(strTemp) Then
Exit Do
End If
Loop
' Then extract second number if exists
If strTemp <> "" Then
Do While IsNumeric(Mid(strTemp, lngCount, 1)) = True
varTemp = Left(strTemp, lngCount)
lngCount = lngCount + 1
If lngCount > Len(strTemp) Then
Exit Do
End If
Loop
End If
End If
' Retrun Value
If IsNumeric(varTemp) Then
fncGetNumericValue = CCur(varTemp)
Else
fncGetNumericValue = 0
End If
End Function
Here is what I'm trying to do:
https://www.youtube.com/watch?v=EjHnJVxuWJA
I have very limited knowledge of VBA, so please excuse me if I ask any stupid question. Running this thing successfully will save me hips of time. thanks!
Something like this:
Private Sub extract_num()
Dim cell as Range
Dim ws as Worksheet: Set ws = Sheets("Sheet1") ' replace Sheet1 with ur sheet name
Dim lr as Long: Set lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
Dim values() As String
Dim i as Byte
Dim temp as Double
For Each cell in ws.Range("A2:A" & lr)
If Not isEmpty(cell) Then
values = Split(cell, " ")
For i = LBound(values) to UBound(values)
values(i) = Replace(values(i), ",", ".")
If isNumeric(values(i)) Then
temp = temp + values(i)
End If
Next i
cell.Offset(0, 2) = temp
temp = 0
End If
Next cell
End Function
This is presuming:
a) Individual words and numbers are always separated by space "123 abc 321"
b) Commas "," are used as an arithmetic floatpoint separator ##,##
Slightly different approach from Rawrplus
Option Explicit
Sub UpdateTotals()
Dim aRawValues As Variant
Dim iLRow&, iRow&, iArr&
Dim dTotal#
With ThisWorkbook.Worksheets("Sheet1") '<-- Change the sheet name to your sheet
iLRow = .Cells(Rows.Count, 1).End(xlUp).Row ' Get row count
For iRow = 1 To iLRow ' Loop through all rows in the sheet
aRawValues = Split(.Range("A" & iRow).Value, " ") ' Create and array of current cell value
For iArr = LBound(aRawValues) To UBound(aRawValues) ' Loop through all values in the array
dTotal = dTotal + ReturnDouble(Replace(aRawValues(iArr), ",", ".")) ' Add the returned double to total
Next
.Range("B" & iRow).Value = dTotal ' Set value in column B
dTotal = 0# ' Reset total
Next
End With
End Sub
Function ReturnDouble(ByVal sTextToConvert As String) As Double
Dim iCount%
Dim sNumbers$, sCurrChr$
sNumbers = ""
For iCount = 1 To Len(sTextToConvert)
sCurrChr = Mid(sTextToConvert, iCount, 1)
If IsNumeric(sCurrChr) Or sCurrChr = "." Then
sNumbers = sNumbers & sCurrChr
End If
Next
If Len(sNumbers) > 0 Then
ReturnDouble = CDbl(sNumbers)
Else
ReturnDouble = 0#
End If
End Function
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
My Data is this CSI [40%], CSSEl [50%], LDN [10%] within one cell separated by comma. can any one help me with excel vba code which will sort percentages in descending order and provide me output as this: CSSEl [50%],CSI [40%],LDN [10%].
This will do it for you.
Add the below code into a new Module within VBA and then call the formula in a cell adjacent to your cell with the values.
So in the example below, your original values are in column A and the custom UDF in column B directly adjacent.
Public Function SortByInternalNumber(ByVal strText As String, ByVal strDelimiter As String)
Dim arrValues() As String, strValue As String, i As Long, lngNumber As Long, arrNumbers() As String
Dim strNumber As String, lngMaxNumber As Long, lngMaxIndex As Long, strResult As String
Dim bFound As Boolean, arrMaxValues() As Long, lngIndex As Long, strMaxValue As String
Dim strThisValue As String
Application.Volatile
' Split up the initial string with all of the values.
arrValues = Split(strText, strDelimiter)
For i = 0 To UBound(arrValues)
strValue = Trim(arrValues(i))
strNumber = Replace(Replace(Split(strValue, "[")(1), "%", ""), "]", "")
ReDim Preserve arrNumbers(i)
arrNumbers(i) = strNumber
Next
' Now process all of the numbers in the descending order.
Do While 1 = 1
lngMaxNumber = -1
bFound = False
For i = 0 To UBound(arrNumbers)
If arrNumbers(i) <> "" Then
lngNumber = CLng(arrNumbers(i))
If lngMaxNumber < lngNumber Then
lngMaxNumber = lngNumber
lngMaxIndex = i
End If
bFound = True
End If
Next
If Not bFound Then Exit Do
lngIndex = -1
' Retrieve all of the values that are of the same value as the current max.
For i = 0 To UBound(arrNumbers)
If arrNumbers(i) <> "" Then
If CLng(arrNumbers(i)) = lngMaxNumber Then
lngIndex = lngIndex + 1
ReDim Preserve arrMaxValues(lngIndex)
arrMaxValues(lngIndex) = i
End If
End If
Next
strMaxValue = ""
' Now do the same thing as above but instead of descending, do ascending.
For i = 0 To UBound(arrMaxValues)
strThisValue = Trim(arrValues(arrMaxValues(i)))
If strMaxValue > strThisValue Or strMaxValue = "" Then
strMaxValue = strThisValue
lngMaxIndex = arrMaxValues(i)
End If
Next
strResult = strResult & ", " & strMaxValue
arrNumbers(lngMaxIndex) = ""
Loop
If strResult <> "" Then
strResult = Mid(strResult, 3)
End If
SortByInternalNumber = Trim(strResult)
End Function
It's fairly rigid in but I have demonstrated that you can parameterize more of the relevant options.
I hope that makes sense and I hope it's what you're after.
I think the easiest way is to create a helper column where you extract the numeric value. Assuming your data starts in A1, write a formula in column B like
=MID(A1, FIND("[", A1)+1, FIND("]",A1)-FIND("[", A1)-1)
With this, you can easily sort the data.
May try another alternative
Sub testsort()
Dim txt As String, txt2 As String, Arr As Variant
Dim Nums() As Long, NumSort() As Long, i As Integer, k As Integer
txt = "CSI [40%], CSSEl [50%], LDN [10%], ABC [40%],ZXH[30%]"
Arr = Split(txt, ",")
ReDim Nums(LBound(Arr) To UBound(Arr))
ReDim NumSort(LBound(Arr) To UBound(Arr))
For i = LBound(Arr) To UBound(Arr)
Spos = InStr(1, Arr(i), "[")
Epos = InStr(1, Arr(i), "%")
If Spos > 0 And Epos > Spos Then
Nums(i) = Val(Mid(Arr(i), Spos + 1, Epos - Spos - 1))
Else
Nums(i) = 0
End If
Next i
For i = LBound(Arr) To UBound(Arr)
NumSort(i) = LBound(Arr)
For k = LBound(Arr) To UBound(Arr)
If Nums(i) < Nums(k) Or (Nums(i) = Nums(k) And k < i) Then
NumSort(i) = NumSort(i) + 1
End If
Next
Debug.Print Arr(i), Nums(i), NumSort(i)
Next
For i = LBound(Arr) To UBound(Arr) ' rank
For k = LBound(Arr) To UBound(Arr)
If NumSort(k) = i Then
txt2 = txt2 & Arr(k) & ","
Exit For
End If
Next k
Next i
If Len(txt2) > 0 Then txt2 = Left(txt2, Len(txt2) - 1) 'delete last comma
Debug.Print txt2
End Sub
My rows do not have the same length and I need to avoid the "blanks" in between when I export to CSV.
For example, when I export this:
1 2 3 4 5
1 2
1 3 3 4
1 2 3 4 5
I get this:
1,2,3,4,5
1,2,,,
1,3,3,4,
1,2,3,4,5
And I need to remove the extra seperators from the empty cells between.
I am already running a macro to export as CSV, so it would be best if I could "delete" the empty cells in the beginning of this.
This small macro will:
avoid creating empty CSV records corresponding to empty Excel rows
avoid trailing commas
Option Explicit
Sub CSV_Makerr()
Dim r As Range
Dim sOut As String, k As Long, M As Long
Dim N As Long, nFirstRow As Long, nLastRow As Long
Dim MyFilePath As String, MyFileName As String
Dim fs, a, mm As Long
Dim separator As String
ActiveSheet.UsedRange
Set r = ActiveSheet.UsedRange
nLastRow = r.Rows.Count + r.Row - 1
nFirstRow = r.Row
separator = ","
MyFilePath = "C:\TestFolder\"
MyFileName = "whatever"
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(MyFilePath & MyFileName & ".csv", True)
For N = nFirstRow To nLastRow
k = Application.WorksheetFunction.CountA(Cells(N, 1).EntireRow)
sOut = ""
If k = 0 Then
Else
M = Cells(N, Columns.Count).End(xlToLeft).Column
For mm = 1 To M
sOut = sOut & Cells(N, mm).Text & separator
Next mm
sOut = Left(sOut, Len(sOut) - 1)
a.writeline (sOut)
End If
Next
a.Close
End Sub
I found it was pretty simple to solve, I just added a loop to check and delete if the last symbol in a line was a seperator
With Selection
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
Else
With ActiveSheet.UsedRange
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
End If
For RowNdx = StartRow To EndRow
WholeLine = ""
For ColNdx = StartCol To EndCol
If Cells(RowNdx, ColNdx).Value = "" Then
CellValue = ""
Else
CellValue = Cells(RowNdx, ColNdx).Value
End If
WholeLine = WholeLine & CellValue & Sep
Next ColNdx
WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
//I added this to delete the last seperator in a line before printing
Dim last As String
last = Right(WholeLine, 1)
Do Until last <> ","
WholeLine = Left(WholeLine, Len(WholeLine) - 1)
last = Right(WholeLine, 1)
Loop
Print #nFileNum, WholeLine
Next RowNdx