0

如何将 Excel 语句的大小减小到 txt 文件并跳过没有数据的空单元格。下面是我的代码的一小部分,但至少有 204 行长并且非常冗余。

Private Sub CommandButton1_Click()
Open (Cells(3, 4)) For Output As #1
   If Len(Cells(5, 6)) <> 0 Then Print #1, Replace(Cells(5, 2), "-", ""); ","; Trim(Cells(5, 6)); ","; Trim(Cells(5, 8))
   If Len(Cells(6, 6)) <> 0 Then Print #1, Replace(Cells(6, 2), "-", ""); ","; Trim(Cells(6, 6)); ","; Trim(Cells(6, 8))
If Len(Cells(7, 6)) <> 0 Then Print #1, Replace(Cells(7, 2), "-", ""); ","; Trim(Cells(7, 6)); ","; Trim(Cells(7, 8))
If Len(Cells(8, 6)) <> 0 Then Print #1, Replace(Cells(8, 2), "-", ""); ","; Trim(Cells(8, 6)); ","; Trim(Cells(8, 8))
If Len(Cells(9, 6)) <> 0 Then Print #1, Replace(Cells(9, 2), "-", ""); ","; Trim(Cells(9, 6)); ","; Trim(Cells(9, 8))
If Len(Cells(10, 6)) <> 0 Then Print #1, Replace(Cells(10, 2), "-", ""); ","; Trim(Cells(10, 6)); ","; Trim(Cells(10, 8))
End Sub
4

2 回答 2

0

只需使用 for 循环将索引 6,7,8... 替换为循环变量。

于 2013-07-20T01:25:52.083 回答
0

您可以/应该调查For..Each循环:

Dim rngGet As Range
Dim rng As Range

Set rngGet = Range(Cells(5, 6), Cells(10, 6))
'or Range("F5:F10")
For Each rng In rngGet
    'do something with rng
Next 'rng

循环通过范围 MSDN

于 2013-07-19T22:23:14.117 回答