VBA code to reference the value in a specified cell , not the formula

JKK22

New Member
Joined
Oct 12, 2022
Messages
17
Office Version
  1. 365
Platform
  1. Windows
I found some VBA code that works for me, except for one line, and am wondering if someone can give me a suggestion.
In the below code, instead of manually designating how many rows to go down (+29) , I have a formula that calculates the # rows to go down in cell B2 on sh1. ( formula to calc because the range changes all the time). Can I have the VBA just reference the value that is returned from the formula in B2?
Dim strFind As String
Dim fRow, fCol As Integer
Dim sh1, sh2 As Worksheet

Set sh1 = Sheets("Inv5")
Set sh2 = Sheets("Inv1")
strFind = sh1.Range("N5").Value
Set foundCell = sh2.Range("C:C").Find(strFind, LookIn:=xlValues)

If Not foundCell Is Nothing Then
fRow = foundCell.Row
fCol = foundCell.Column
sh2.Range(Cells(fRow, fCol + 10).Address & ":" & Cells(fRow + 29, fCol).Address).Copy
sh1.Range("R10").PasteSpecial xlPasteValues
Application.CutCopyMode = False

Else

Call MsgBox("Not found the match cell!", vbExclamation, "Finding String")

End If
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Sure.

You should be able to just replace:
Excel Formula:
fRow + 29
with
Excel Formula:
fRow + Range("B2").Value

Also, you don't to figure out the address to build the range, i.e.
this:
Excel Formula:
sh2.Range(Cells(fRow, fCol + 10).Address & ":" & Cells(fRow + 29, fCol).Address).Copy
can be simplified to this:
Excel Formula:
sh2.Range(Cells(fRow, fCol + 10), Cells(fRow + 29, fCol)).Copy
 
Upvote 0
Your first suggestion worked to change it to Range("B2").Value- Thank You

The second simplification suggestion did not work, it returned an error
 
Upvote 0
If you implement them both together, it should look like:
VBA Code:
sh2.Range(Cells(fRow, fCol + 10), Cells(fRow + Range("B2").Value, fCol)).Copy

If you still get an error, what is the exact error message?

What are your values of "fRow", "fCol" at the time of the error?
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,882
Members
449,097
Latest member
dbomb1414

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