简体   繁体   中英

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.

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.

Note: The counta in the code yields 17 columns. I have changed some of the naming and number of columns I am checking in the if condition in the interest of confidentiality. 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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