Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不在vba中工作的多个单元格的范围_Vba_Excel - Fatal编程技术网

不在vba中工作的多个单元格的范围

不在vba中工作的多个单元格的范围,vba,excel,Vba,Excel,我正在尝试同时访问多个单元格,如下所示: Set rng = Worksheets("dts").Range("A3,C3:D3,G8,I8:J8,G9,I9:J9,G21,I21:J21,G30,I30:J30,G39,I39:J39") rangetoHTML(rng) Function rangetoHTML(rng As Range) ' Changed by Ron de Bruin 28-Oct-2006 ' Working in Office 2000-2013 Dim

我正在尝试同时访问多个单元格,如下所示:

Set rng = Worksheets("dts").Range("A3,C3:D3,G8,I8:J8,G9,I9:J9,G21,I21:J21,G30,I30:J30,G39,I39:J39")
rangetoHTML(rng)

Function rangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    rangetoHTML = ts.readall
    ts.Close
    rangetoHTML = Replace(rangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing

End Function
当我设置为HTML时,如下所示:

Set rng = Worksheets("dts").Range("A3,C3:D3,G8,I8:J8,G9,I9:J9,G21,I21:J21,G30,I30:J30,G39,I39:J39")
rangetoHTML(rng)

Function rangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    rangetoHTML = ts.readall
    ts.Close
    rangetoHTML = Replace(rangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing

End Function

它跳过rng.Copy行。不知道为什么。需要一些指导。

您必须在所有要复制的范围单元格中循环

但是
Range
对象的
Areas
属性可能会有所帮助,您也可以避免设置
tempWb
,而只需“动态”创建它,并在它不再有用后关闭它

具体如下:

Function rangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    Dim area As Range, cellToOffsetFrom As Range

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    With Workbooks.Add(1) '<--| create temp wb
        With .Sheets(1)
            Set cellToOffsetFrom = rng.Areas(1).Cells(1, 1) '<--| get the 'rng' upleftmost cell as reference for offsetting all other ones
            For Each area In rng.Areas '<--| loop through 'Areas'
                area.Copy '<--| copy single 'Area', i.e. contiguous cells
                With .Cells(area.Cells(1, 1).Row - cellToOffsetFrom.Row + 1, area.Cells(1, 1).Column - cellToOffsetFrom.Column + 1) '<--| reference proper target cell to paste values
                    .PasteSpecial Paste:=8
                    .PasteSpecial xlPasteValues, , False, False
                    .PasteSpecial xlPasteFormats, , False, False
                End With
                Application.CutCopyMode = False
            Next
            On Error Resume Next
            .DrawingObjects.Visible = True
            .DrawingObjects.Delete
            On Error GoTo 0
        End With

        With .PublishObjects.Add( _
            SourceType:=xlSourceRange, _
            Filename:=TempFile, _
            Sheet:=.Sheets(1).Name, _
            Source:=.Sheets(1).UsedRange.Address, _
            HtmlType:=xlHtmlStatic)
            .Publish (True)
        End With
    End With
    ActiveWorkbook.Close savechanges:=False '<--|'Close TempWB

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    rangetoHTML = ts.readall
    ts.Close
    rangetoHTML = Replace(rangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing

End Function
函数范围HTML(rng作为范围)
“由Ron de Bruin于2006年10月28日更改
“2000-2013年在办公室工作
作为对象的Dim fso
将T作为对象
将文件设置为字符串
将TempWB设置为工作簿
变暗区域作为范围,单元格从作为范围偏移
TempFile=Environ$(“temp”)和“\”格式(现在是“dd-mm-yy h-mm-ss”)和“.htm”
'复制范围并创建一个新工作簿,以超过中的数据

使用工作簿。添加(1)“您必须循环所有要复制的范围单元格

但是
Range
对象的
Areas
属性可能会有所帮助,您也可以避免设置
tempWb
,而只需“动态”创建它,并在它不再有用后关闭它

具体如下:

Function rangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    Dim area As Range, cellToOffsetFrom As Range

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    With Workbooks.Add(1) '<--| create temp wb
        With .Sheets(1)
            Set cellToOffsetFrom = rng.Areas(1).Cells(1, 1) '<--| get the 'rng' upleftmost cell as reference for offsetting all other ones
            For Each area In rng.Areas '<--| loop through 'Areas'
                area.Copy '<--| copy single 'Area', i.e. contiguous cells
                With .Cells(area.Cells(1, 1).Row - cellToOffsetFrom.Row + 1, area.Cells(1, 1).Column - cellToOffsetFrom.Column + 1) '<--| reference proper target cell to paste values
                    .PasteSpecial Paste:=8
                    .PasteSpecial xlPasteValues, , False, False
                    .PasteSpecial xlPasteFormats, , False, False
                End With
                Application.CutCopyMode = False
            Next
            On Error Resume Next
            .DrawingObjects.Visible = True
            .DrawingObjects.Delete
            On Error GoTo 0
        End With

        With .PublishObjects.Add( _
            SourceType:=xlSourceRange, _
            Filename:=TempFile, _
            Sheet:=.Sheets(1).Name, _
            Source:=.Sheets(1).UsedRange.Address, _
            HtmlType:=xlHtmlStatic)
            .Publish (True)
        End With
    End With
    ActiveWorkbook.Close savechanges:=False '<--|'Close TempWB

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    rangetoHTML = ts.readall
    ts.Close
    rangetoHTML = Replace(rangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing

End Function
函数范围HTML(rng作为范围)
“由Ron de Bruin于2006年10月28日更改
“2000-2013年在办公室工作
作为对象的Dim fso
将T作为对象
将文件设置为字符串
将TempWB设置为工作簿
变暗区域作为范围,单元格从作为范围偏移
TempFile=Environ$(“temp”)和“\”格式(现在是“dd-mm-yy h-mm-ss”)和“.htm”
'复制范围并创建一个新工作簿,以超过中的数据

使用Workbooks.Add(1)'Excel不允许您复制“多项选择”-这就是我在测试代码时得到的结果为什么会这样?Excel就是这样。如果您在GUI中尝试,它也会这样做。尝试在GUI中进行多重选择并键入
CTRL+C
,您将收到相同的消息。Excel不允许您复制“多重选择”-这就是我在测试代码时得到的结果。为什么会这样?Excel就是这样。如果您在GUI中尝试,它也会这样做。尝试在GUI中进行多重选择并键入
CTRL+C
,您将收到相同的消息。