Find Column Header By Name And Select All Data Below Column Header (Excel-VBA)

前端 未结 5 1520
予麋鹿
予麋鹿 2020-12-06 07:42

This is my first post...

I\'m attempting to create a macro to do the following:

  1. Search a spreadsheet column header by name.
  2. Select all data fr
5条回答
  •  [愿得一人]
    2020-12-06 08:30

    Add a dim for the range that you want:

    Dim MyRng, RngStart, RngEnd as Range
    

    Then change:

    ActiveSheet.Range(c.Address).Offset(1, 0).Select
    

    to the below so that all data in that column is found.

    set RngStart = ActiveSheet.Cells(1, c.column)
    set RngEnd = ActiveSheet.Cells(rows.count, c.column).end(xlup)
    set MyRng = ActiveSheet.Range(RngStart & ":" & RngEnd)
    

    Now you can play about with the data. If you want to paste this somewhere which is formatted as number:

    MyRng.copy
    Sheets("Wherever").Range("Wherever").pastespecial xlvalues
    

    If you want to change the format of the cells you have now found (How to format column to number format in Excel sheet?) that is whole number format, if you want decimal points then use "number" instead of "0":

    MyRng.NumberFormat = "0"
    

    or the new destination:

    Sheets("Wherever").Range("Wherever").NumberFormat = "0"
    

    General formatting which matches exactly the convert to number function:

    MyRng.NumberFormat = "General"
    MyRng.Value = MyRng.Value
    

提交回复
热议问题