Vba 如果范围中的单元格包含值,则在左侧单元格中插入文件路径

Vba 如果范围中的单元格包含值,则在左侧单元格中插入文件路径,vba,excel,Vba,Excel,上面的标题应该解释我在这里想要成功的原因。有人能帮我吗 Dim csv_ap As String Dim path_report As String Sheets("Mail Report").Activate csv_ap = Range("C65000").End(xlUp).Offset(1).Value If csv_ap = "NO" Then path_report = MAIN_PATH & "For Resolution\" & Format(today, "

上面的标题应该解释我在这里想要成功的原因。有人能帮我吗

Dim csv_ap As String
Dim path_report As String

Sheets("Mail Report").Activate
csv_ap = Range("C65000").End(xlUp).Offset(1).Value

If csv_ap = "NO" Then
path_report = MAIN_PATH & "For Resolution\" & Format(today, "dd_mm_yy") & "manual_handling_" & Range("A65000").End(xlUp).Value

End If

Range("B65000").End(xlUp).Offset(1).Value = path_report
我已经使用人们在下面的评论中提供给我的信息修改了上面的代码。我现在遇到的问题是,最后处理的“否”会将其路径位置粘贴到替换标头的范围(B1)中。有人能看出另一个问题在哪里吗

Dim csv_ap As String
Dim path_report As String

Sheets("Mail Report").Activate
csv_ap = Range("C65000").End(xlUp).Value

If csv_ap = "NO" Then
path_report = MAIN_PATH & "For Resolution\" & Format(today, "dd_mm_yy") & "manual_handling_" & Range("A65000").End(xlUp).Value

Range("B65000").End(xlUp).Value = path_report

End If

您的问题是这行
csv\u ap=Range(“C65000”).End(xlUp).Offset(1).Value

从看着它。您将进入
C65000
,然后向上移动,直到有一个值为止。但是你要给它加一个。因此,如果在中具有最后一个值的单元格是
C3
,它将转到
C4
,该单元格将为空。因此
csv\u app
将始终是
vbnullstring
,因此将行更改为

csv\u ap=范围(“C65000”)。结束(xlUp)。值


因此,您试图从该代码中找到
C
中的最后一个值,然后将该值与最新的空白单元格一起放入
B
列中

偏移量是你的问题它总是会返回一个空白单元格:我想到了这个:

Sub test()
    Dim csv_ap As String
    Dim path_report As String
    Dim ws As Worksheet
    Const MAIN_PATH As String = "FOR_TESTING_ONLY"

    Set ws = Sheets("Mail Report")

    csv_ap = ws.Range("C65000").End(xlUp).Value 'remove offset, you're always going to get a blank cell with it.

    If csv_ap = "NO" Then
        path_report = MAIN_PATH & "For Resolution\" & Format(Date, "dd_mm_yy") & "manual_handling_" & Range("A65000").End(xlUp).Value
        'changed today to date, Excel 2007 doesn't have a today function for me to test with
        Range("B65000").End(xlUp).Value = path_report ' removed offset to match csv_ap
    End If

End Sub

从另一篇文章中获得了一个运行良好的代码:

Dim csv_ap As Range
Dim path_report As String

Sheets("Mail Report").Activate
set csv_ap = Range("C65000").End(xlUp)

If csv_ap.Value = "NO" Then
path_report = MAIN_PATH & "For Resolution\" & Format(today, "dd_mm_yy") & "manual_handling_" & Range("A65000").End(xlUp).Value

csv_ap.Offset(0, -1) = path_report

End If

比你快一分钟=]使用上面的代码我修改了我的代码,但现在它将用最新的“否”路径位置替换标题(B1)。知道为什么会这样吗?我不小心编辑了你的答案而不是我的问题…对不起!在采纳您的建议后,它现在将用最新的“否”路径位置替换标题(B1)。知道为什么会这样吗?