使用FileDialog打开工作簿并进行操作 [英] Open a workbook using FileDialog and manipulate it

查看:72
本文介绍了使用FileDialog打开工作簿并进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow上找到了以下代码:

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

我知道这段代码应该在FileDialog中选择一个文件. 但是,一旦选择了.xls文件,如何操作该文件?换句话说,我的文件对象在哪里可以操纵?

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.

推荐答案

下面是一个示例:

Dim wb As Workbook
Dim ws As Worksheet
Dim r As Range

Set wb = Workbooks.Open(txtfilename) ' the file path you selected in FileDialog
Set ws = wb.Worksheets(1)
Set r = ws.Cells(1, 1)

With r    
    .Value = "Hello world!"
    .Interior.Color = RGB(255,20,20) 'bright red
End With 

这篇关于使用FileDialog打开工作簿并进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆