0

我有已关闭的 sheet1.xls。我在 sheet2.xls VBA 中工作,如下所示。

With Range("A:XFD")
 <I need Some Code here to format entire sheet1.xls cells into string format> 
End With

亲切的帮助。谢谢

4

3 回答 3

1

像这样的东西可以让你格式化合上的书。它打开,然后格式化,然后再次关闭。

Option Explicit
Public Sub Format_Closed_Sheet()
    Dim sht1book As Workbook
    Dim sht1ws As Worksheet
    Dim strValue As String
    Dim rng As Range

    Application.ScreenUpdating = False

    With Application

    '--> Open sheet1.xls
    Set sht1book = .Workbooks.Open _
    ("Path to Sheet1.xls")
    End With

    Set sht1ws = sht1book.Sheets("Sheet1")

    '--> Format the range as text

    Set rng = sht1ws.Range("A:XFD")
    rng.NumberFormat = "@"

    '--> Save sheet1.xls and close
    Application.DisplayAlerts = False
    sht1book.Save
    sht1book.Close
    Application.DisplayAlerts = True

    Application.ScreenUpdating = True
End Sub
于 2012-06-13T14:59:19.163 回答
1

我会这样做:

Dim b As Workbook
Dim sh As Worksheet

Set b = Workbooks.Open("C:\mypath\mybook.xls") ' or wherever your workbook is

Set sh = b.Sheets("Sheet1") ' or whatever sheet
sh.Cells.NumberFormat = "@" ' format as Text

b.Close

如果要将工作簿中的所有工作表格式化为文本,可以执行以下操作:

For Each sh In wb.Sheets
    sh.Cells.NumberFormat = "@" ' format as Text
Next sh
于 2012-06-13T15:12:08.003 回答
0

只需声明一个工作簿和工作表:

dim oBook as excel.workbook
dim oSheet as excel.worksheet

set oBook = workbooks.open("<workbook path and filename>")
set oSheet = oBook.sheets("<SheetName>")

然后:

with oSheet.range("A:XFD") 
     <Format>
end with
oBook.close
set oBook = nothing 
set oSheet = nothing

等等。

于 2012-06-13T13:24:16.363 回答