Computing.Net > Forums > Programming > Coding selection.areas in VBA

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Coding selection.areas in VBA

Reply to Message Icon

Name: tkylewatson
Date: September 18, 2003 at 08:44:39 Pacific
OS: Windows 2000
CPU/Ram: x86 Family 6 Model 8 Step
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: September 18, 2003 at 17:09:26 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Coding selection.areas in VBA

Combo Boxes in VBA www.computing.net/answers/programming/combo-boxes-in-vba/9015.html

Paste Special in VBA www.computing.net/answers/programming/paste-special-in-vba/6340.html

can't open references in vba www.computing.net/answers/programming/cant-open-references-in-vba/19860.html