Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在excelvba中退出for循环_Excel_Vba - Fatal编程技术网

如何在excelvba中退出for循环

如何在excelvba中退出for循环,excel,vba,Excel,Vba,我的数据集是这样的 我想做 请看第一排 我的代码是 Private Sub CommandButton1_Click() Dim MyColInstance, i As Long Dim MyWorksheetLastColumn As Byte MyWorksheetLastColumn = Worksheets(1).Cells(1, columns.Count).End(xlToLeft).Column For i = 1 To MyWorksheetLastColu

我的数据集是这样的

我想做

请看第一排

我的代码是

Private Sub CommandButton1_Click()
  Dim MyColInstance, i As Long
  Dim MyWorksheetLastColumn As Byte
  MyWorksheetLastColumn = Worksheets(1).Cells(1, columns.Count).End(xlToLeft).Column

  For i = 1 To MyWorksheetLastColumn
    MyColInstance = ColInstance("Preference", i)
    Cells(1, MyColInstance).Value = "Preference" & i

  Next i

End Sub

Function ColInstance(HeadingString As String, InstanceNum As Long)
  Dim ColNum As Long
  On Error Resume Next
  ColNum = 0

  For X = 1 To InstanceNum
     ColNum = (Range("A1").Offset(0, ColNum).Column) + Application.WorksheetFunction.Match(HeadingString, Range("A1").Offset(0, ColNum + 1).Resize(1, Columns.Count - (ColNum + 1)), 0)
  Next

  ColInstance = ColNum

End Function

问题是,在运行此代码时,它显示了一个错误,因为for循环未完成。我们能做什么?你能这样做吗?在我看来,您只是在第一行的标题中添加了一个后缀

Sub UpdateColumnHeaders()
    Dim headers As Range, header As Range, suffixes As Range, suffix As Range, i As Integer

    Set headers = Range(Cells(1, 1), Cells(1, Range("A1").End(xlToRight).Column))
    Set suffixes = Range("A1:A" & Range("A1").End(xlDown).Row)

    i = 1

    For Each header In headers
        If header = "Preferences" Then
            header = header & suffixes(i)
            i = i + 1
        End If
    Next
End Sub

不。我在“首选项”之间有很多列,我不想发布这些列。请帮助我解决此问题,请参阅更新的代码-仅在标题为“首选项”时添加后缀
Private Sub CommandButton1_Click()

Dim Count1, Count2 As Integer
Dim MyWorksheetLastRow As Byte
Dim MyColInstance, emp_i As Long

For Each Row_Cel In Range("1:1")
If Row_Cel.Value = "Employment" Then
Count1 = Count1 + 1
End If
If Row_Cel.Value = "Job" Then
Count2 = Count2 + 1
End If
Next Row_Cel

For emp_i = 1 To Count1
MyColInstance = ColInstance("Employment", emp_i)
Cells(1, MyColInstance).Value = "Employment" & emp_i
Next emp_i
For emp_i = 1 To Count2
MyColInstance = ColInstance("Job", emp_i)
Cells(1, MyColInstance).Value = "Job" & emp_i
Next emp_i


End Sub

Function ColInstance(HeadingString As String, InstanceNum As Long)
Dim ColNum As Long
On Error Resume Next
ColNum = 0
For X = 1 To InstanceNum
ColNum = (Range("A1").Offset(0, ColNum).Column) + Application.WorksheetFunction.Match(HeadingString, Range("A1").Offset(0, ColNum + 1).Resize(1, Columns.Count - (ColNum + 1)), 0)
Next
ColInstance = ColNum
End Function