如何将存储的范围粘贴到下一个空闲列和某些行?

时间:2014-07-08 11:38:55

标签: excel excel-vba range vba

我一直坚持这个问题,如果有人能帮助我,我真的很感激。

所以这就是我想用这个宏做的事情。

  1. 从工作表" Overtime"
  2. 获取两个不同范围的数据
  3. 将这些数据范围彼此相邻地存储在工作表"摘要" 2.1主要思想是宏将数据存储到最后一个条目旁边的摘要表中。
  4. 当我将范围定义为.Range(" E5:E37")时,我能够执行此操作但是我需要将预定义列更改为下一个空列,如何执行此操作?

    我尝试将范围更改为范围(.Cells(5,CheckCol),。Cells(37,CheckCol)),但这不起作用。

    很抱歉,如果代码看起来搞砸了,我刚刚开始使用VBA,结尾部分有点乱,因为我不知道该怎么做:D这就是我做过的事情到目前为止。

    Sub Transfer_Data_1()
    '
    ' Transfer_Data_1 Macro
    
    ' Locate cells on a row
    PasteToCol = Sheets("Summary").Cells(5, Columns.Count).End(xlToLeft).Column + 1
    PasteToCol2 = Sheets("Summary").Cells(5, Columns.Count).End(xlToLeft).Column + 2
    CheckCol = Sheets("Summary").Cells(6, Columns.Count).End(xlToLeft).Column
    ' Locate the last stored date
    CheckDateOLD = Sheets("Summary").Cells(6, Columns.Count).End(xlToLeft).Column - 1
    
    ' Store date values and hours
    OvertimeDateOLD = Sheets("Summary").Cells(6, CheckDateOLD).Value
    OvertimeTotal = Sheets("Overtime").Cells(40, 7).Value
    OvertimeTotalOLD = Sheets("Summary").Cells(37, CheckCol).Value
    
    Set OvertimeDatesNEW = GetDates(Sheets("Overtime"), 8, 2, 40)
    Set OvertimeHoursNEW = GetHours(Sheets("Overtime"), 8, 7, 40)
    
    'Convert entered date to month name
    OvertimeDate2 = Sheets("Overtime").Cells(9, 2).Value
    OvertimeDate = MonthName(Month(OvertimeDate2))
    
    ' Check if the values were already there and paste accordingly if not
    If OvertimeDateOLD = OvertimeDate And OvertimeTotalOLD = OvertimeTotal Then
        MsgBox "These results have already been updated"
    ElseIf OvertimeDateOLD = OvertimeDate And OvertimeTotalOLD <> OvertimeTotal Then
        Sheets("Summary").Range(5, PasteToCol).Value = OvertimeDatesNEW
        Sheets("Summary").Cells(5, PasteToCol).Value = OvertimeHoursNEW
        MsgBox "Data for this month has been updated"
    ElseIf OvertimeDateOLD <> OvertimeDate And OvertimeTotalOLD <> OvertimeTotal Then
        Sheets("Summary").Range(.Cells(5, CheckCol), .Cells(37, CheckCol)).Value = OvertimeDatesNEW.Value
        Sheets("Summary").Range("F5:F37").Value = OvertimeHoursNEW.Value
        MsgBox "Results have been stored to Summary"
    End If
    
    End Sub
    

    这两个函数用于从两个范围获取值。它本可以更轻松地完成,但我想测试功能以及我这样做是为了学习。

    >Function GetDates(Overtime As Worksheet, StartRow As Integer, StartCol As Integer, EndRow >As Integer)
    >Dim DataTableDates As Range
    >
    >Set DataTableDates = Overtime.Range(Overtime.Cells(StartRow, StartCol), >Overtime.Cells(EndRow, StartCol))
    >Set GetDates = DataTableDates
    >End Function
    >
    >Function GetHours(Overtime As Worksheet, StartRow As Integer, StartCol As Integer, EndRow >As Integer)
    >Dim DataTableHours As Range
    >
    >Set DataTableHours = Overtime.Range(Overtime.Cells(StartRow, StartCol), >Overtime.Cells(EndRow, StartCol))
    >Set GetHours = DataTableHours
    >End Function
    

1 个答案:

答案 0 :(得分:0)

看起来您在.

Cells之前添加了Sheets("Summary").Range(.Cells(5, CheckCol), .Cells(37, CheckCol)).Value = OvertimeDatesNEW.Value

您可能想尝试

Sheets("Summary").Select
Range(Cells(5, CheckCol), Cells(37, CheckCol)).Value = OvertimeDatesNEW.Value
Range("F5:F37").Value = OvertimeHoursNEW.Value

With Sheets("Summary")
    .Range(.Cells(5, CheckCol), .Cells(37, CheckCol)).Value = OvertimeDatesNEW.Value
    .Range("F5:F37").Value = OvertimeHoursNEW.Value
End With

以下是一个例子:

Sub FindLastCol()
    Dim LastCol As Integer

    'Find the first empty column in row 1 on the active sheet
    LastCol = Rows(1).Columns(Columns.Count).End(xlToLeft).Column + 1

    'Set Rows 1 through 50 in the first empty column to "Example"
    Range(Cells(1, LastCol), Cells(50, LastCol)) = "Example"
End Sub