Run-time error '9' - please help my sinking ship (excel workbook)

justlearning4

New Member
Joined
Nov 17, 2022
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hello,

This is my first time posting, so I hope it goes well. I am having an issue with developing a VBA code that transfers all data from the 'Insert CDRN Data" sheet to exact columns in the 'Calendar' sheet. Every time I run the code, I get an error message that says "Subscript out of range" which is odd because the sheets I am referencing all exist, which you can see in the attachments. I would greatly appreciate if someone could help me with this as I am in the first week of learning how to code with VBA.

*For reference, the Subs below "MoveDates" work, but I am trying to develop a code that copies and pastes all data that is shown in the "Insert CDRN Data".

Thank you!
VBA CODE Help.png
VBA CODE Help2.png
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Your sheet name looks like it is "Calendar " not "Calendar" i.e. a space at the end of the name.
 
Upvote 0
Ahhhh, you have a very keen eye, thank you. I am now getting an error for my last line of code in the first sub
 

Attachments

  • VBACODEHELP3.PNG
    VBACODEHELP3.PNG
    35.6 KB · Views: 3
Upvote 0
Can you post your code in the thread using code tags please rather than images (Copy your code, paste it in the thread, select all the code and click the
1668734489745.png
icon in the reply window as we can't copy/paste the code from an image
 
Upvote 0
Can you post your code in the thread using code tags please rather than images (Copy your code, paste it in the thread, select all the code and click the View attachment 78975 icon in the reply window as we can't copy/paste the code from an image
VBA Code:
Sub MoveDates()

    Sheets("Calendar").Activate
    Range("C3", Range("C3").End(xlDown)).ClearContents
 
 
    Sheets("Insert CDRN Data").Activate
    Range("A2", Range("A2").End(xlDown)).Copy
 
 
    Sheets("Calendar").Activate
    Range("C3").PasteSpecial


End Sub
 
Upvote 0
What happens with the code below?

VBA Code:
Sub MoveDates()

    Sheets("Calendar").Range("C3", Sheets("Calendar").Range("C3").End(xlDown)).ClearContents
 
 
    With Sheets("Insert CDRN Data")
    .Range("A2", .Range("A2").End(xlDown)).Copy Sheets("Calendar").Range("C3")
    End With

End Sub
 
Upvote 0
VBA Code:
Sub MoveDates()

    Sheets("Calendar").Activate
    Range("C3", Range("C3").End(xlDown)).ClearContents
 
 
    Sheets("Insert CDRN Data").Activate
    Range("A2", Range("A2").End(xlDown)).Copy
 
 
    Sheets("Calendar").Activate
    Range("C3").PasteSpecial


End Sub

What happens with the code below?

VBA Code:
Sub MoveDates()

    Sheets("Calendar").Range("C3", Sheets("Calendar").Range("C3").End(xlDown)).ClearContents
 
 
    With Sheets("Insert CDRN Data")
    .Range("A2", .Range("A2").End(xlDown)).Copy Sheets("Calendar").Range("C3")
    End With

End Sub
Great! That worked perfectly. Do you know how to format the pasted data into lets say white and light grey alternating cell colors?
 
Upvote 0
What happens with the code below?

VBA Code:
Sub MoveDates()

    Sheets("Calendar").Range("C3", Sheets("Calendar").Range("C3").End(xlDown)).ClearContents
 
 
    With Sheets("Insert CDRN Data")
    .Range("A2", .Range("A2").End(xlDown)).Copy Sheets("Calendar").Range("C3")
    End With

End Sub
So, I am having a problem with this code now. The data that I am bringing in from the "Insert CDRN Data" sheet sometimes has missing cell values. So, when I run this code for the columns that have missing data, the code stops at the first cell with no data in it. Would you have an alternative code that breaks through the empty cells and continues to look for data to bring over until the absolute last row? For instance, I have attached pictures comparing the two sheets.
VBACODEHELP5.png
VBACODEHELP6.png
 
Upvote 0
Would you have an alternative code that breaks through the empty cells

VBA Code:
Sub MoveDates()

    Sheets("Calendar").Range("C3:C" & Sheets("Calendar").Range("C" & Rows.Count).End(xlUp).Row).ClearContents
 
 
    With Sheets("Insert CDRN Data")
        .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row).Copy Sheets("Calendar").Range("C3")
    End With

End Sub

Do you know how to format the pasted data into lets say white and light grey alternating cell colors


Select the cells in the column column > Home > Conditional Formatting > New Rule > click Use a formula to determine which cells to format >in the Format values where this formula is true box put the formula =
Excel Formula:
MOD(ROW(),2)=0

Click Format > In the Format Cells box, click Fill >Pick a color > click OK.

or

Turn the range into a Table and change the table style to suit

or

run the code below (adjust the colour to suit)

VBA Code:
Sub alternatecolor()

    Dim x As Long

    For x = 3 To Sheets("Calendar").Range("C" & Rows.Count).End(xlUp).Row
        If x Mod 2 = 0 Then
            Sheets("Calendar").Cells(x, "C").Interior.Color = 15132390
        End If
    Next

End Sub
 
Upvote 0
VBA Code:
Sub MoveDates()

    Sheets("Calendar").Range("C3:C" & Sheets("Calendar").Range("C" & Rows.Count).End(xlUp).Row).ClearContents
 
 
    With Sheets("Insert CDRN Data")
        .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row).Copy Sheets("Calendar").Range("C3")
    End With

End Sub




Select the cells in the column column > Home > Conditional Formatting > New Rule > click Use a formula to determine which cells to format >in the Format values where this formula is true box put the formula =
Excel Formula:
MOD(ROW(),2)=0

Click Format > In the Format Cells box, click Fill >Pick a color > click OK.

or

Turn the range into a Table and change the table style to suit

or

run the code below (adjust the colour to suit)

VBA Code:
Sub alternatecolor()

    Dim x As Long

    For x = 3 To Sheets("Calendar").Range("C" & Rows.Count).End(xlUp).Row
        If x Mod 2 = 0 Then
            Sheets("Calendar").Cells(x, "C").Interior.Color = 15132390
        End If
    Next

End Sub
Holllllyyyyyyyyy heck, you are good. I admire your skills, and I appreciate your help!
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,749
Members
449,050
Latest member
excelknuckles

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