>im 尝试从 excel 文件生成表和列到 txt 文件的列表。我的代码只是为所有 clumn 生成直到行尾,因为我不知道如何定义变量来限制循环每个表:
这里的代码和Excel数据图像Excel数据
Dim field As String
Dim table As String
Dim t As Integer
Dim f As Integer
Dim lastrow As Integer
Dim myFile As String
Dim xStr As String
myFile = "D:table.txt"
With Sheets("Sheet1")
Open myFile For Output As #1
table = """table_name"": """ & .Range("I2") & ""","
Print #1, table
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For f = 2 To lastrow
field = """column_name"": [" & Chr(10) & """" & .Range("B" & f) & """" & Chr(10) & _
"],"
Print #1, field
Next f
Close #1
End With
我想象下面的代码(CMIIW 逻辑(,但是如何在 vba 上编写它:
for t = 1 to "count table on sheet (i dnt know how to define this value)"
table = "tablename"
Print #1, table
for f = "first row table t + 1" to lastrow table t
field= "fieldname"
Print #1, field
Next f
Next t
根据user11246173的回答,我知道如何循环为表计数的数量,但是我仍然无法像上面的代码那样获取每个特定单元格的范围。请帮助解决我的代码。
任何人都可以帮我吗?
在 VBA 中,工作表表称为 ListObjects。它们是属于父工作表的集合,并且(除其他属性外(与大多数其他集合对象一样具有计数和索引。每个列表对象都有许多列表列。
Option Explicit
Sub loopLO()
Dim i As Long, c As Long
With Worksheets("sheet1")
For i = 1 To .ListObjects.Count
Debug.Print .ListObjects(i).Name
For c = 1 To .ListObjects(i).ListColumns.Count
Debug.Print .ListObjects(i).ListColumns(c)
Next c
Next i
End With
End Sub