使用循环Excel VBA

时间:2017-03-13 11:20:37

标签: excel vba excel-vba

我下面有两张图片,有助于描述我需要做的事情:

  • 我有多张表,每张都有不同的名字
  • 在每张表格中,有许多类别构成矩阵
  • 我想将此矩阵中的特定值返回到'平面文件'版本

Picture of dummy data

Flat file headers

在上面的图像中,您可以看到带有一些虚拟数据的矩阵以及我想要返回的数据的标题:流,功能,区域,公司,产品,原始应用程序和目标应用程序。

到目前为止,我的逻辑是创建一些垂直和水平运行的循环,使用一些if / else语句指定在特定情况下返回的内容。

例如,如果公司名称为HB,则将空白返回原始应用程序'并填写目标应用程序'使用单元格值 - 但如果公司名称不是HB,则返回原始应用程序'和'目标应用程序'。

我试图去除我到目前为止所写的大部分内容,现在当我运行它时,它只是说“跑步”。但实际上并没有将任何内容返回到我的目标表中,我不知道问题出在哪里或我需要修复什么。

很高兴提供更多说明,因为我意识到这是一个非常冗长的描述。

到目前为止,我的代码如下。任何提示或信息在提高效率和帮助解决我的问题方面都很受欢迎,因为我是VBA的新手。

Sub sheetNames()

     Dim rowNumberInMappingSheet As Integer
     rowNumberInMappingSheet = 2

     Call populateMappingSheet

End Sub

Function populateMappingSheet()
     Dim stream As String
     Dim functionalValue As String
     Dim orgApplication As String
     Dim regionValue As String
     Dim companyName As String
     Dim productValue As String
     Dim targetApp As String
     Dim rowNumber As Integer

For i = 5 To Sheets.Count
    rowNumber = i - 3
    stream = Sheets(i).Name

    For j = 7 To Rows.Count
        If (Cells(j, "C").Value = 0) Then
            j = j + 1

        ElseIf (Cells(j, "C").Value = 1) Then
            j = Rows.Count

        Else:
            functionalValue = Cells(j, "C").Text
            orgApplication = ""

            For k = 5 To Columns.Count
                If (Cells(1, k).Value = 0) Then
                    k = k + 1
                ElseIf (Cells(1, k).Value = 1) Then
                    Exit For
                Else:
                    regionValue = Cells(1, k).Text
                    companyName = Cells(2, k).Text
                    productValue = Cells(4, k).Text

                    If (companyName = "HB") Then
                        orgApplication = ""
                        targetApp = Cells(j, k).Text
                        Call WriteRowInCombinedMappingSheet(stream, functionalValue, regionValue, companyName, productValue, orgApplication, targetApp)
                    ElseIf (Cells(5, k).Text = "Current TC application") Then
                        orgApplication = Cells(j, k).Text
                    ElseIf (Cells(5, k).Text = "Target Application") Then
                        targetApp = Cells(j, k).Text
                        Call WriteRowInCombinedMappingSheet(stream, functionalValue, regionValue, companyName, productValue, orgApplication, targetApp)

                    End If
                End If
             Next k
             End If
        Next j
    Next i
End Function

'new function to write

Function WriteRowInCombinedMappingSheet(stream, functionalValue, regionValue, companyName, productValue, orgApplication, targetApp)

     rowNumberInCombinedMappingSheet = rowNumberInCombinedMappingSheet + 1

'writing to a cell in a different sheet

        Worksheets("combinedmapping").Activate

            Cells(rowNumberInCombinedMappingSheet, "A") = stream
            Cells(rowNumberInCombinedMappingSheet, "B") = functionalValue
            Cells(rowNumberInCombinedMappingSheet, "C") = regionValue
            Cells(rowNumberInCombinedMappingSheet, "D") = companyName
            Cells(rowNumberInCombinedMappingSheet, "E") = productValue
            Cells(rowNumberInCombinedMappingSheet, "F") = orgApplication
            Cells(rowNumberInCombinedMappingSheet, "G") = targetApp
 End Function

 Sub activateSheet(sheetname As String)
   'activate the combined mapping sheet
       Worksheets(combinedmapping).Activate
 End Sub

0 个答案:

没有答案