Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a spreadsheet where the user can select multiple non-contiguous rows. I am to loop through the records and create a comma delimited file. I know how to create the comma delimited file.
However, my questions is how do I determine the rows/areas that was selected, then loop through each area to process each row within that area.
Thanks a lot!

I don't know excel vba code too well, but i took a stab at it.
It is something like the following, however, this doesn't work properly and it seems to delete the data in the cells, but this is sort of what you need to do.
Option Explicit
Sub SaveCSVFile()
Dim FP As Long
FP = FreeFile()
Open "C:\MyFile.csv" For Output As #FP
Write #FP, GatherSelections
Close #FP
End Sub
Function GatherSelections() As String
Dim Selections As String
Dim T As Object
Dim Y As Long
Dim X As Long
Selections = ""
With Sheet1
For Y = 0 To .Range(ActiveWindow.RangeSelection.Address).Cells.Rows.Count - 1
For X = 0 To .Range(ActiveWindow.RangeSelection.Address).Cells.Columns.Count - 1
If .Cells(.Range(ActiveWindow.RangeSelection.Address).Cells.Row + Y, .Range(ActiveWindow.RangeSelection.Address).Cells.Column + X).Value <> "" Then
Selections = Selections & """" & .Range(ActiveWindow.RangeSelection.Address).Cells(Y, X).Value & ""","
End If
Next X
Selections = Selections & vbCrLf
Next Y
End With
GatherSelections = Selections
End Function
Chi Happens

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |