2013-02-06 39 views
0

兩個「彼得」幫我走出這一個以前,但我無法完整代碼添加到我的評論。公式複製不正確使用VB - 全碼

問題是:當我將「.FormulaR1C1」添加到下面的代碼中時,我得到一個錯誤消息爲「Object required」。它沒有使用「.FormulaR1C1」,但會在實際公式中產生問題。

守則

表(gcsCombinedSheetName).Cells(lngLastRow,columnletter).FormulaR1C1 = getConfigPosition.Offset(0,2).Formula.R1C1

完整代碼

Sub Pricing_format() 

Dim lngLastRow As Long 
Dim lngLastRow2 As Long 
Dim Fundcolumn As Long 
Dim rngToCheck As Range 
Dim columnletter As String 
Dim columnheader As String 
Dim ActuateColumn As Long 
Dim getConfigPosition As Range 
Dim counter As Long 
Dim getFormula As Variant 
Dim Wk As Worksheet 
Dim WB As Workbook 

Application.ScreenUpdating = False 
Application.DisplayAlerts = False 
Application.Calculation = xlCalculationAutomatic 

Set gRwksconfigeration = Sheets(gcsConfigSheetName) 
gRnCT_FieldSearch_Brks = gRwksconfigeration.Range(CT_FieldSearch_Brks) 
gRnCT_Fieldsearch_SecurID = gRwksconfigeration.Range(CT_FieldSearch_SecurID) 
gRnCT_FieldSearch_BrokerFactor = gRwksconfigeration.Range(CT_FieldSearch_BrokerFactor) 
gRnCT_Required_Col = gRwksconfigeration.Range(CT_Required_Col) 
gRnCT_File_Loc = gRwksconfigeration.Range(CT_File_Loc) 
gsInputFileName = Dir(gRnCT_File_Loc) 

counter = 1 

Set gwkscurrent = ActiveWorkbook 

    With Sheets(gcsCombinedSheetName) 
     .Cells.Clear 
    End With 

    Do 

     lngLastRow2 = FindLastRow(gwkscurrent.Sheets(gcsCombinedSheetName).Name) 

     'On Error GoTo OpenError 
      Set gwkbInputdata = Workbooks.Open(gRnCT_File_Loc & gsInputFileName) 
     'On Error GoTo 0 

     With Sheets(1) 

      Fundcolumn = Sheets(1).Cells.Find(What:=gRnCT_Fieldsearch_SecurID, after:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, searchorder:=xlByColumns).Column 
      lngLastRow = FindLastRow(Sheets(1).Name) 

       If Application.WorksheetFunction.CountA(.Cells) > 0 Then 

        Set rngToCheck = Range(.Cells(1, Fundcolumn), .Cells(lngLastRow, Fundcolumn)) 

        If rngToCheck.Count > 1 Then 

          On Error Resume Next 
          rngToCheck.SpecialCells(xlCellTypeBlanks).EntireRow.Delete 
          On Error GoTo 0 
         Else 
          If VBA.IsEmpty(rngToCheck) Then rngToCheck.EntireRow.Delete 
        End If 

       End If 

       On Error Resume Next 
        .Cells.Find(What:=gRnCT_FieldSearch_Brks, after:=ActiveCell, LookIn:=xlValues _ 
         , LookAt:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlNext).EntireColumn.Delete Shift:=xlToLeft 
       On Error GoTo 0 

       On Error Resume Next 
        .Cells.Find(What:=gRnCT_FieldSearch_BrokerFactor, after:=ActiveCell, LookIn:=xlValues _ 
         , LookAt:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlNext).EntireColumn.Delete Shift:=xlToLeft 
       On Error GoTo 0 

      lngLastRow = FindLastRow(Sheets(1).Name) 

      Range(.Cells(1, 1), .Cells(lngLastRow, 1)).EntireRow.Copy gwkscurrent.Sheets(gcsCombinedSheetName).Cells(lngLastRow2 + 1, 1) 

      ActiveWorkbook.Close False 

     End With 

     gsInputFileName = Dir 

    Loop Until gsInputFileName = vbNullString 



    Set getConfigPosition = Sheets(gcsConfigSheetName).Cells.Find(What:=gRnCT_Required_Col, after:=ActiveCell, LookIn:= _ 
      xlValues, LookAt:=xlWhole, searchorder:=xlByColumns, searchdirection:= _ 
      xlNext, MatchCase:=False, SearchFormat:=False).Offset(counter, 0) 

    Do 

     columnletter = getConfigPosition 
     columnheader = getConfigPosition.Offset(0, 1) 
     getFormula = getConfigPosition.Offset(0, 2) 


      lngLastRow = FindLastRow(gcsCombinedSheetName) 

      Sheets(gcsCombinedSheetName).Cells(lngLastRow, columnletter).FormulaR1C1 = getConfigPosition.Offset(0, 2).Formula.R1C1 
      Sheets(gcsCombinedSheetName).Cells(2, columnletter) = getConfigPosition.Offset(0, 1) 

      With Sheets(gcsCombinedSheetName) 
       .Cells(lngLastRow, columnletter).Copy Range(.Cells(lngLastRow, columnletter), .Cells(3, columnletter)) 
      End With 

      counter = counter + 1 

      Set getConfigPosition = Sheets(gcsConfigSheetName).Cells.Find(What:=gRnCT_Required_Col, after:=ActiveCell, LookIn:= _ 
        xlValues, LookAt:=xlWhole, searchorder:=xlByColumns, searchdirection:= _ 
        xlNext, MatchCase:=False, SearchFormat:=False).Offset(counter, 0) 


    Loop Until getConfigPosition = "" 

    With Sheets(gcsCombinedSheetName).Rows("2:2") 
        .Font.Bold = True 
        .EntireColumn.AutoFit 
    End With 

    MsgBox ("Macro Complete") 

完Sub

回答

1

幾點意見:

  • 爲了解決您的問題,更換Formula.R1C1FormulaR1C1
  • 你的壓痕很奇怪。不需要例如在行lngLastRow = FindLastRow(gcsCombinedSheetName)中縮進。通常,只有當存在「子碼塊」時才縮進,例如在For循環,一個If等語句
  • 使用With只有當你訪問一個對象的多個屬性。
    With Sheets(gcsCombinedSheetName) 
        .Cells.Clear 
    End With
    可以Sheets(gcsCombinedSheetName).Cells.Clear
  • 代替考慮使用Range.Resize代替Range(Cell1, Cell2)) - 往往前者更容易

最後但並非最不重要的:可以在SO編輯問題,所以不需要打開一個新的! :-)

+0

感謝您所有的意見和你的意見是(一如既往)歡迎和非常有益的。我需要得到「.resize」的竅門,並開始使用更頻繁。再次感謝。 – user1624926

0

我明白了。當我在公式中使用RC []引用,並使用「.formula」而不是「.formulaR1C1」時,它起作用。它違背了我理解的邏輯,但它工作。我想感謝「Peter A」&「Peter L」的繼續支持。謝謝小夥子們。