Range Resizing compile error

billyheanue

Board Regular
Joined
Jul 13, 2015
Messages
109
Hi all,

I am trying to resize the range A5 into a different amount of rows, with only 1 column wide.

The cell located in coordinates [13,6] holds a value that contains the number of rows I want the range resized to.

I tried using Cells(13,6).value to accomplish this, but running it is giving me an error, and I think my syntax is all messed up.



Code:
Private Sub CommandButton5_Click()


Range("A5").resize(Cells(13,6).value),1).Formula = "=VLOOKUP(Sheet1!B16,Sheet2!" _
    & Sheets("Sheet2").Range("A4").CurrentRegion.Address & ",Sheet1!G$13,FALSE)"
    
End Sub

Can anyone help me out?

Thanks!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try removing the red )


Code:
Range("A5").resize(Cells(13,6).value[B][COLOR="#FF0000"])[/COLOR][/B],1).Formula = "=VLOOKUP(Sheet1!B16,Sheet2!" _
    & Sheets("Sheet2").Range("A4").CurrentRegion.Address & ",Sheet1!G$13,FALSE)"
 
Upvote 0
Maybe try breaking it down to isolate the error, try:
Code:
Private Sub CommandButton5_Click()

Dim resizeVal as Long
Dim strFormula as String
Dim rng as Range

resizeVal = Cells(13,6).Value
If Not IsNumeric(resizeVal) Then
  Msgbox "Value in F13 is not numeric, range will not resize"
  Exit Sub
End If

strFormula = "=VLOOKUP(@1,Sheet2!@2,@3,0)" '0 is equivalent to FALSE
strFormula = Replace(strFormula, "@1", "Sheet1!B16")
strFormula = Replace(strFormula, "@2", Sheets("Sheet2").Range("A4").CurrentRegion.Address)
strFormula = Replace(strFormula, "@3", "Sheet1!$G$13")

Set rng = Range("A5").Resize(resizeVal)

If Not rng Is Nothing Then
    rng.formula = strformula
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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