vba代码在执行后在列中显示空白

时间:2018-07-28 15:00:29

标签: excel vba excel-vba

两个名为“ Sheet1”和“ Sheet2”的工作表。在工作表“ Sheet1”的“ D”列中,有唯一的数值(动态行)。在工作表“ Sheet2”的“ A”列中,存在一些数值不是唯一的(具有许多重复值)(动态行)。

我的问题是找到“ D”列的工作表“ Sheet1”的数值与“ A”列的工作表“ Sheet2”的数值匹配(从最后一行开始搜索),并且当第一个数值匹配时,它应该停止搜索重复项并将同一行的工作表“ Sheet1”列“ A”的单元格值复制到工作表“ Sheet2”列“ C”。 该过程应继续到工作表“ Sheet1”的动态列“ D”的所有单元格值。我认为我有道理。

我知道在这个论坛和其他论坛上都有很多类似的问题,但是我无法修改可用的代码来解决我的问题。如果有人可以帮助我解决问题。谢谢您的提前帮助。

Sub offset()

Dim w1 As Worksheet, w2 As Worksheet
Dim c As Range, FR As Long

Application.ScreenUpdating = False

Set w1=Worksheets("Sheet1")
Set w2 =Worksheets("Sheet2")

For Each c In w1.Range("D2", w1.Range("D" & Rows.Count).End(xlUp))
    FR = 0
    On Error Resume Next
    FR = Application.Match(c, w2.Columns("A"), xlPrevious)
    On Error GoTo 0
    If FR <> 0 Then w2.Range("C" & FR).Value = c.Offset(, -3)
Next c

Application.ScreenUpdating = True

End Sub

图片不正确: incorrect image

正确的图像: correct image

3 个答案:

答案 0 :(得分:1)

如果使用的是Application.Match,请将返回的值传递回一个变量。与Long不同,可以使用IsError进行测试。

通常,将您的子对象命名为与该子对象中使用的本机VBA函数相同的做法是“错误的做法”。

xlPrevious是Range.Find的参数,不是Application.Match的参数。

Sub myoffset()

    Dim w1 As Worksheet, w2 As Worksheet
    Dim c As Range, FR As variant

    Application.ScreenUpdating = False

    Set w1=Worksheets("Sheet1")
    Set w2 =Worksheets("Sheet2")

    with ws1
        For Each c In .Range(.cells(2, "D"), .cells(.Rows.Count, "D").End(xlUp))
            FR = Application.Match(c, w2.Columns("A"), 0)
            If not iserror(FR) Then _
              w2.cells(fr, "C") = c.Offset(0, -3).value
        Next c
    end with

    Application.ScreenUpdating = True

End Sub

答案 1 :(得分:0)

我已经对此进行了测试,但是该子程序有效,但是我不确定这是否正是您所追求的。

Sub test()
    Dim i As Long
    Dim j As Long
    Dim w1 As Worksheet
    Dim w2 As Worksheet
    Dim size1 As Long
    Dim size2 As Long

    'sets references to the respective worksheets
    Set w1 = Excel.Application.ThisWorkbook.Worksheets("Sheet1")
    Set w2 = Excel.Application.ThisWorkbook.Worksheets("Sheet2")

    'gets the absolute last row with data in it for w1 // ignores empty cells
    size1 = w1.UsedRange.Rows.Count

    'same as previous line but for w2
    size2 = w2.UsedRange.Rows.Count

    'iterate through the height w1 column D from bottom to top
    For i = size1 To 2 Step -1

        'iterate through the height of w2 column A from bottom to top
        For j = size2 To 2 Step -1

            'tests if the cells are empty so you don't copy blank cells if there are
            'gaps in your data
            If Not IsEmpty(w2.Cells(i, 1).Value) And Not IsEmpty(w1.Cells(i, 4).Value) Then

                'if w2 column A value = w1 column D value
                If w2.Cells(i, 1).Value = w1.Cells(i, 4).Value Then

                    'assign the value in w1 column A to w2 column C
                    w2.Cells(i, 3).Value = w1.Cells(i, 1).Value

                    'exits the nested for loop and returns you to the outer for loop
                    'so you can search using the next criterion
                    Exit For
                End If
            End If
        Next
    Next
End Sub

如果我很接近,但不完全在那里,请在评论中提问。

答案 2 :(得分:0)

我试图进行优化,但不了解您希望从此行代码中获得什么

  

c.Offset(,-3)

Vue.prototype.$axios = axios.create(
  {
    headers:
      {
        'Content-Type': 'application/json'
      },
    baseURL: process.env.API_URL
  }
);

Vue.prototype.$axios.interceptors.request.use(
  config =>
  {
    if (store.getters.isUserLoggedin) config.headers['Authorization'] = 'Bearer ' + store.getters.user.token;
    return config;
  }
);

Vue.prototype.$axios.interceptors.response.use(
  response => response,
  error =>
  {
    if (error && error.response && error.response.status && error.response.status === 403)
    {
      store.dispatch('unsetUser');
      router.push('/login');
      return Promise.resolve(); // Avoid showing any errors
    }
    else
    {
      logger.error({
        message: 'Error in HTTP request',
        response: JSON.stringify(extractError(error)),
        status: _get(error, 'response.status', 'unknown'),
        url: _get(error, 'config.url', 'unknown'),
        userId: store.getters.userInfo.id
      });
      throw error;
    }
  }
);