抱歉,如果標題令人困惑 - 不知道如何最好地描述它
我有 400 cols x 2000 行的資料,格式如下:
| 姓名 | 籃子 1 | 籃子 2 | 籃子 3 |
|---|---|---|---|
| 蘋果 | 30% | 40% | 45% |
| 香蕉 | 20% | 55% | |
| 橘子 | 50% | 60% |
在另一個選項卡上,我想這樣如果我將香蕉放入 A2 的單元格中,那么 B2 將填充籃子 1 (20%)、籃子 3 (55%)。
如果每行的單元格不是空白的,我之前使用 if & isblank 陳述句來顯示列名,但這對于 400 多列來說太手動了。解決這個問題的最佳方法是什么?任何幫助,將不勝感激。謝謝!
uj5u.com熱心網友回復:
獲取分隔資料 (UDF):標題和行
在 Excel 中的單元格中
B2,使用以下公式:=FruitByBasket(A2)將以下代碼復制到標準模塊,例如
Module1.調整常量部分中的值。
Option Explicit
Function FruitsByBasket( _
ByVal Fruit As String) _
As String
Application.Volatile
Const wsName As String = "Sheet1"
Const FruitColumn As String = "A"
Const Delimiter As String = ", "
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim crg As Range: Set crg = ws.Columns(FruitColumn)
Dim FruitRow As Variant: FruitRow = Application.Match(Fruit, crg, 0)
If IsError(FruitRow) Then Exit Function
Dim LastColumn As Long
LastColumn = ws.Cells(FruitRow, ws.Columns.Count).End(xlToLeft).Column
If LastColumn = 1 Then Exit Function
Dim rrg As Range
Set rrg = ws.Rows(FruitRow).Resize(, LastColumn - 1).Offset(, 1)
Dim cCount As Long: cCount = rrg.Columns.Count
Dim rData As Variant
Dim hData As Variant
If cCount = 1 Then
ReDim rData(1 To 1, 1 To 1): rData(1, 1) = rrg.Value
ReDim hData(1 To 1, 1 To 1)
hData(1, 1) = rrg.EntireColumn.Rows(1).Value
Else
rData = rrg.Value
hData = rrg.EntireColumn.Rows(1).Value
End If
Dim dLen As Long: dLen = Len(Delimiter)
Dim c As Long
For c = 1 To cCount
If IsNumeric(rData(1, c)) Then
If Len(rData(1, c)) > 0 Then
FruitsByBasket = FruitsByBasket & hData(1, c) & " (" _
& Format(rData(1, c), "#%") & ")" & Delimiter
End If
End If
Next c
If Len(FruitsByBasket) > 0 Then
FruitsByBasket = Left(FruitsByBasket, Len(FruitsByBasket) - dLen)
End If
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435209.html
上一篇:索引和匹配矩陣公式
下一篇:使用兩個比較器時阻塞遞回呼叫
