从另一个文件中检索行值

时间:2014-07-08 18:43:08

标签: excel-vba vba excel

我一直在这个项目中工作。我无法理解为什么它不会从" SheetName"中复制行字符串值。用作参数传递给此函数(SheetName)。该函数可以读取文件并使用基于第一个文件中找到的列标题数的复选框创建第二个文件,但列标题不会作为复选框的标题复制到第二个文件中。任何帮助表示赞赏。

 Function CallFunction(SheetName As Variant) As Long

 Dim text As String
 Dim titles(200) As String ' Dim titles(200) As String ' Array
 Dim nTitles As Integer
 Dim wks As Worksheet
 Dim myCaption As String
 Dim NewBook As Workbook

 PathName = Range("F22").Value
 Filename = Range("F23").Value

 ControlFile = ActiveWorkbook.Name
 Workbooks.Open Filename:=PathName & "\" & Filename

 Set wks = ActiveWorkbook.Worksheets(SheetName)

 For i = 1 To 199
     If Trim(wks.Cells(4, i).Value) = "" Then
        nTitles = i - 1
        Exit For
    End If
        titles(i - 1) = wks.Cells(4, i).Value
 Next

 i = 1

 Workbooks.Add
 Set NewBook = ActiveWorkbook
 NewBook.SaveAs fileExported
 Workbooks.Open (fileExported)

 For Each cell In Range(Sheets(SheetName).Cells(4, 1), Sheets(SheetName).Cells(4, 1 + nTitles))

    myCaption = Sheets(SheetName).Cells(4, i).Value

    With Sheets(SheetName).checkBoxes.Add(cell.Left, _
        cell.Top, cell.Width, cell.Height)
        .Interior.ColorIndex = 12
        .Caption = myCaption
        .Characters.text = myCaption
        .Border.Weight = xlThin
        .Name = myCaption
    End With

    i = i + 1
 Next
 End Function

1 个答案:

答案 0 :(得分:0)

我找到了自己问题的答案,我忘了在这里添加答案。好的,这是

 ' Save all Jira column titles into jTitles
If sj = True Or ji = True Then
    For j = 1 To 199
         If Trim(wks1.Cells(4, j).Value) = "" Then
            titlesj = j - 1
            Exit For
        End If
            jTitles(j - 1) = wks1.Cells(4, j).Value
     Next

     j = 1

     ' Add column titles as checkboxes
     For j = 0 To titlesj
        Sheet1.ListBox1.AddItem jTitles(j)
        Sheet1.ListBox3.AddItem jTitles(j)
     Next
     wb1.Close
End If