在Excel vba中复制工作表

k7fdbhmy  于 2023-03-24  发布在  其他
关注(0)|答案(2)|浏览(150)

此VBA代码提示用户输入周数,然后根据该输入显示/隐藏Excel工作表中的列。最后,它将可见单元格复制到名为“Week [week number] Results”的新工作表。
我试图将代码的结果复制到excel中的新工作表,但创建的工作表是空的这是我要复制的工作表enter image description here

Sub ShowColumnsAndCopy()

' Get user input for week number or "all"
Dim weekNumber As Variant
weekNumber = InputBox("Enter the week number (1-49) or type 'all' to show all columns:")

' Check if input is valid
If Not (IsNumeric(weekNumber) And weekNumber >= 1 And weekNumber <= 49) And weekNumber <> "all" Then
    MsgBox "Invalid input"
    Exit Sub
End If

' Show first 9 columns
Range("A7:I7").EntireColumn.Hidden = False

' Show all columns if "all" is entered
If weekNumber = "all" Then
    Range("J7").EntireColumn.Resize(1, Columns.Count - 9).Hidden = False
    Exit Sub
End If

' Show columns with week number
Dim col As Integer
col = 10
While col <= 79
    If Cells(7, col).Value = "RAP Week " & weekNumber _
        Or Cells(7, col).Value = "Inventaire Week " & weekNumber _
        Or Cells(7, col).Value = "EDI Week " & weekNumber Then
        Cells(7, col).EntireColumn.Hidden = False
    Else
        Cells(7, col).EntireColumn.Hidden = True
    End If
    col = col + 1
Wend

' Show remainder columns
Range("CA7").Select
Dim lastColumn As Integer
lastColumn = ActiveCell.End(xlToLeft).Column
If lastColumn > 9 Then
    Range("J7:" & Split(Cells(1, lastColumn).Address, "$")(1)).EntireColumn.Hidden = False
End If

' Copy result to new sheet
Dim newSheet As Worksheet
Set newSheet = Worksheets.Add(After:=ActiveSheet)
newSheet.Name = "Week " & weekNumber & " Results"
Dim rngCopy As Range
Set rngCopy = Range("A7", Cells(Rows.Count, lastColumn).End(xlUp)).SpecialCells(xlCellTypeVisible)
rngCopy.Copy newSheet.Range("A1")

End Sub
wvmv3b1j

wvmv3b1j1#

你能确切地说明你在这个问题中的意思吗?
你只是想在运行这个宏后复制工作表的可见内容到一个新的工作表吗?如果是这样,那么粘贴特殊的值应该可以做到。

ne5o7dgx

ne5o7dgx2#

当你写Range("A7"...)时,你不会告诉VBA你想使用哪个工作表。在这种情况下,VBA将使用ActiveSheet-并且它将更改为新创建的工作表(Workbook.Add将焦点设置在新工作表上)。
你不应该依赖于活动工作表,而是告诉VBA你想使用哪个工作表。基本上有两种方法可以做到这一点:将工作表分配给工作表变量或使用with子句。
下面的代码显示了With-子句。**您需要在代码中每次出现RangeCells**之前添加一个“.”-这将告诉VBA您想要使用With-子句中定义的对象(在本例中为Worksheet):

With ThisWorkbook.Sheets("PDB")  

    ' Show first 9 columns
    .Range("A7:I7").EntireColumn.Hidden = False

    ' Show all columns if "all" is entered
    If weekNumber = "all" Then
        .Range("J7").EntireColumn.Resize(1, Columns.Count - 9).Hidden = False
        Exit Sub
    End If
    
    ' Show columns with week number
    Dim col As Long
    For col = 10 to 79
        If .Cells(7, col).Value = "RAP Week " & weekNumber _
        Or .Cells(7, col).Value = "Inventaire Week " & weekNumber _
        Or .Cells(7, col).Value = "EDI Week " & weekNumber Then
            .Cells(7, col).EntireColumn.Hidden = False
        Else
            .Cells(7, col).EntireColumn.Hidden = True
        End If
    Next col
    
    (...)
    
End With

2备注:(1)养成使用Long而不是Integer的习惯-Integer仅使用2个字节(占用内存4个字节,所以它甚至不保存空间),并且经常导致溢出错误。(2)在您的情况下,For-Loop比While-Loop更好

相关问题