Trying to reference my range variable to format the new range... doing something wrong!

Pat_The_Bat

Board Regular
Joined
Jul 12, 2018
Messages
83
I am trying to insert a row above the cells that have text values that begin with the word "ASSET", and the same thing for "Income".
I've got my code below working up to a point
It inserts the row above ASSETS and INCOME correctly
then I want to select cells (B?:D?") to format those 3 cells in the row I just inserted

I thought I would just Dim Asset as Range
and then
Set Asset= Range("B:D" & Assets)

but that isn't working. Any help is appreciated.
Code:
Sub InsertCats2()


'If Sheets("Publish Doc List").Range("Z1:Z100") = "" Then


Dim Assets As Integer, Income As Integer


With Sheets("Publish Doc List")
    Assets = .Range("D:D").Find(what:="ASSETS", after:=.Range("D1")).Row
    
    Income = .Range("D:D").Find(what:="INCOME", after:=.Range("D1"), searchdirection:=xlPrevious).Row
    
End With
    Debug.Print Assets
    Debug.Print Income
    
    Range("D" & Assets).EntireRow.Insert
    Range("D" & Income).EntireRow.Insert
    
    Dim AssetHDR As Range
    Dim IncomeHDR As Range
    
    Set AssetHDR = Range("B:D" & Assets)
    Set IncomeHDR = Range("B:D" & Income)
    
    Debug.Print AssetHDR
    Debug.Print IncomeHDR
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try
Code:
Set AssetHDR = Range("B" & Assets).Resize(, 3)
 
Upvote 0
Assets and Income are single row numbers.
So if you substitute them into your Range declaration, it would look something like this (if Assets was 44):
Code:
Set AssetHDR = Range("B:D44")
which is not a valid range.

Try this format:
Code:
Set AssetHDR = Range("B" & Assets & ":D" & Assets)
which will return:
Code:
Set AssetHDR = Range("B44:D44")
which is a valid range.


EDIT: Or more simply, do it the way that Fluff posted above!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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