如何将所有非零值及其各自的列标题检索到不同的文本框中

时间:2017-10-23 11:45:44

标签: excel excel-vba vba

我已经尝试了以下代码,但是在将下一列及其各自的值循环到下一个文本框/标签时面临问题。 (对于这个问题,请将非零值取为0)

Private Sub CommandButton1_Click()
Dim i As Long, lastrow As Long
Dim ws As Worksheet
Dim fcolumn As Long
Dim lcolumn As Long
Set ws = Worksheets("md")
lastrow = ws.Cells.Find(What:="*", SearchOrder:=xlRows,SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

fcolumn = 9
lcolumn = 50

'defining the rows to be search from
For i = 2 To lastrow
fcolumn = fcolumn + 1
If ws.Cells(i, "A").Value = Val(Me.TextBox1) Then
' Get non zero values from the working row
If Sheets("md").Cells(i, fcolumn).Value <> 0 Then
Me.Label1 = ws.Cells(2, fcolumn)
Me.TextBox1 = Sheets("md").Cells(i, fcolumn).Value

End If
If Sheets("md").Cells(i, fcolumn).Value <> 0 Then
Me.Label2 = ws.Cells(2, fcolumn)
Me.TextBox1 = Sheets("md").Cells(i, fcolumn).Value
End If



Next
End Sub

1 个答案:

答案 0 :(得分:0)

要循环列,请在此行前添加For循环:For i = 2 To lastrow

For循环应该类似于

For fcolumn = 9 to lcolumn

根据建议,请将您的代码缩进以便于阅读。