0

这是我的代码行:

ActiveCell.FormulaR1C1 = "='Main'!R[9]C[0]&LEFT("" "",5-LEN('Main'!R[9]C[1])) &'Main'!R[9]C[1]&"" ""&DAY('Main'!R[4]C[8]) & ""-"" & UPPER(TEXT('Main'!R[4]C[8],""mmm"")) & ""-"" &YEAR('Main'R[4]C[8])&"" "" & 'Main'!R[9]C[3] & "" "" & 'Main'!R[9]C[4] & "" "" & LEFT('Main'!R[9]C[5],35)& LEFT("" "",35-LEN(LEFT('Main'!R[9]C[5],35))) & "" "" & LEFT("" "", 12-LEN('Main'!R[9]C[6]))& 'Main'!R[9]C[6] & "" "" & 'Main'!R[9]C[7] & "" "" & LEFT('Main'!R[4]C[9],9) & LEFT("" "",9-LEN(LEFT('Main'!R[4]C[9],9))) & "" "" & 'Main'!R[9]C[2] & LEFT("" "",4-LEN('Main'!R[9]C[2])) & "" DATALOAD"""

我得到一个run-time error 1004说法,它是一个application-defined or object-defined error。问题是我对 VBA 不是很熟悉。这个公式中有很多东西。基本上,我需要的只是将数据以 EXACT 格式粘贴到新工作表上。这就是为什么会有一堆空格之类的。我之所以使用R1C1,是因为每行在工作表 1 上都有不同的数据,需要相应地粘贴到工作表 2 中。

关于为什么此代码遇到错误的任何想法?任何帮助将不胜感激。

为了澄清:我的整个宏如下......

Dim row As Integer
Dim col As String
col = "A"
row = 1
Do Until IsEmpty(ActiveCell) 
     Sheets("Protected").Select 
     Range(col & row).Select 
     ActiveCell.FormulaR1C1 = "big long code as mentioned above goes here"
     Sheets("Main").Select
     ActiveCell.Offset(1, 0).Select 
     row = row + 1

从评论中复制

实际工作公式为

=Main!A11&LEFT(" ",5-LEN(Main!B11)) &Main!B11&" "&DAY(Main!$D$6) & "-" & UPPER(TEXT(Main!$D$6,"mmm")) & "-" &YEAR(Main!$D$6)&" " & Main!D11 & " " & Main!E11 & " " & LEFT(Main!F11,35)& LEFT(" ",35-LEN(LEFT(Main!F11,35))) & " " & LEFT(" ", 12-LEN(Main!G11))& Main!G11 & " " & Main!H11 & " " & LEFT(Main!$E$6,9) & LEFT(" ",9-LEN(LEFT(Main!$E$6,9))) & " " & Main!C11 & LEFT(" ",4-LEN(Main!C11)) & " DATALOAD"

4

1 回答 1

3

当您有一个工作公式时,最好的办法是记录一个宏来查看 Excel 如何将公式存储在单元格中。

这是我从宏中得到的

ActiveCell.FormulaR1C1 = "=Main!R[10]C[-3]&LEFT("" "",5-LEN(Main!R[10]C[-2])) &Main!R[10]C[-2]&"" ""&DAY(Main!R6C4) & ""-"" & UPPER(TEXT(Main!R6C4,""mmm"")) & ""-"" &YEAR(Main!R6C4)&"" "" & Main!R[10]C & "" "" & Main!R[10]C[1] & "" "" & LEFT(Main!R[10]C[2],35)& LEFT("" "",35-LEN(LEFT(Main!R[10]C[2],35))) & "" "" & LEFT("" "", 12-LEN(Main!R[10]C[3]))& Main!R[10]C[3] & "" "" & Main!R[10]C[4] &" & " LEFT(Main!R6C5,9) & LEFT("" "",9-LEN(LEFT(Main!R6C5,9))) & "" "" & Main!R[10]C[-1] & LEFT("" "",4-LEN(Main!R[10]C[-1])) & "" DATALOAD"""

您可以将此公式与您的公式进行比较,看看缺少什么:)

于 2013-04-03T17:04:57.577 回答