2016-12-11 69 views
0

我是一個vba上的新手。 我已經成功地將其他工作簿中的多個工作表導入到一張工作表中。 但我想保持格式,並保持輸入單元格的顏色。 這是我的代碼SOFAR:Excel vba將幾張紙導入到單張紙並保持格式化

Public strFileName As String 
Public currentWB As Workbook 
Public dataWB As Workbook 
Public strCopyRange As String 

Sub delData() 'Clears MasterData except 1st line 
Dim ws As Worksheet 
For Each ws In Worksheets 
    Select Case ws.Name 
    Case Is = "List", "Sheet1" 
     ' Do Nothing 
    Case Else 
     ws.UsedRange.Offset(1).ClearContents 


    End Select 
Next ws 
End Sub 

Sub GetData() 
Dim strWhereToCopy As String, strStartCellColName As String 
Dim strListSheet As String 

strListSheet = "List" 

On Error GoTo ErrH 
Sheets(strListSheet).Select 
Range("B2").Select 

'this is the main loop, we will open the files one by one and copy their data into the masterdata sheet 
Set currentWB = ActiveWorkbook 
Do While ActiveCell.Value <> "" 

    strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value 
    strCopyRange = ActiveCell.Offset(0, 2) & ":" & ActiveCell.Offset(0, 3) 
    strWhereToCopy = ActiveCell.Offset(0, 4).Value 
    strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1) 

    Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True 
    Set dataWB = ActiveWorkbook 

    Range(strCopyRange).Select 
    Selection.Copy 

    currentWB.Activate 
    Sheets(strWhereToCopy).Select 
    lastRow = LastRowInOneColumn(strStartCellColName) 
    Cells(lastRow + 1, 1).Select 

    Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone 
    Application.CutCopyMode = False 
    dataWB.Close False 
    Sheets(strListSheet).Select 
    ActiveCell.Offset(1, 0).Select 
Loop 
Exit Sub 


ErrH: 
    MsgBox "It seems some file was missing. The data copy operation is not complete." 
Exit Sub 


End Sub 

Public Function LastRowInOneColumn(col) 
    'Find the last used row in a Column: column A in this example 
    Dim lastRow As Long 
    With ActiveSheet 
    lastRow = .Cells(.Rows.Count, col).End(xlUp).Row 
    End With 
    LastRowInOneColumn = lastRow 
End Function 

我的函數的GetData()犯規覆蓋現有數據 - 所以我做了一個解決辦法,創造功能delData()。我如何讓函數GetData覆蓋現有數據? 我想保留從我導入的工作表的格式和顏色。這也應該覆蓋舊的數據/顏色。

我真的希望這是有道理的人:O) 任何建議最apreciated 謝謝...

回答

0

用途:

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

,而不是Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone

+0

看來,如果工作我保留兩條線,而不是用新的替換舊線。你知道我如何隱藏特定顏色的細胞嗎?像排序和Filtre - 但我希望能夠隱藏具有特定顏色的單元格 – Brian

+0

不要介意關於隱藏具有特定顏色單元格的問題。我用分類和過濾器完成了這個。感謝您的幫助。我真的很感激它:o) – Brian

相關問題