VBA在没有格式的情况下分配值

时间:2014-12-15 18:52:03

标签: excel vba excel-vba formatting assign

我在工作簿中循环,以便将数据汇总到一张纸上。各种源表上的数据始终位于相同的列中,但行数会有所不同。我正在分配值,但条件格式仍然存在。

基本上将值从一本书复制并粘贴到另一本书的最快和最好的方法是什么?屏幕更新已关闭。

 For Each sheet In Workbooks(filename).Worksheets
        If sheet.Name = "Template" Then
            lastrow = sheet.Range("A" & Rows.Count).End(xlUp).row
            For row = 2 To lastrow
                All.Range("A" & All_nextrow).Value = sheet.Range("A" & row).Value
                All.Range("B" & All_nextrow).Value = sheet.Range("B" & row).Value
                All.Range("C" & All_nextrow).Value = sheet.Range("C" & row).Value
                All.Range("D" & All_nextrow).Value = sheet.Range("D" & row).Value
                All.Range("E" & All_nextrow).Value = sheet.Range("E" & row).Value
                All.Range("F" & All_nextrow).Value = sheet.Range("F" & row).Value
                All.Range("G" & All_nextrow).Value = sheet.Range("G" & row).Value
                All.Range("H" & All_nextrow).Value = sheet.Range("H" & row).Value
                All.Range("I" & All_nextrow).Value = sheet.Range("I" & row).Value
                All.Range("J" & All_nextrow).Value = sheet.Range("J" & row).Value
                All.Range("K" & All_nextrow).Value = sheet.Range("K" & row).Value
                All.Range("L" & All_nextrow).Value = Workbooks(filename).Name
                All_nextrow = All_nextrow + 1
            Next row
        End If
    Next sheet

4 个答案:

答案 0 :(得分:0)

我不确定你需要什么,但是如果你想复制A2和L之间的所有细胞{lastrow}你可以使用:

lastrow = SheetFrom.Range("A" & Rows.Count).End(xlUp).Row
SheetFrom.Range("A2:L" & lastrow).Copy
SheetTo.Range("A2").PasteSpecial (xlPasteValues)

答案 1 :(得分:0)

Sub CopyPaste()
Dim WorkbookToCopy As Workbook
Dim WorkbookToPaste As Workbook
Dim RowCount As Integer

Set WorkbookToCopy = Workbook1 'Workbook to copy name like Workbook1
Set WorkbookToPaste = Workbook2
RowCount = 1        ' 'clean' row in WorkbookToPaste
For Each Sheet In Workbook1
    For Each Column In Sheet
    RowCount = 1
        For Each Cell In Column
            WorkbookToPaste.Sheets(Sheet).Cells(RowCount, Column).Value = Cell.Value
            RowCount = RowCount + 1
        Next
    Next
Next
End Sub

我不确定它是否有效,但我想向您展示一些逻辑,您可以在宏中使用它。

答案 2 :(得分:0)

这可能就是你要找的东西。您需要了解的代码存在一些问题。

在这个例子中,我使用" All"作为你把它放在上面的工作表的名称。

这段代码使用一个简单的循环遍历列,你使用Range和列的实际字母名称,这使用Cell(lRow,lCol)并循环,直到你到达L列的地方改变模式。

我还删除了每个工作表,因为你正在运行一个IF语句,只能进行#34;模板"将要使用的那个。因此,没有必要循环遍历所有这些以找到您想要的那个。如果你打算使用更多,那么If Sheet.Name =" Template"需要去。

提供此代码,并根据您的需要进行修改。如果您对任何故障发表评论,我将很乐意修改答案。

Sub DataAggregate()

Dim sheet As String
Dim all As String
Dim allRow As Long

    all = "All"    'whatever the name of "ALL" is, set here.
    allRow = 2

    sheet = "Template"

    lastRow = Sheets(sheet).Range("A" & Rows.Count).End(xlUp).row
        For lRow = 2 To lastRow
            For lCol = 1 To 11
                Sheets(all).Cells(allRow,lCol) = Sheets(sheet).Cells(lRow, lCol).Text
            Next lCol

            Sheets(all).Cells(allRow, "L") = sheet  'or filename'  'confused as to what you want
            allRow = allRow + 1
        Next lRow
    Next ws
End Sub

答案 3 :(得分:0)

对于仍在寻找好的答案的任何人,可以结合使用 NumberFormat 和。 FormulaR1C1 来控制单元格格式。

当任何值存储在带有“ .value”的单元格中时,将对其内部进行格式化。要覆盖此内容,您需要更改NumberFormat并使用“ .FormulaR1C1”而不是“ .value”。

All.Range("A" & All_nextrow & ":" & "L" & All_nextrow).NumberFormat = "@"

然后

All.Range("A" & All_nextrow).FormulaR1C1 = sheet.Range("A" & row).Value

您可以按照@genespos的建议通过.copy和.PasteSpecial实现此目的,但我不喜欢宏执行期间的单元交互。