简体   繁体   中英

Using VBA in Excel, how do you Hide columns that do not have a header specified in an array?

I have a large spreadsheet and I want to hide the columns that are of less importance. I as able to write the code to delete the unnecessary columns, but decided I would rather just Hide these instead o delete them. Unfortunately, the code is returning a bug. How do I hide columns, using specified header names?

Below is the code I wrote that deletes the columns that do not match the header specified in the array. How can I edit this to hide columns instead of delete them?

 Sub Delete_Rearrange() 'deletes unnecessary columns, then rearranges columns

    Dim I As Long, e As Long, n As Long

    Application.ScreenUpdating = False
    e = Cells(1, Columns.Count).End(xlToLeft).Column
    For I = e To 1 Step -1
    Select Case Cells(1, I).Value
        Case "accrue_mkt_disc_elect", "accrued_oid", "error", "accrued_unrealized_mkt_disc", "acq_dt", "acq_prem", "amort_prem_elect", "bond_prem", "Currency_code", "END_DT", "Error", "exc_rte", "fmv_as_of_dt_of_gf", "gf_dt", "gf_or_inh_in", "include_mkt_disc_inc_elect", "isn_sec_iss_id", "iso_crr_cd", "last_adj_dt", "mkt_disc", "rc_con_in", "rv_fm_cus_at_nm", "sb_fm_nm", "spot_rte_elect", "tl_cur_cs", "tl_og_cs", "tl_qty", "trf_initial_dt", "trf_sett_dt", "Error"
             On Error Resume Next
            n = WorksheetFunction.Match(Cells(1, I), Range(Cells(1, I - 1), Cells(1, 1)), False)
            If Err = 0 Then Columns(I).Delete
            On Error GoTo 0
        Case Else
            Columns(I).Delete
    End Select
Next I
Application.ScreenUpdating = True

End Sub

If I change Delete to Hidden in the above code I get the error

"Hidden Method of Range Class Failed"

, referencing to the line Case Else Columns (I).Hidden near the end of the code.

正确的用法是Columns(I).Hidden = True

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