Excel-通过跳过空单元格组装文件夹路径

时间:2018-09-26 14:14:07

标签: excel

要处理其他应用程序,我需要准备文件夹路径。

期望的结果是绿色列。应该有一个公式,其操作类似于“向右迈进-向上直到找到一个值”,然后将其与黄色单元格中的值放在一起+在下一列中进行相同的操作。

简而言之:a)黄色是Achor b)橙色B / C / D列是用户的输入项c)绿色是理想的结果。

enter image description here

2 个答案:

答案 0 :(得分:0)

我必须使用我需要表示为平面文件的xml来执行一次。如果将这些值向下复制以填充空白单元格,则可能只有一列,将每一列连接到左侧。但这不是很程序化,而且劳动强度大。

答案 1 :(得分:0)

这里有一些VBA可以执行我上面解释的操作,但是会自动执行。可能有些笨拙,但可以在我的测试中使用。填充空白要比上下左右编程所有if / then逻辑容易。

Turns this Into this

Sub pathMaker()

Dim r As Integer
Dim c As Integer
Dim lrow As Integer
Dim lcol As Integer
Dim firstrow As Integer
Dim headers As String
Dim resultcol As Integer


lcol = ActiveSheet.UsedRange.Columns.Count
lrow = ActiveSheet.UsedRange.Rows.Count
resultcol = lcol + 1
headers = MsgBox("Does your data contain a header row?", 
vbQuestion + vbYesNo, "Headers")

' Determines whether to make the first or second row a 
'filepath
If headers = vbYes Then
    firstrow = 2
    'lrow = lrow - 1
    Else
    firstrow = 1
End If

'Goes through each row a column at a time and copies the 
'filepath element down (which results 
'in an extra row at the end, but it isn't included in the 
'list of filepaths later so just ignore)   

For c = 1 To lcol
   Select Case c
   Case 1
    For r = firstrow To lrow
        If IsEmpty(Cells(r, c).Offset(1, 0)) = True Then
            Cells(r, c).Offset(1, 0) = Cells(r, c)
        End If
        Next r
   Case Is > 1
    For r = firstrow To lrow
        If IsEmpty(Cells(r, c).Offset(1, 0)) = True Then
            If Cells(r, c).Offset(1, -1) = Cells(r, 
c).Offset(0, -1) Then
                Cells(r, c).Offset(1, 0) = Cells(r, c)
            End If
        End If
        Next r
   End Select
   Next c
'Concatenates populated cells into filepaths in the last 
'column plus one   
For ir = firstrow To lrow
    For ic = 1 To lcol
        If IsEmpty(Cells(ir, ic)) = False Then
        Cells(ir, resultcol) = Cells(ir, resultcol) & 
Cells(ir, ic) & "\"
        End If
    Next ic
Next ir

End Sub

希望有帮助!祝你好运。