Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
941 views
in Technique[技术] by (71.8m points)

file - Open a workbook using FileDialog and manipulate it

I found this code here on StackOverflow:

Dim fd As Office.FileDialog

    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    With fd

        .AllowMultiSelect = False
        .Title = "Please select the file to kill his non colored cells"
        .Filters.Add "Excel", "*.xls"
        .Filters.Add "All", "*.*"

        If .Show = True Then
            txtFileName = .SelectedItems(1)
        End If

    End With

I know this code should select a file in FileDialog. However, once I have chosen the .xls file, how do I manipulate the file? In other words, where is my file object for me to manipulate?

I would like someone to continue this code to make some simple manipulation on the workbook so I could learn how to do those simple things on a workbook that I opened.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two approaches for you (I prefer using first one). In both approaches wb variable stores opened workbook. I commented code in details, but if you have some questions - ask:)

First approach:

Sub test1()
    Dim xlFileName
    Dim wb As Workbook

    xlFileName = GetOpenFilename("Excel (*.xls*),*.xls*", 1, _
        "Please select the file to kill his non colored cells")

    'if user pressed CANCEL - exit sub
    If xlFileName = False Then
        MsgBox "User pressed CANCEL"
        Exit Sub
    End If

    'Tries to open workbook with choosen file name
    On Error Resume Next
    Set wb = Application.Workbooks.Open(xlFileName)
    On Error GoTo 0

    'If we can't find workbook with choosen path, exit Sub
    If wb Is Nothing Then
        MsgBox "Can't find file"
        Exit Sub
    End If

     'your code here
    wb.Worksheets("Sheet1").Range("A1").Value = "test"

    'close workbook with saving changes
    wb.Close SaveChanges:=True
    Set wb = Nothing

End Sub

Second approach:

Sub test()
    Dim xlFileName As String
    Dim fd As Office.FileDialog
    Dim wb As Workbook

    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    With fd

        .AllowMultiSelect = False
        .Title = "Please select the file to kill his non colored cells"
        .Filters.Add "Excel", "*.xls*"
        .Filters.Add "All", "*.*"

        If .Show Then
           xlFileName = .SelectedItems(1)
        Else
           'if user pressed CANCEL - exit sub
           MsgBox "User pressed CANCEL"
           Exit Sub
        End If

    End With

    'Tries to open workbook with choosen file name       
    On Error Resume Next
    Set wb = Workbooks.Open(xlFileName)
    On Error GoTo 0

    'If we can't find workbook with choosen path, exit Sub
    If wb Is Nothing Then
        MsgBox "Can't find file"
        Exit Sub
    End If

    'your code here
    wb.Worksheets("Sheet1").Range("A1").Value = "test"

    'close workbook with saving changes
    wb.Close SaveChanges:=True
    Set wb = Nothing

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...