如何使用列号来引用 Excel 列而不是列字母
How to use the column number to reference an Excel column instead of the column letter
以下代码有效:
Imports Microsoft.Office.Interop
Dim xlApp As New Excel.Application
xlApp.Visible = True
Dim wb1 As Excel.Workbook
wb1 = xlApp.Workbooks.Open("C:\Book1.xlsx")
Dim ws1 As Excel.Worksheet
ws1 = CType(wb1.Sheets(1), Excel.Worksheet)
With ws1
CType(.Columns("K:XFD"), Excel.Range).NumberFormat = "General"
End With
我想使用列号而不是列字母。
这是我尝试过的方法,但它不起作用:
With ws1
CType((.Columns(11), .Columns(.Columns.Count)), Excel.Range)).NumberFormat = "General"
End With
一般情况下,您可以使用整数。您应该只访问 Range
属性,传递两个 Cell
引用。
Dim a, b, c, d As Integer
With ws1
.Range(.Cells(a, b), .Cells(c, d)).NumberFormat = "General"
End With
哪里
这是您的具体解决方案
With ws1
Dim a, b, c, d As Integer
a = 1 ' row 1
b = 11 ' column k
c = .Rows.Count ' the last row
d = .Columns.Count ' the last column
.Range(.Cells(a, b), .Cells(c, d)).NumberFormat = "General"
End With
并且由于 Range
接受对象,您可以将整数索引和字符串都传递给 Cells
。这可能对以后有帮助。
With ws1
Dim a, b, c, d As Object
a = 1 ' row 1
b = "k" ' column k
c = .Rows.Count
d = .Columns.Count
.Range(.Cells(a, b), .Cells(c, d)).NumberFormat = "General"
End With
我首先想了解你到底在做什么,所以我使用了这段代码:
With ws1
CType(.Columns("K:XFD"), Excel.Range).Select()
End With
这会突出显示从 K 到 XFD 的所有行。
考虑到这一点。如果您想使用数字而不是字母,请使用以下代码:
With ws1
.Range(.Cells(1, 11), .Cells(.Rows.Count, .Columns.Count)).NumberFormat = "General"
End With
在你可以使用这个之后再次测试它是否正在做你正在做的事情:
With ws1
.Range(.Cells(1, 11), .Cells(.Rows.Count, .Columns.Count)).Select()
End With
以下代码有效:
Imports Microsoft.Office.Interop
Dim xlApp As New Excel.Application
xlApp.Visible = True
Dim wb1 As Excel.Workbook
wb1 = xlApp.Workbooks.Open("C:\Book1.xlsx")
Dim ws1 As Excel.Worksheet
ws1 = CType(wb1.Sheets(1), Excel.Worksheet)
With ws1
CType(.Columns("K:XFD"), Excel.Range).NumberFormat = "General"
End With
我想使用列号而不是列字母。
这是我尝试过的方法,但它不起作用:
With ws1
CType((.Columns(11), .Columns(.Columns.Count)), Excel.Range)).NumberFormat = "General"
End With
一般情况下,您可以使用整数。您应该只访问 Range
属性,传递两个 Cell
引用。
Dim a, b, c, d As Integer
With ws1
.Range(.Cells(a, b), .Cells(c, d)).NumberFormat = "General"
End With
哪里
这是您的具体解决方案
With ws1
Dim a, b, c, d As Integer
a = 1 ' row 1
b = 11 ' column k
c = .Rows.Count ' the last row
d = .Columns.Count ' the last column
.Range(.Cells(a, b), .Cells(c, d)).NumberFormat = "General"
End With
并且由于 Range
接受对象,您可以将整数索引和字符串都传递给 Cells
。这可能对以后有帮助。
With ws1
Dim a, b, c, d As Object
a = 1 ' row 1
b = "k" ' column k
c = .Rows.Count
d = .Columns.Count
.Range(.Cells(a, b), .Cells(c, d)).NumberFormat = "General"
End With
我首先想了解你到底在做什么,所以我使用了这段代码:
With ws1
CType(.Columns("K:XFD"), Excel.Range).Select()
End With
这会突出显示从 K 到 XFD 的所有行。
考虑到这一点。如果您想使用数字而不是字母,请使用以下代码:
With ws1
.Range(.Cells(1, 11), .Cells(.Rows.Count, .Columns.Count)).NumberFormat = "General"
End With
在你可以使用这个之后再次测试它是否正在做你正在做的事情:
With ws1
.Range(.Cells(1, 11), .Cells(.Rows.Count, .Columns.Count)).Select()
End With