不格式化分配值

问题描述 投票:0回答:4

我正在遍历工作簿,将数据汇总到一张纸上。各种原始工作表上的数据始终在同一列中,但各行会有所不同。

我正在分配值,但条件格式已通过。

关闭屏幕更新。

如何将值从一本书复制到另一本书?

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
excel vba assign
4个回答
0
投票

这可能是您要寻找的。您需要了解一些代码问题。

在此示例中,我使用“全部”作为您要放置到的工作表的名称。

此代码使用一个简单的循环来遍历各列,您在其中使用了Range和该列的实际字母名称,这使用了Cells(lRow,lCol)并以此方式循环,直到到达要更改的L列为止模式。

我还删除了每个工作表的,因为您正在运行IF语句,从而仅使将使用的“ TEMPLATE”。因此,无需遍历所有这些对象来找到您想要的对象。如果您打算使用更多功能,则需要使用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

0
投票

我不确定要了解什么,但是如果要复制A2和L {lastrow}之间的所有单元格,可以使用:

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

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

我不确定它的工作原理,但是我想向您展示一些逻辑,您可以在宏中使用。


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来实现,但我不喜欢宏执行期间的单元交互。
© www.soinside.com 2019 - 2024. All rights reserved.