VBA: lastrow

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
708
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
In the range of ("D10:T41") I am looking for the last used row + 1 and put data into it from another sheet
However, I am filling all rows from lr to the end of the range T41.
I believe my issue is on this line but can't figure it out. .Range("D41:T" & lr).Value = Sheets("Admin").Range("DailyAccumData").Value
I have tried .Range("D" & lr).Value = Sheets("Admin").Range("DailyAccumData").Value
however, that puts the data in one cell, not the range of cells.

Some assistance please.

Full code:
VBA Code:
Sub MoveAccums()
Dim lr As Long
Dim iReply As String
Dim Sht As Worksheet
Dim intDaysInMonth As Integer
Dim i As Integer
Dim Mess As Object, Recip

Application.ScreenUpdating = False

    With Sheets("Accumulative")
        If .Range("D41").Value = Empty Then
                lr = Sheets("Accumulative").Range("D41").End(xlUp).Row + 1
                'Debug.Print lr
                .Range("D41:T" & lr).Value = Sheets("Admin").Range("DailyAccumData").Value
            Else:
                iReply = MsgBox(Prompt:="Daily Accumulative data is full" & vbNewLine & _
                "Do you want to clear the data for a new month?." & vbNewLine & vbNewLine & "Click YES to continue or NO to cancel", _
                 Buttons:=vbYesNo, Title:="Daily Accumulative Data")

                If iReply = vbNo Then
                    Exit Sub
                Else:
             
        If Not .Range("D60").Value = Empty Then
                iReply = MsgBox(Prompt:="Monthly Accumulative data is full" & vbNewLine & _
                "This data must be cleared to continue!." & vbNewLine & vbNewLine & "Click OK to continue.", _
                Buttons:=vbOKOnly, Title:="Monthly Accumulative Data")
             
                .Range("D49:T60").ClearContents
                .Unprotect Password:="Accumulative"
                lr = Sheets("Accumulative").Range("D60").End(xlUp).Row + 1
                Sheets("Accumulative").Range("D49:T" & lr).Value = Sheets("Accumulative").Range("D42:T42").Value
                .Range("AccumMonthlyTotals").Calculate
                .Range("C10:T41").ClearContents

                intDaysInMonth = Day(DateSerial(Year(Now()), Month(Now()) + 1, 0)) + 1 '+ TimeValue("07:00:00")
                   .Cells(10, 3).Resize(intDaysInMonth, 1).ClearContents
                For i = 1 To intDaysInMonth
                    .Cells(10, 3).Offset(i - 1, 0) = DateSerial(Year(Now()), Month(Now()), i) + TimeValue("07:00:00")
                Next i
             
                lr = Sheets("Accumulative").Range("D41").End(xlUp).Row + 1
                Sheets("Accumulative").Range("D10:T" & lr).Value = Sheets("Admin").Range("Q4:AG4").Value
                Range("AccumMonthlyTotals").Calculate
                Range("AccumTotals").Calculate
                End If
         End If
       End If
    End With
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Sheets("Admin").Range("DailyAccumData").Value
What do you have in the range named "DailyAccumData"? I assume you have a range of cells from column D to column T or something similar.
Try the following:

Rich (BB code):
Sub MoveAccums()
  Dim lr As Long
  Dim iReply As String
  Dim Sht As Worksheet
  Dim intDaysInMonth As Integer
  Dim i As Integer
  Dim Mess As Object, Recip
  
  Application.ScreenUpdating = False

  With Sheets("Accumulative")
    If .Range("D41").Value = Empty Then
      lr = 10
      Do While .Range("D" & lr).Value <> ""
        lr = lr + 1
      Loop
      .Range("D" & lr & ":T" & lr).Value = Sheets("Admin").Range("DailyAccumData").Value
    Else
      iReply = MsgBox(Prompt:="Daily Accumulative data is full" & vbNewLine & _
      "Do you want to clear the data for a new month?." & vbNewLine & vbNewLine & "Click YES to continue or NO to cancel", _
       Buttons:=vbYesNo, Title:="Daily Accumulative Data")

      If iReply = vbNo Then
        Exit Sub
      Else
        If Not .Range("D60").Value = Empty Then
          iReply = MsgBox(Prompt:="Monthly Accumulative data is full" & vbNewLine & _
            "This data must be cleared to continue!." & vbNewLine & vbNewLine & "Click OK to continue.", _
            Buttons:=vbOKOnly, Title:="Monthly Accumulative Data")
          
          .Range("D49:T60").ClearContents
          .Unprotect Password:="Accumulative"
          lr = .Range("D60").End(xlUp).Row + 1
          .Range("D49:T" & lr).Value = .Range("D42:T42").Value
          .Range("AccumMonthlyTotals").Calculate
          .Range("C10:T41").ClearContents

          intDaysInMonth = Day(DateSerial(Year(Now()), Month(Now()) + 1, 0)) + 1 '+ TimeValue("07:00:00")
          .Cells(10, 3).Resize(intDaysInMonth, 1).ClearContents
          For i = 1 To intDaysInMonth
            .Cells(10, 3).Offset(i - 1, 0) = DateSerial(Year(Now()), Month(Now()), i) + TimeValue("07:00:00")
          Next i
                
          lr = .Range("D41").End(xlUp).Row + 1
          .Range("D10:T" & lr).Value = Sheets("Admin").Range("Q4:AG4").Value
          Range("AccumMonthlyTotals").Calculate
          Range("AccumTotals").Calculate
        End If
      End If
    End If
  End With
End Sub

Note: To put formatting (colors, font size, etc.) in your code, use the RICH icon:
1673370097500.png

------
 
Upvote 1
Solution
Yes, there is another range on the same sheet. One range is daily, the other is monthly.
I will give it a try in a bit.

Thanks for the reply!
 
Upvote 0

Forum statistics

Threads
1,214,804
Messages
6,121,652
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top