简体   繁体   English

VBA-跨可见列复制粘贴

[英]VBA - Copy pasting across visible columns

I have a code that imports a table and then goes on to hide columns that are not relevant to the end user. 我有一个导入表的代码,然后继续隐藏与最终用户无关的列。

As part of the formatting process, I need to add a header to the excel doc that consists of merged and unmerged cells. 作为格式化过程的一部分,我需要向包含合并和未合并单元格的excel文档中添加标头。

My problem is that considering the fact that a number of columns have been hidden, it is proving impossible to import the header. 我的问题是,考虑到隐藏了许多列这一事实,事实证明不可能导入标题。 The header would need to be pasted across numerous non-sequential columns. 标头将需要粘贴到许多非连续列中。 Is there a way to do that? 有没有办法做到这一点?

For reference I have copy pasted the code I use to hide the columns in the first place. 作为参考,我已经复制粘贴了用于隐藏列的代码。 I am guessing I will need to find a way to delete the columns instead. 我猜我将需要找到一种删除列的方法。 The problem with that is that if I do Columns(I).delete, the loop ends after deleting the first column. 这样做的问题是,如果我执行Columns(I).delete,则循环将在删除第一列后结束。

Note: The counta in the code yields 17 columns. 注意:代码中的counta产生17列。 I have changed some of the naming and number of columns I am checking in the if condition in the interest of confidentiality. 为了保密起见,我更改了if条件中要检查的某些名称和列数。 If there is any other info you need please let me know 如果您需要其他任何信息,请告诉我

Edit: Upon further investigation and YowE3K's comment the problem in the code is that it is skipping the processing of a given column after one is deleted (because it is now in column i which the code thinks it has already processed) 编辑:经过进一步的调查和YowE3K的评论,代码中的问题是删除一个给定列后将跳过该列的处理(因为它现在位于第i列中,代码认为它已经处理过)

Sub NewView()
ActiveWindow.DisplayHeadings = False
Application.ScreenUpdating = False
ActiveSheet.Range("A14:Z14").Copy
ActiveWorkbook.Sheets.Add Before:=Worksheets(Worksheets.Count)
ActiveWorkbook.ActiveSheet.Name = "Temp_View"
ActiveWorkbook.Sheets("Temp_View").Activate
Worksheets("Temp_View").Range("B1").Formula = "=COUNTA($8:$8)"
ActiveSheet.Range("B8").PasteSpecial _
Paste:=xlPasteValues
Dim countUsedCols As Long
countUsedCols = ActiveWorkbook.Sheets("Temp View").Range("B1").Value
Dim currentColName As String
Dim newColName As String

For i = 2 To countUsedCols + 1


    currentColName = ActiveSheet.Cells(8, i).Value

    If currentColName <> "Salary" and currentColName<>"Net Worth" Then
        ActiveSheet.Columns(i).Select
        Selection.EntireColumn.Hidden = True
        'Selection.EntireColumn.Delete

the best way loop over columns or rows (if you are going to delete some of them) is going backwards: 最好的循环遍历列或行(如果要删除其中的一些)的方法是倒退:

For i = countUsedCols To 2 Step -1

currentColName = ActiveSheet.Cells(8, i).Value

If currentColName <> "Salary" And currentColName <> "Net Worth" Then
    ActiveSheet.Columns(i).Delete
End If

That way you dont have to mess with you variables or to worry about the effexcts of the deleted cells. 这样,您不必弄乱变量,也不必担心删除的单元格的效果。 In Addition: you need'nt select the column before deleting. 在“添加”中:删除前无需选择列。

So, the answer to the issue ended up as follows: 因此,问题的答案最终如下:

For i = 2 To countUsedCols

currentColName = ActiveSheet.Cells(8, i).Value

If currentColName <> "Salary" and currentColName<>"Net Worth" Then
    ActiveSheet.Columns(i).Select
    'Selection.EntireColumn.Hidden = True
    Selection.EntireColumn.Delete
    i = i - 1
    If currentColName = "" Then
       Exit for
    End if
....
End if

It was oddly simple 这很简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM