VBA根据第三个

时间:2015-04-22 11:22:19

标签: excel vba excel-vba variables string-concatenation

我目前正在尝试根据第三列中存在的另一组数据来合并来自两个不同列的两组数据。

在这种情况下,我需要将G列(三个字母代码的字符串)的基本国家标签连接到H列中的字符串前面,提供字符串" Missing Audio"或"缺少音频/辅助"列F

我当前的固定变量是所有数据都将从各个列的第六行开始,但它们将运行到一个未确定的点。

我已经获得了各种尝试的代码,我很乐意发布,但它们似乎都没有效果,我开始觉得我已经过了复杂的问题在我脑海中的过程和它创建一个编码器块的形式!任何提供的帮助都将受到大力赞赏

正如我在这里所要求的那样,尝试使用变量要求提取基本的Concatenate脚本:

Sub Notestidy()

Dim w1 As Worksheet
Dim c As Range, AC As String
Dim rng As Range, rng2 As Range, iRow As Integer, iCol As Integer, i As Integer

Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
Set rng = w1.Range("G6", w1.Range("G" & Rows.Count).End(X1up))
Set rng2 = w1.Range("H6", w1.Range("H" & Rows.Count).End(X1up))

For Each c In w1.Range("F6", w1.Range("F" & Rows.Count).End(xlUp))
  AC = .String("Missing Audio", "Missing Audio/Subs")
  On Error Resume Next
  If c = Range Then
    Dim varConctnt As Variant
    For iRow = 1 To rng.Rows.Count: rng2.Rows.Count
    For iCol = 1 To rng.Columns.Count: rng2.Columns.Count
    If Not rng(iRow, iCol).Value = vbNullString Then
      varConctnt = varConctnt & ", " & rng(iRow, iCol).Value
    End If
Next iCol
If varConctnt = vbNullString Then MsgBox "Empty Array": GoTo skip1
MsgBox Mid(varConctnt, 2)
varConctnt = ""

skip1:
Next iRow

Application.ScreenUpdating = True

End Sub

新代码尝试:

> Sub Notestidy()
> 
> Dim w1 As Worksheet Dim rng As Range Dim FirstRow As Integer
> 
> 
> 
> Set w1 = Worksheets("Sheet1") FirstRow = 0
> 
> 
> Set rng = w1.Range("F:F").Find(What:="Missing Audio",
> LookIn:=xlValues, _   LookAt:=xlPart, SearchDirection:=xlNext,
> MatchCase:=False) While Not rng Is Nothing   If FirstRow <> rng.Row
> Then
>     If FirstRow = 0 Then
>       FirstRow = rng.Row
>     End If
>     w1.Range("H" & rng.Row) = w1.Range("G" & rng.Row) & w1.Range("H" & rng.Row)
>     rng.FindNext After:=rng   Else
>     Set rng = Nothing   End If Wend
> 
> Application.ScreenUpdating = True
> 
> End Sub

1 个答案:

答案 0 :(得分:0)

如果我正确地解释了你的问题,我想你可能过于复杂了!

Sub Notestidy()    

    Dim cc, sc, nc As String
    Dim ws As Worksheet

    Set ws = Sheet1

    cc = "G"
    sc = "H"
    nc = "F"

    Dim i, max As Integer
    max = ws.UsedRange.Rows.Count

    For i = 2 To max

        If ws.Range(nc & i).Value = "Missing Audio" Or ws.Range(nc & i).Value = "Missing Audio/Subs" Then

            ws.Range(sc & i).Value = ws.Range(cc & i).Value & " " & ws.Range(sc & i).Value

        End If

    Next i

    Set ws = Nothing
End Sub

编辑:或者根据FreeMan使用Find方法的答案:

Sub Notestidy()

    Dim w1 As Worksheet
    Dim result As Range
    Dim FirstRow As Integer

    Set w1 = Worksheets("Sheet1")
    FirstRow = 0

    With w1.Range("F:F")
        Set result = .Find(What:="Missing Audio", LookIn:=xlValues, LookAt:=xlPart)

        If Not result Is Nothing Then
            FirstRow = result.Row
            Do
                w1.Range("H" & result.Row) = w1.Range("G" & result.Row) & w1.Range("H" & result.Row)
                Set result = .FindNext(result)
            Loop While Not result Is Nothing And result.Row <> FirstRow
        End If
    End With

    Set w1 = Nothing
    Set result = Nothing

End Sub