im试图从Excel文件生成表和列的列表到txt文件。 我的代码只为所有簇生成直到行尾,因为我不知道如何定义变量来限制循环每个表:
此处的代码和excel数据为image Excel data
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的回答,我知道如何循环作为表数, 但是我仍然无法像上面的代码那样获得每个特定单元格的范围。 请帮助解决我的代码。
有人可以帮我吗?
答案 0 :(得分:0)
在VBA中,工作表表称为ListObjects。它们是一个属于父工作表的集合,并且(除其他属性外)与大多数其他集合对象一样具有一个计数和一个索引。每个ListObject都有许多ListColumns。
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