Inserting blank column if Space is not available

dstolp

New Member
Joined
Nov 20, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Please refer to the pictures for clarity. I am making a scheduling tool that will map the process times for each day of the week for 4 weeks. The issue I am having is the code I currently have inserts a column for, this example, Monday on week 1. Obviously, this column runs down the following week 2, 3 and 4, which is fine. However, what I would like is when I start a process on monday of week two, the code could see a column is blank, and instead of inserting a new column it just utilizes the previously created column from week 1. For more clarity, week 1 encompasses rows 10-31, and week 2 encompasses rows 41-62.

VBA Code:
'''Week 1 tuesday start'''
 For i = 1 To myWorksheet.Cells(5, myWorksheet.Range("TuesdayStarts").Column)
        myFirstColumnT = myWorksheet.Cells.Find( _
                                             What:="TS", _
                                             LookIn:=xlFormulas, _
                                             LookAt:=xlPart, _
                                             SearchOrder:=xlByColumns, _
                                             SearchDirection:=xlPrevious).Column
                                    

        myLastColumnT = myWorksheet.Cells.Find( _
                                             What:="TE", _
                                             LookIn:=xlFormulas, _
                                             LookAt:=xlPart, _
                                             SearchOrder:=xlByColumns, _
                                             SearchDirection:=xlPrevious).Column
                                         
        For newcol = myLastColumnT To (myFirstColumnT + 1) Step -100000000
            myWorksheet.Columns(newcol).Insert CopyOrigin:=xlFormatFromRightOrBelow
            Sheets("Templates").Range("A2:A7").Copy
            myWorksheet.Cells(10, newcol).PasteSpecial
            Sheets("Templates").Range("H2:H5").Copy
            myWorksheet.Cells(41, newcol).PasteSpecial
        Next newcol
    
        myFirstColumnW = myWorksheet.Cells.Find( _
                                             What:="WfS", _
                                             LookIn:=xlFormulas, _
                                             LookAt:=xlPart, _
                                             SearchOrder:=xlByColumns, _
                                             SearchDirection:=xlPrevious).Column
                                    

        myLastColumnW = myWorksheet.Cells.Find( _
                                             What:="WfE", _
                                             LookIn:=xlFormulas, _
                                             LookAt:=xlPart, _
                                             SearchOrder:=xlByColumns, _
                                             SearchDirection:=xlPrevious).Column
                                         
        For newcol = myLastColumnW To (myFirstColumnW + 1) Step -100000000
            myWorksheet.Columns(newcol).Insert CopyOrigin:=xlFormatFromRightOrBelow
            Sheets("Templates").Range("B2:B16").Copy
            myWorksheet.Cells(10, newcol).PasteSpecial
       next newcol

VBA Code:
'''Week 2 Tuesday Start'''
For i = 1 To myWorksheet.Cells(36, myWorksheet.Range("TuesdayStarts").Column)
myFirstColumnT = myWorksheet.Cells.Find( _
What:="TS", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column


myLastColumnT = myWorksheet.Cells.Find( _
What:="TE", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

For newcol = myLastColumnT To (myFirstColumnT + 1) Step -100000000
myWorksheet.Columns(newcol).Insert CopyOrigin:=xlFormatFromRightOrBelow
Sheets("Templates").Range("A2:A7").Copy
myWorksheet.Cells(41, newcol).PasteSpecial
next newcol
 

Attachments

  • desired Results.PNG
    desired Results.PNG
    39.6 KB · Views: 10
  • Current Results.PNG
    Current Results.PNG
    36.1 KB · Views: 10

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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