仅复制可见表格中的单元格并粘贴到下一个自由列VBA中

时间:2015-08-17 18:32:54

标签: excel vba excel-vba

我有一个VBA代码,当前正在运行所有工作表中的单元格(C2:C3),并粘贴在“主”表格中。我的问题是我希望它只复制可见的工作表,因为我的一些隐藏的工作表有不同的数据运行我的工作表。我也有一个问题,它粘贴在下一行,我希望它粘贴在下一列,无法弄清楚:/。

 Option Explicit

Sub Sample()
Dim wsInput As Worksheet, wsOutput As Worksheet
Dim rng As Range
Dim LRowO As Long, LRowI As Long
Set wsOutput = ThisWorkbook.Sheets("Master")
For Each wsInput In ThisWorkbook.Worksheets
    If wsInput.Name <> wsOutput.Name Then
        With wsInput
            Set rng = .Range("C2:C3")
            rng.Copy
            With wsOutput
                LRowO = .Range("A" & .Rows.Count).End(xlUp).Row + 1
                .Range("A" & LRowO).PasteSpecial xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
        End With
    End If
Next wsInput

Exit Sub

 End Sub

3 个答案:

答案 0 :(得分:3)

听起来你需要检查工作表是否被隐藏,并且还要跟踪下一列的内容,并在每次粘贴时增加它。以下是为这两件事修改的代码

 Option Explicit

Sub Sample()
Dim wsInput As Worksheet, wsOutput As Worksheet
Dim rng As Range
LRowI As Long
Dim nextCol as Long
Set wsOutput = ThisWorkbook.Sheets("Master")
nextCol = 1
For Each wsInput In ThisWorkbook.Worksheets
    If wsInput.Name <> wsOutput.Name and wsInput.Visible = True Then
        With wsInput
            Set rng = .Range("C2:C3")
            rng.Copy
            With wsOutput
                .Cells(1, nextCol).PasteSpecial xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                nextCol = nextCol + 1
            End With
        End With
    End If
Next wsInput

Exit Sub

 End Sub

答案 1 :(得分:1)

检查

<?php

        $doc = JFactory::getDocument();
        $doc->addScript(JUri::base() .'js/jquery-1.7.1.min.js',true);
        $doc->addScript(JUri::base() .'js/jspdf.debug.js',true);
        $doc->addScript(JUri::base() .'js/basic.js',true);
        $doc->addScript(JUri::base() .'js/png.js',true);
        $doc->addScript(JUri::base() .'js/png_support.js',true);
        $doc->addScript(JUri::base() .'js/zlib.js',true);
        $doc->addScript(JUri::base() .'js/FileSaver.js',true);
        $doc->addScript(JUri::base() .'js/tableExport.js',true);
        $doc->addScript(JUri::base() .'js/jquery.base64.js',true);
        $doc->addScript(JUri::base() .'js/html2canvas.js',true);
        $doc->addScript(JUri::base() .'js/standard_fonts_metrics.js',true);
        $doc->addScript(JUri::base() .'js/split_text_to_size.js',true);
        $doc->addScript(JUri::base() .'js/from_html.js',true);
    $script = "function loadtable(){
    var doc = new jsPDF();
    doc.text(20, 20, 'Hello world.');
    doc.save('Test.pdf');}";
    $doc->addScriptDeclaration($script);
    echo "<button id='buttonMU' class='gen_btn' onclick='loadtable()'>Load Pdf</button>";
    ?>

属性。例如,更改

worksheets(wsInput).visible

If wsInput.Name<>wsOutput.name Then

这只会使用可见的工作表。

此处的更多信息https://msdn.microsoft.com/en-us/library/office/ff197786.aspx

我不认为我完全理解你问题的第二部分。

答案 2 :(得分:1)

你应该可以使用这样的东西来检查你的床单是否可见/隐藏。

@app.route()

If wsInput.Visible = True Then
        ' Do copy here     

然后,为了粘贴到下一列,可以使用offset属性。

If Sheets(wsInput.Name).Visible = True Then
        ' Do copy here

ActiveCell.Offset(rowOffset, columnOffset).Activate rowOffset更改为直接从右侧一列到达单元格所需的数字。即。

columnOffset

将当前选择从ActiveCell.Offset(0, 1).Activate 更改为A1