我首先从VBA开始,建议使用PowerBI。但是,我的问题是否真的可以通过powerBI解决。
我有一个定期发送的文件,其中包含需要分组的特定信息。我正在尝试创建一个VBA宏来自动执行文件准备,并使其适合于在其他工具中导入。
我大约有40个列,但有时更少。列的位置有时会更改,因此我无法参考列的位置(A,B,C ...),我必须寻找列的名称。
而且,我必须寻找一些特定的列。如果这些列中有数据,则需要将每种情况的特定元素添加到先前串联的列中。
我在下面提供示例代码
这里是虚拟数据描述:
|客户| ID |信息超人|信息产品类型|规格1 |规格2 |指定一个|规格b |信息客户类型|信息设施类型|地理|
[当“ spec 1”,“ spec 2”,“ Spec a”或“ Spec b”都没有值时,我需要按以下方式对数据进行分组(包括标题):“ Client” +“ Superman” + Bravo +“ Info顾客类型“ +信息设施类型”,即ClientSpermanBravoInfo
[如果有规格1或2的值:“客户端”“超人” AlphaBravo“信息客户类型”信息工具类型“
[如果规格1或2以及规格a和b有值:“客户”“超人” AlphaAlphaBravo“信息客户类型”信息工具类型“。
到目前为止,我具有以下VBA代码,但是使用PowerBI将提供一个更简单,更优雅的解决方案。我一直在寻找接近该资源的资源,例如:https://powerbi.microsoft.com/en-us/blog/power-bi-desktop-april-update-feature-summary/,以便以特定方式查询数据,并根据特定条件创建列,但是我找不到解决方案。
Dim data
data = .Resize(.Rows.Count - 1).Offset(1).Value
End With
Dim result As Dictionary
Set result = New Dictionary
Dim i
For i = 1 To UBound(data, 1)
Select Case True
Case _
data(i, headers("Costumer")) = "" Or _
data(i, headers("Zone ")) = "" Or _
MsgBox "Empty row"
Exit For
Case _
data(i, headers("Spec A")) = "" And _
data(i, headers("Spec B")) = "" And _
data(i, headers("Spec_C ")) = "" And _
data(i, headers("Spec_D ")) = ""
result(result.Count) = _
q & "Costumer" & data(i, headers("Costumer”)) & _
q & "Alpha" & _
q & "Zone" & data(i, headers("Zone")) & _
q
Case _
data(i, headers("Spec_1")) = "" And _
data(i, headers("Spec_2")) = "" And _
data(i, headers("Spec_3")) = "" And _
data(i, headers("Spec_4")) = "" And _
data(i, headers("Spec_5")) = "" And _
data(i, headers("Spec_6")) = "" And _
data(i, headers("Spec_7")) = ""
result(result.Count) = _
q & "Costumer" & data(i, headers("Costumer")) & _
q & "Alphabet" & _
q & "Zone" & data(i, headers("Zone")) & _
q
Case Else
result(result.Count) = _
q & " Costumer" & data(i, headers("Costumer ")) &
q & "AlphabetAlpha" & _
q & " Zone " & data(i, headers("Zone")) & _
q
End Select
Next
' output result data to sheet 2
If result.Count = 0 Then
MsgBox "No result data for output"
Exit Sub
End If
With ThisWorkbook.Sheets(2)
.Cells.Delete
.Cells(1, 1).Resize(result.Count).Value = _
WorksheetFunction.Transpose(result.Items())
End With
MsgBox "Completed"
Sub TralaNome()
Const q = """"
' get source data table from sheet 1
With ThisWorkbook.Sheets(1).Cells(1, 1).CurrentRegion
' check if data exists
If .Rows.Count < 2 Or .Columns.Count < 2 Then
MsgBox "No data table"
Exit Sub
End If
' retrieve headers name and column numbers dictionary
Dim headers As Dictionary
Set headers = New Dictionary
Dim headCell
For Each headCell In .Rows(1).Cells
headers(headCell.Value) = headers.Count + 1
Next
' check mandatory headers
For Each headCell In Array(("Costumer", "ID", "Zone“, "Product Quali", "Spec A", "Spec B", "Spec_C", "Spec_D", "Spec_1", " Spec_2", " Spec_3", " Spec_4", " Spec_5", " Spec_6", " Spec_7", "Chiavetta", "Tipo_di _prodotto", "Unicorno_Cioccolato", “cacao tree“)
If Not headers.Exists(headCell) Then
MsgBox "Header '" & headCell & "' doesn't exists"
Exit Sub
End If
Next
Dim data
' retrieve table data
data = .Resize(.Rows.Count - 1).Offset(1).Value
End With
' process each row in table data
Dim result As Dictionary
Set result = New Dictionary
Dim i
For i = 1 To UBound(data, 1)
MsgBox "Empty row"
Exit For
result(result.Count) = _
q & "ID " & data(i, headers("ID ")) & _
q & " Tipo_di _prodotto " & data(i, headers("Tipo_di _prodotto")) & _
q & " Unicorno_Cioccolato " & data(i, headers("Unicorno_Cioccolato")) & _
q & " cacao tree " & data(i, headers("cacao tree "))&_qEnd Select
Next
' output result data to sheet 2
If result.Count = 0 Then
MsgBox "No result data for output"
Exit Sub
End If
With ThisWorkbook.Sheets(2)
.Cells.Delete
.Cells(1, 1).Resize(result.Count).Value = _
WorksheetFunction.Transpose(result.Items())
End With
MsgBox "Completed"
End Sub
Sub NewColumnNames()
Dim OrignialColumnNames As String
Dim a As Integer
Dim NewColumnNames As Integer
Spec A = Amber A
Spec_B = Amber B
Spec_C = Amber C
Spec_D = Amber D
Spec_1 = Zio_1
Spec_2 = Zio_2
Spec_3 = Zio_3
Spec_4 = Zio_4
Spec_5 = Zio_5
Spec_6 = Zio_6
Spec_7 = Zio_7
Noup_Start = Mip_F
Noup_End = Nip_D
Snouba = Snup_N
SnipChocolat = Choco_F
End Sub
我首先从VBA开始,建议使用PowerBI。但是,我的问题是否真的可以通过powerBI解决。我有一个定期发送的文件,...
您的问题混合了各种列名,因此很难遵循(例如,没有名为“ spec”的列,例如,我们仅查看“ spec 1”,“ spec 2”,“ spec a”和“ spec b”)。