从不同的列中获取唯一值,然后粘贴到另一张表中

时间:2019-02-15 05:38:25

标签: excel vba

我可以从一列(sheet1)中获取值,然后粘贴到另一列(sheet2)

Sub Test()
    Dim Sh1 As Worksheet
    Dim Rng As Range
    Dim Sh2 As Worksheet
    Set Sh1 = Worksheets("Sheet1")
    Set Rng = Sh1.Range("A1:A" & Sh1.Range("A65536").End(xlUp).Row)
    Set Sh2 = Worksheets("Sheet2")
    Rng.Cells(1, 1).Copy Sh2.Cells(1, 1)
    Rng.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sh2.Range("A1"), Unique:=True
End Sub

我想做的不是为多列(b,e,g)只做一列。我该怎么办?

2 个答案:

答案 0 :(得分:0)

您需要定义特定的列ColumnList = Array("B", "E", "G"),然后使用循环处理它们中的每一个For Each Col In ColumnList

我还建议使用有意义的变量名,例如wsInput而不是sh1,这会使您的代码更具可读性,因此更易于维护,从而减少了错误。

Option Explicit

Public Sub CopyUniqueDataOfColumns()
    Dim wsInput As Worksheet
    Set wsInput = ThisWorkbook.Worksheets("Sheet1")

    Dim wsOutput As Worksheet
    Set wsOutput = ThisWorkbook.Worksheets("Sheet2")

    Dim ColumnList() As Variant
    ColumnList = Array("B", "E", "G")

    Dim LastRow As Long
    Dim CopyRng As Range

    Dim Col As Variant
    For Each Col In ColumnList
        LastRow = wsInput.Cells(wsInput.Rows.Count, Col).End(xlUp).Row

        If LastRow > 1 Then 'If data is found copy it
            Set CopyRng = wsInput.Range(wsInput.Cells(1, Col), wsInput.Cells(LastRow, Col))
            CopyRng.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsOutput.Cells(1, Col), Unique:=True
        Else 'If no data copy only header
            wsOutput.Cells(1, Col).Value = wsInput.Cells(1, Col).Value
        End If
    Next Col
End Sub

答案 1 :(得分:-1)

尝试一下:

public class LinkPdf {

static final float INCH = 72;
public static List<PDAnnotation> annotations;

@SuppressWarnings("null")
public static void main(String[] args) throws Exception {
    PDRectangle position = null;
    PDDocument document = new PDDocument();
    try {
        for (int i = 0; i < 10; i++) {
            PDPage page = new PDPage();
            document.addPage(page);
        }
        PDPage page1 = document.getPage(0);

        HashMap<Integer, String> pgs = new HashMap<Integer, String>();

        for (int i = 0; i < document.getNumberOfPages(); i++) {
            pgs.put(i, "Jump to Page" + i);

        }

        PDFont font = PDType1Font.HELVETICA_BOLD;
        PDPageContentStream contents = new PDPageContentStream(document,
                page1);
        contents.beginText();
        contents.setFont(font, 18);
        contents.newLineAtOffset(50, 600);
        contents.setLeading(20f);
        contents.showText("PDFBox");

        position = new PDRectangle();
        for (int i = 0; i < document.getNumberOfPages(); i++) {

            contents.newLine();
            contents.showText(pgs.get(i));
            contents.newLine();
        }
        contents.endText();
        contents.close();

        PDRectangle[] pos1 = new PDRectangle[document.getNumberOfPages()];
        for(int i=0;i<document.getNumberOfPages();i++){
            pos1[i]=new PDRectangle();
            pos1[i].setLowerLeftX(50);
            pos1[i].setLowerLeftY(575-(i*40)); 
            pos1[i].setUpperRightX(INCH + 100);
            pos1[i].setUpperRightY(585-(i*40));
            getPage(document, page1, pgs.get(i), i, pos1[i]);
        }

        document.save("D:/link.pdf");
        System.out.println("Completed");
    } finally {
        document.close();
    }
}

public static void getPage(PDDocument document, PDPage page1, String txt,
        int pageno, PDRectangle position) throws IOException {
    annotations = page1.getAnnotations();
    PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
    borderThick.setWidth(INCH / 12); // 12th inch

    PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary();
    borderThin.setWidth(INCH / 72); // 1 point

    PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
    borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
    borderULine.setWidth(INCH / 72); // 1 point

    float pw = page1.getMediaBox().getUpperRightX();
    float ph = page1.getMediaBox().getUpperRightY();
    float textWidth = PDType1Font.TIMES_BOLD.getStringWidth("PDFBox") / 1000 * 18;

    PDAnnotationLink pageLink = new PDAnnotationLink();
    pageLink.setBorderStyle(borderULine);
    textWidth = PDType1Font.TIMES_BOLD.getStringWidth(txt) / 1000 * 18;

    pageLink.setRectangle(position);
    PDActionGoTo actionGoto = new PDActionGoTo();
    PDPageDestination dest = new PDPageFitWidthDestination();
    dest.setPage(document.getPage(pageno));
    actionGoto.setDestination(dest);
    pageLink.setAction(actionGoto);
    annotations.add(pageLink);
}
}

Col是列数的循环,并且复制每列以获取唯一值。根据需要更改1和3(例如,B-F列为2-6)。