Excel没有数据读取行,然后跳过这一行

我有我的代码问题。 我需要它来读取成本中心和物料编号,然后在SAP中查看。 它是完美的。 问题在于用户应该能够手动input材料和价格,而不需要成本中心和材料编号。 程序应该看到这一行,然后转到下一行。 现在,当遇到空行时,它停止从SAP加载信息。 我知道为什么这样做,但我似乎无法弄清楚如何纠正它。 在下面的代码中,您可以看到代码。 问题是“While”声明。

Sub StartExtract() Dim currentline As Integer ' This is the system to connect to W_System = "PE1400" ' We start looking for order numbers from line 6 in the sheet currentline = 6 While Cells(currentline, 1).Value <> "" ' Run the actual GUI script RunGUIScript currentline ' move to the next line currentline = currentline + 1 Wend End Sub 

尝试这个。

如果您有任何问题,请告诉我。

 Option Explicit Sub StartExtract() 'You should really reference the Workbook and Sheet..ALWAYS Dim DataWS As Worksheet 'Please insert the sheet name in which the data lies Set DataWS = ThisWorkbook.Sheets("Sheet1") Dim LastRow As Long With DataWS LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With ' This is the system to connect to W_System = "PE1400" ' We start looking for order numbers from line 6 in the sheet 'Always want to use Long when referencing Columns or Rows Dim currentline As Long For currentline = 6 To LastRow If DataWS.Cells(currentline, 1).Value <> "" Then ' Run the actual GUI script RunGUIScript currentline End If Next currentline End Sub 

谢谢@Zerk! 我试图重写我的代码,以符合你的build议,这最终工作:

 Sub StartExtract() Dim currentline As Integer Dim i As Long ' This is the system to connect to W_System = "PE1400" ' We start looking for order numbers from line 6 in the sheet currentline = 6 For i = 6 To 300 If Cells(currentline, 1).Value <> "" Then ' Run the actual GUI script RunGUIScript currentline ' move to the next line currentline = currentline + 1 Else currentline = currentline + 1 End If Next i End Sub 

你可以用a代替上一行填满的行:

 LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row for currentline = 6 to LastRow 'if Cells(currentline,1).value<>"" then RunGUIScript currentline 'end if 'the for will automatically increment currentline Next