简体   繁体   English

将vlookup公式输入单元格时出现错误1004 vba excel

[英]Error 1004 vba excel when entering vlookup formula into cell

I have tried everything, including reading many questions in the forum about Error 1004 Application Defined or Object-Defined Error, but I can't find anything that is helping me fix the error. 我已经尝试了一切,包括在论坛中阅读有关错误1004应用程序定义或对象定义错误的许多问题,但我找不到任何帮助我修复错误的内容。 It writes the correct formula in the Immediate window.Here is the code. 它在立即窗口中写入正确的公式。这是代码。 I've put where I get the error in the code. 我把代码中的错误放在了哪里。

The variable definitions are the following, and I've included the values in the watch window at the time of the Error 1004 变量定义如下,我在错误1004时将值包含在监视窗口中

Dim templateName As String       ' = "Company 1"   and yes this worksheet exists in my file
Dim servicesRow As Integer       ' = 22
Dim j as Integer                 ' = 1
Public firstProjectSOF as Range  ' firstProjectSOF.Row = 5
Dim columnTemp As Variant        ' columnTemp(0) = "A"
Public lastProjectSOF as Range   ' lastProjectSOF.Row = 65
Dim Rng as Range                 ' Rng.Column = 17



For j = 1 To numEmployees

    With Sheets("SOF").Range("E5", Range("E5").End(xlToRight))       'Finds the cell in SOF of that employee's last name
        Set Rng = .Find(What:=employees_lastName(j), _
                    LookIn:=xlValues, _
                    LookAt:=xlPart, _
                    SearchOrder:=xlByColumns, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)
    End With
    'This is where the error 1004 occurs, on trying to write the VLOOKUP formula into the cell

    Sheets(templateName).Range(Cells(servicesRow + 1 + j, 5)).FormulaLocal = "=VLOOKUP($G$10,'Staff Output Final'!B" & firstProjectSOF.Row & ":" _
    & columnTemp(0) & lastProjectSOF.Row & "," & (Rng.Column - 1) & ",FALSE)"
Next

For info, if it is relevant, my computer is a Mac from Canada (English), but I recently edited the file on a PC computer in France (with OS in French). 有关信息,如果它是相关的,我的电脑是来自加拿大的Mac(英语),但我最近在法国的PC电脑上编辑了该文件(使用法语操作系统)。 Just in case that was creating problems, I just tried creating a new excel file on my Mac in and copying and pasting the tabs from the old file into the new one, but I am still getting the Error 1004... 为了防止出现问题,我只是尝试在我的Mac上创建一个新的excel文件,然后将旧文件中的选项卡复制并粘贴到新文件中,但我仍然收到错误1004 ...

Thanks for any help! 谢谢你的帮助! Much appreciated! 非常感激!

In fact, I just tried something by chance and it worked.... I removed Range, and just left the Cells part, so the code became: 事实上,我只是偶然尝试了一些东西并且它有效....我删除了Range,然后离开了Cells部分,所以代码变为:

Sheets(templateName).Cells(servicesRow + 1 + j, 5).FormulaLocal = "=VLOOKUP($G$10,'Staff Output Final'!B" & firstProjectSOF.Row & ":" _
& columnTemp(0) & lastProjectSOF.Row & "," & (Rng.Column - 1) & ",FALSE)"

I'm not quite sure why this works, but it does.... 我不太清楚为什么会这样,但它确实......

Does anyone know why??? 有谁知道为什么??? Since higher up in the same code, I use the following code to assign a VLOOKUP formula to another cell, and it worked with "Range" no problems. 由于在相同的代码中更高,我使用以下代码将VLOOKUP公式分配给另一个单元格,并且它使用“Range”没有问题。

Sheets(templateName).Range("A18:H18").FormulaLocal = "=VLOOKUP($G$10,'PEIINV2 - paste here'!B" & firstProjectPEIINV.Row & ":AH" & lastProjectPEIINV.Row & _
",2,FALSE)"
Sheets(templateName).Range(Cells(servicesRow + 1 + j, 5))

Your starting code misses a sheets(templatename) before cells(...), so if the activesheet is not templateName, it could trigger mistakes. 您的起始代码在单元格(...)之前缺少工作表(templatename),因此如果活动表格不是templateName,则可能会触发错误。

You might also want to change 您可能还想要改变

With Sheets("SOF").Range("E5", Range("E5").End(xlToRight))   

to

With Sheets("SOF").Range("E5", sheets("SOF").Range("E5").End(xlToRight))   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM