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 谢谢...
看来,如果工作我保留两条线,而不是用新的替换旧线。你知道我如何隐藏特定颜色的细胞吗?像排序和Filtre - 但我希望能够隐藏具有特定颜色的单元格 – Brian
不要介意关于隐藏具有特定颜色单元格的问题。我用分类和过滤器完成了这个。感谢您的帮助。我真的很感激它:o) – Brian