AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1801463
Accepted
Dave
Dave
Asked: 2023-07-31 21:44:46 +0800 CST2023-07-31 21:44:46 +0800 CST 2023-07-31 21:44:46 +0800 CST

Excel for Microsoft 365:根据标题行中的列名称更改列宽度

  • 772

我正在使用 Excel for Microsoft 365。

在第一个工作表上,我录制了一个宏来调整列宽和行高。然后我切换到第二个工作表并运行这个宏,并意识到我遇到了下面描述的问题。

每个工作表至少包含一个(可能是多个)列,其标题行(第 1 行)中的列名称包含字符串“foo”。这些列的位置因工作表而异。

我对所有列执行一些步骤。这些步骤效果很好。但是,我想对“foo”列执行一个额外的步骤。具体来说,我想将这些列的宽度更改为 30。由于这些列的位置因工作表而异,因此这并不是那么容易做到的。

下面是说明问题的 VBA 代码:

Sub Macro1()
    ' This part works.
    Application.Goto Reference:="R1C1"
    Cells.Select

    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With

    Columns("A:A").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ColumnWidth = 100

    Cells.Select
    Cells.EntireRow.AutoFit
    Cells.EntireColumn.AutoFit

    ' The problem starts here.
    ' How may I do this for all columns whose column name in the header row contains "foo"?
    ' There are an arbitrary number of such columns and their locations vary from worksheet to worksheet.
    Columns("G:G").Select
    Selection.ColumnWidth = 30
    Columns("L:L").Select
    Selection.ColumnWidth = 30

    ' The rest of this macro works fine.
    Cells.Select
    Cells.EntireRow.AutoFit

    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With

    ActiveWindow.FreezePanes = True
End Sub

在此代码中,我将列 G 和 L 设置为宽度 30。我如何更改此代码以将列名(在标题行中)包含“foo”的任何列的宽度设置为 30,而不是对列进行硬编码?

microsoft-excel
  • 1 1 个回答
  • 21 Views

1 个回答

  • Voted
  1. Best Answer
    Paul
    2023-07-31T22:54:10+08:002023-07-31T22:54:10+08:00

    您可以迭代所有列,并检查顶行是否包含“foo”:

    For Each myColumn In ActiveSheet.Columns
        If InStr(1, LCase(myColumn.Cells(1, 1)), "foo") <> 0 Then
            myColumn.ColumnWidth = 30
        End If
    Next
    
    • 1

相关问题

  • 带有“和”运算符的 Excel 数据透视表

  • 如何对整列使用 Excel 的 LENGTH 函数?

  • Excel 数组(2 个变量)

  • 如何从 WSL 打开 office 文件

  • VBA根据文件名重命名工作表

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve