2017-09-21 30 views
0

我會加入數據轉換成我的日常工作簿,我通常會使用一套公式來計算的故障率和成功對我來說。我已經有代碼編譯的數據,但我現在缺乏的是如何設定的公式複製下一個空單元格,以便它可以幫我算算率。我的公式集從「P22」添加到「AB22」,我需要將這些公式複製到下一個空行。這是我現在的宏,如果足夠的話請檢查一下,看看是否有改進的地方,因爲我還是vba的新手。非常感謝你。我的工作表的如何從選擇的範圍複製公式,並將其複製到下一個空行與宏

Sub trial() 

Dim wb As Workbook, wb2 As Workbook, wb3 As Workbook 
Dim ws As Worksheet 

Dim fn As String 

Set wb = ActiveWorkbook 
Set ws = Sheets.Add(After:=Sheets(Worksheets.Count)) 
Dim Ret 

    Ret = Application.GetOpenFilename("Lkl Files (*.lkl), *.lkl") 

    If Ret <> False Then 
     With ActiveSheet.QueryTables.Add(Connection:= _ 
     "TEXT;" & Ret, Destination:=Range("$A$1")) 
     .Name = "SPC_PLTB_450B_12092107_25°C_CW" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = 65001 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = True 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = False 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
     .TextFileDecimalSeparator = "," 
     .TextFileThousandsSeparator = "." 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 

     End With 
    End If 


    Sheets(2).Activate 

    'this is for the date (loop) 

    Dim FirstCell As String 
     Dim i As Integer 
      FirstCell = "C19" 
      Range(FirstCell).Select 
      Do Until ActiveCell.Value = "" 
      If ActiveCell.Value = "" Then 
      Exit Do 
      Else 
      ActiveCell.Offset(1, 0).Select 
      End If 
      Loop 
      ActiveCell = Format(Date, "mm/dd/yyyy") 

    ws.Activate 
    ws.AutoFilterMode = False 
    ws.Range("$A$9:$P$417").AutoFilter Field:=5, Criteria1:= _ 
     "1" 
    Range("F31:F401").Select 
    Selection.Copy 



    Sheets(2).Activate 


    'this is for the raw data 

      FirstCell = "D19" 
      Range(FirstCell).Select 
      Do Until ActiveCell.Value = "" 
      If ActiveCell.Value = "" Then 
      Exit Do 
      Else 
      ActiveCell.Offset(1, 0).Select 
      End If 
      Loop 


    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=True 

    Sheets(3).Activate 
    FirstCell = "C19" 
      Range(FirstCell).Select 
      Do Until ActiveCell.Value = "" 
      If ActiveCell.Value = "" Then 
      Exit Do 
      Else 
      ActiveCell.Offset(1, 0).Select 
      End If 
      Loop 
      ActiveCell = Format(Date, "mm/dd/yyyy") 

    ws.Activate 

    Range("D31:D401").Select 
    Application.CutCopyMode = False 
    Selection.Copy 


    Sheets(3).Activate 
      FirstCell = "D19" 
      Range(FirstCell).Select 
      Do Until ActiveCell.Value = "" 
      If ActiveCell.Value = "" Then 
      Exit Do 
      Else 
      ActiveCell.Offset(1, 0).Select 
      End If 
      Loop 


    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=True 

     Sheets(4).Activate 
    FirstCell = "C19" 
      Range(FirstCell).Select 
      Do Until ActiveCell.Value = "" 
      If ActiveCell.Value = "" Then 
      Exit Do 
      Else 
      ActiveCell.Offset(1, 0).Select 
      End If 
      Loop 
      ActiveCell = Format(Date, "mm/dd/yyyy") 

    ws.Activate 

    Range("G31:G401").Select 
    Application.CutCopyMode = False 
    Selection.Copy 



    Sheets(4).Activate 
    FirstCell = "D19" 
      Range(FirstCell).Select 
      Do Until ActiveCell.Value = "" 
      If ActiveCell.Value = "" Then 
      Exit Do 
      Else 
      ActiveCell.Offset(1, 0).Select 
      End If 
      Loop 


    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=True 


End Sub 

enter image description here

回答

0

這是棘手給一個理想的解決方案,而更多地瞭解你現有的Excel文件,但我做到了根據你發佈什麼,放在一起的Excel與您一對夫婦可能的解決方案文件中的一些假設。

既然我們不能將文件附加到這裏的職位我上傳到免費的文件主機FileTown;您可以下載[宏禁用] XLSX file here

按照以下步驟爲我的意思有關數據現在創建公式一個例子開始對錶「例1」,您將添加,以及如何通過使使圖表自動更新將數據導入Excel'表格',以及如何創建到源數據文件的數據連接,以便您無需在數據更改時重新導入數據,所有無宏

不妨礙你學習VBA &宏,但Excel有一些非常強大的內置數據管理功能,在這種情況下,這可能是一個比自定義更好的方法。另一方面,我首先通過在做簡單的重複性任務時記錄宏來開始學習VBA(在90年代的Excel v.5中),然後通過「改變這條線或刪除那條線」來試驗生成的VBA並看到發生了什麼; Excel中自動生成的大約一半的代碼可能是無關緊要的。 (只需在對它們進行備份之前製作一份備份文件,並檢查MSDN/VBA或Google的任何您感興趣的內容,並且沒有任何東西會丟失......我是Excel的忠實粉絲,因爲在VBA和它的'內置函數,它的功能是無窮無盡的!)

+0

非常感謝你@ashleedawg!沒有期望的東西如此詳細,但非常感謝哈哈..雖然我正在考慮用宏來做,但我認爲手動將它拖拽到100行應該沒問題,我猜... – Fong

+0

不客氣。別擔心,您將有*很多*更多的機會讓MS Office變得超級複雜! – ashleedawg

0

沒有動搖你學習VBA,但我認爲你可以逃脫,沒有它在這種情況下。

你可以做一個小的變化,以「P22」到「AB22」的公式,讓他們留空白,如果存在「d至N」的數據。

使用你的榜樣,複製單元格P22並粘貼到細胞P23。接下來,圍繞您的公式輸入「if」語句,如:

=IF (D23 = "" , "" , {your existing formula here}) 

...並將該公式填充到右側,並向下幾十行。在您輸入工作表左側的數據之前,單元格「P到AB」將爲空白。這很難解釋,但如果這沒有意義,我可以給你一個樣本工作表。我

+0

如果我可以有一個示例工作表來指導我完成它,將不勝感激。從您的示例中,它是否會讓我對代碼宏進行硬編碼?因爲對我而言,硬編碼並不是真的可取,因爲我會將越來越多的數據加入其中。因此,搜索下一個空行會更好。或者,我太新了,無法理解您在那裏編碼的真實性對不起,如果我誤以爲你.. – Fong

相關問題