使用Excel VBA从多张图纸返回多个值
我在下面提供了两张图片,这将有助于描述我需要做什么:使用Excel VBA从多张图纸返回多个值,vba,excel,Vba,Excel,我在下面提供了两张图片,这将有助于描述我需要做什么: 我有多张纸,每张都有不同的名字 在每张表格中,有许多类别构成一个矩阵 我想将此矩阵中的特定值返回到“平面文件”版本 在上面的图像中,您可以看到包含一些虚拟数据的矩阵,以及我想要返回的数据的标题:流、函数、区域、公司、产品、原始应用程序和目标应用程序 到目前为止,我的逻辑是创建一些垂直和水平运行的循环,使用一些if/else语句指定在特定情况下返回的内容 例如,如果公司名称为HB,则将“原始应用程序”返回为空,并用单元格值填充“目标应用
- 我有多张纸,每张都有不同的名字
- 在每张表格中,有许多类别构成一个矩阵
- 我想将此矩阵中的特定值返回到“平面文件”版本
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
您有许多不合格的引用,例如
单元格
,而不是工作表(sheetname).Cells
。把这些清理干净就好了。此外,在“WriteRowInCombinedMappingSheet”中,您可以尝试写入cell.value,而不仅仅是cell。这是否就是我收到运行时错误13的原因:如果(Cells(1,Columns(k)).value=0,则,则上的类型不匹配?很抱歉我的无知,但是清理它们是否意味着简单地添加工作表(sheetname)。
作为我的每个单元格的预修复
引用?您可以将与工作表(sheetname)
一起使用,然后从开始每个单元格(x,y)
。另外,当您使用单元格(j,Columns(k))
时,您可以只使用单元格(j,k)
谢谢Jordan和Winterkenell。代码现在似乎正在运行,但我面临以下两个问题:1)当我在另一个选项卡上时,代码只会写入我的目标页,例如“IT”。2) 代码不循环。它在我的目标工作表上写第一行,然后停止。有什么想法吗?