VBA help - probably easy to answer

mtietz

New Member
Joined
Jan 21, 2022
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
I am pretty new using VBA

I have a simple goal.

I want to use VBA to input a formula in every blank cell in column H only IF the cell in Column B on the same row has a number. If the cell in column H is not blank, I do not want the VBA to erase its contents, and if Column B is blank or has any text other than just a number, I don't want any changes to that row

Here is the formula
=IF(B11>0,INDEX(REPORT!A:B,MATCH(CONCATENATE("Char #",B11),REPORT!A:A,0)+1,2),"")

Thanks for any help!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
try this:
VBA Code:
Sub test()
formstring = "=IF(B11>0,INDEX(REPORT!A:B,MATCH(CONCATENATE(""Char #"",B11),REPORT!A:A,0)+1,2),"""")"

LastRow = Range("A1").CurrentRegion.Rows.Count
inarr = Range(Cells(1, 1), Cells(LastRow, 8)) ' pick all data from column 1 to 8
For i = 1 To LastRow
 If IsNumeric(inarr(i, 2)) And (inarr(i, 8)) = "" Then
   Range(Cells(i, 8), Cells(i, 8)).Formula = formstring
 End If
 Next i

End Sub
 
Upvote 0
try this:
VBA Code:
Sub test()
formstring = "=IF(B11>0,INDEX(REPORT!A:B,MATCH(CONCATENATE(""Char #"",B11),REPORT!A:A,0)+1,2),"""")"

LastRow = Range("A1").CurrentRegion.Rows.Count
inarr = Range(Cells(1, 1), Cells(LastRow, 8)) ' pick all data from column 1 to 8
For i = 1 To LastRow
 If IsNumeric(inarr(i, 2)) And (inarr(i, 8)) = "" Then
   Range(Cells(i, 8), Cells(i, 8)).Formula = formstring
 End If
 Next i

End Sub
Some comments on that code:
  • Range("A1").CurrentRegion may possibly give a smaller (or greater) range than is required to meet the specifications.
  • IsNumeric returns True for blanks so the code would put a formula in if B & H are blank, contrary to the requirement
  • I was also wondering about why the structure Range(Cells(i, 8), Cells(i, 8)).Formula rather than just Cells(i, 8).Formula?
  • This is just a guess, but I suspect the B11 in the given formula is just an example and the 11 might be supposed to change depending on the row where the formula is being inserted.
Based on the above comments, my suggestion is this.
(I have assumed at least one blank cell in column H in the range covered by the column B data.)

VBA Code:
Sub Insert_Formula()
  Dim c As Range
 
  For Each c In Range("H1:H" & Range("B" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks)
    If Application.IsNumber(c.Offset(, -6).Value) Then
      c.Formula2R1C1 = "=IF(RC2>0,INDEX(REPORT!C1:C2,MATCH(CONCATENATE(""Char #"",RC2),REPORT!C1:C1,0)+1,2),"""")"
    End If
  Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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