根据副本的来源,将文本从工作表复制到另一个工作表

时间:2016-12-01 15:46:02

标签: excel vba excel-vba macros

我有一本有很多床单的excel书,目前我有一个宏将两个单元格复制到另一张纸上然后打印,

我现在要做的是取决于副本的来源,然后将它们粘贴到专用的工作表中,

如果我将第一张纸粘贴到纸张11中,请从纸张2复制粘贴到纸张12中,依此类推,

下面的代码用于从我所在的表格复制到一张表格,我现在在顶部添加了if语句以尝试复制到专用表格,但这不起作用并且有错误,

Sub Tag_75()

Application.ScreenUpdating = False

Sheets("lb").Visible = True

Dim lb As Worksheet
Dim sh As Worksheet
Set sh = ActiveSheet

If sh = "Sheet1" Then
    lb = "Sheet11"

ElseIf sh = "Sheet2" Then
    lb = "Sheet12"

ElseIf sh = "Sheet3" Then
    lb = "Sheet13"

ElseIf sh = "Sheet4" Then
    lb = "Sheet14"

End If

ActiveCell.Resize(1, 1).Copy Worksheets("lb").Range("A1")

ActiveCell.Offset(, 1).Resize(1, 1).Copy Worksheets("lb").Range("A2")

Sheets("lb").Select
    Range("A1:A2").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    Selection.Borders(xlEdgeTop).LineStyle = xlNone
    Selection.Borders(xlEdgeBottom).LineStyle = xlNone
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

Worksheets("lb").Range("A2").WrapText = True
Worksheets("lb").Range("A1:A2").Font.Size = 22
Worksheets("lb").Range("A1:A2").ShrinkToFit = True

Dim STDprinter As String

STDprinter = Application.ActivePrinter
'Application.ActivePrinter = "\\SBS2011\Xerox WorkCentre 7830 on Ne04:"
Application.ActivePrinter = "\\JCSP55J\DYMO LabelWriter 450 (Copy 2) on Ne03:"
ActiveSheet.PrintOut
Application.ActivePrinter = STDprinter

Sheets("lb").Visible = False

sh.Activate

End Sub

任何人都可以对if语句有所了解

由于

1 个答案:

答案 0 :(得分:0)

将此更改为If sh = "Sheet1" ThenIf sh.Name = "Sheet1" Then

在您拥有Sheets("lb")的任何地方,将其更改为Sheets(lb)

修改

我将添加更新的代码,因为单独列出它们有太多问题

Sub Tag_75()

Application.ScreenUpdating = False

Dim lb As String
Dim sh As Worksheet
Set sh = ActiveSheet

lb = "Sheet" & CInt(Replace(sh.Name, "Sheet", "")) + 10

Sheets(lb).Visible = True

ActiveCell.Resize(1, 1).Copy Worksheets(lb).Range("A1")

ActiveCell.Offset(, 1).Resize(1, 1).Copy Worksheets(lb).Range("A2")

Sheets(lb).Select
    Range("A1:A2").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    Selection.Borders(xlEdgeTop).LineStyle = xlNone
    Selection.Borders(xlEdgeBottom).LineStyle = xlNone
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

Worksheets(lb).Range("A2").WrapText = True
Worksheets(lb).Range("A1:A2").Font.Size = 22
Worksheets(lb).Range("A1:A2").ShrinkToFit = True

Dim STDprinter As String

STDprinter = Application.ActivePrinter
'Application.ActivePrinter = "\\SBS2011\Xerox WorkCentre 7830 on Ne04:"
Application.ActivePrinter = "\\JCSP55J\DYMO LabelWriter 450 (Copy 2) on Ne03:"
ActiveSheet.PrintOut
Application.ActivePrinter = STDprinter

Sheets(lb).Visible = False

sh.Activate

End Sub