excel - 使用列字母的字符串变量设置范围

标签 excel vba range

我想知道如何编写这个范围,将“G”更改为字符串变量 strColumn .
这是我要更改的代码:

   Dim lastRowElemento As Integer
   lastRowElemento = Cells(Rows.Count, "G").End(xlUp).Row
   Set rngElemento = ws.Range("G2:G" & lastRowElemento)

最佳答案

应用OP的方法,试试这个:

Sub TEST()
Dim ws As Worksheet, Rng As Range, sCol As String
    sCol = "G"
    Set ws = ThisWorkbook.Sheets("TEST")    'change as required
    With ws.Columns(sCol)
        Set Rng = Range(.Cells(2), .Cells(.Rows.Count).End(xlUp))
    End With
    End Sub

关于excel - 使用列字母的字符串变量设置范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69746215/

相关文章:

c# - 在 C# 中设置 Excel 图表轴标题颜色和大小

vba - 使用excel VBA清除除第一行以外的整行数据

excel - 如何在Excel中将表格转换为3列

ruby - 从结束值创建一系列字符串

python - 如何根据阈值扩展整数列表?

c# - 如何在 C# 中读取打开的 excel 文件

c# - Excel 自动化 : How do I set the range?

javascript - 在 Javascript 中按范围设置选择

excel - 如何使用命名范围/数组自动过滤表?

c# - 如何使用 ExcelDna 中的 ExcelCommand